Up until just recently, I have been semi-manually starting and stopping the live stream.
Before I log off for the evening, I would calculate the number of seconds between that time and dawn then use that to create this command:
sleep <seconds> && sh script.sh
I would monitor the stream throughout the day (for mod actions like bot whack-a-mole and to count as an extra viewer :) )
Just after sunset when the light had dimmed, I would then attach to the session and terminate the script.
I found that sometimes I would either forget to do the first command, run the command from the wrong path, or miscalculate the duration.
With this in mind, I worked on an automation project using Home Assistant to control the live stream based on the sun's position. The goal was to automate the start and stop of the stream depending on the sun's elevation.
The Plan
I wanted the stream to run from just before sunrise to just after sunset. To achieve this, I decided to start the stream when the sun’s elevation reaches -8.0 degrees (during dawn) and stop it again when the sun’s elevation drops to -8.0 degrees after sunset (during dusk). This would mean the stream ran as the sun travelled across the sky, creating a seamless integration between the time of day and the stream.
Step 1: Controlling the Stream Remotely via SSH
The stream is controlled by two scripts on a remote host, which I can access over SSH. These scripts use tmux
to ensure the stream continues running even if the connection to the remote host is lost.
- Start Script: Checks for the existence of a
tmux
session. If the session doesn’t exist, it creates one. It then sends the command to the session to start the stream script. - Stop Script: Sends a command to the
tmux
session to stop the stream.
I set up Home Assistant to SSH into the host and trigger these scripts.
Step 2: Creating a new switch and adding SSH Commands in Home Assistant
To start with, I had to do the normal key-pair exchange on the home assistant command line, which creates the key file. In order for home assistant to use those keys, you need to copy them to the config folder.
To trigger these scripts, I created two command_line
services in Home Assistant's configuration.yaml
file:
yamlcommand_line: - switch:
name: Pi_Stream
command_on: "ssh -q -i /config/id_rsa -o UserKnownHostsFile=/config/known_hosts user@host sh start.sh"
command_off: "ssh -q -i /config/id_rsa -o UserKnownHostsFile=/config/known_hosts user@host sh stop.sh"
- The command_on: command connects to the remote host, runs the start script, which checks for the
tmux
session, and starts the stream. - The command_off: command connects to the remote host, runs the stop script, which sends the terminate script command to the session, halting the stream.
Step 3: Automating Based on Sun Elevation
The next step was to automate the stream using the sun's elevation. Home Assistant's built-in sun
integration tracks the position of the sun, which can be used to trigger automations.
I created an automation that stops the stream when the sun's elevation reaches -8.0 degrees after sunset:
yaml- id: '1727953002868'alias: Stop Pi Stream description: When its dark triggers: - trigger: numeric_state entity_id: - sun.sun for: hours: 0 minutes: 1 seconds: 0 attribute: elevation below: -8 conditions: [] actions: - action: switch.turn_off metadata: {} data: {} target: entity_id: switch.pi_stream mode: single
Once this automation is triggered, the stream shuts down as dusk deepens. As the stream is currently running at the time of writing, I need to wait to see if this is successful.
Once this is confirmed to be working, I plan to create another automation to start the stream when the sun's elevation rises to -8.0 degrees in the morning.
My next task is to reconfigure how the chat bot works.
Currently, the bot runs from a script, which is running hourly as a cron job.
The plan is to take this out of cron and utilise the SSH function to get home assistant to fire off the script hourly, but only if the stream is running.
This way its not running all the time.
Summary
By integrating Home Assistant with SSH and tmux, I was able to fully automate the control of a video stream based on the sun's position (subject to successful testing in about 4.5 hours according to the MET office). This approach can be adapted to any scenario where a remote script needs to be triggered from Home Assistant. It opens the door to many possibilities, whether for controlling cameras, live streams, or other devices based on environmental factors like light and time of day.
If you're looking to combine the power of Home Assistant's automation with remote scripts, SSH and tmux are excellent tools to ensure your commands run reliably.