In Episode 66, there’s a discussion of using virtual switches in Homebridge for automation and I would love to learn more about how I could use this. Would anyone who’s using this care to share an example of how the switch triggers an automation or works with an automation?
Here’s one thing I use them for.
I’m have a Plex server on my lowly Raspberry Pi. It has enough power on it to stream 4K media.
But, it sometimes just hangs during the day and won’t stream anything. So I scheduled it to reboot regularly (every 8 hours) and that worked well.
Of course I did’t want it to reboot while streaming so I made a dummy switch called “KeepAlive”. I changed my reboot script to check the switch’s value before doing a reboot.
I could manually turn on the switch before I stream anything but we’re automators. We don’t do that. So, now I have an automation on my Home Assistant instance that turns on the switch when something starts playing and turns it off when it’s idle for 30 mins.
What are you using the for scripting? Applescript? Shell script? Keyboard Maestro?
I use a shell script for the reboot script. Here are a few more details of the setup.
Homebridge
- http-webhooks plugin for the dummy switches
- Dummy switch named
KeepAlive_hostname
wherehostname
is the name of the computer I want to not reboot. E.g.KeepAlive_plex
Config
{
"platforms": [
{
"platform": "HttpWebHooks",
"webhook_port": "51829",
"switches": [
{
"id": "KeepAlive_plex",
"name": "KeepAlive Plex"
}
]
}
]
}
Plex Server
- cron job to run a reboot script every 8 hours.
reboot.sh
#!/bin/bash
logfile=$HOME/cron/cron.log
keepalive=$(/usr/bin/curl -s http://homebridge-server:51829/?accessoryId=KeepAlive_$(hostname) | /usr/bin/jq -r '.state')
time=$(date "+%Y/%m/%d %H:%M:%S")
if [ "$keepalive" != "true" ]; then
echo "$time reboot.sh keepalive is off. rebooting" >> $HOME/cron/cron.log
sudo /sbin/reboot
fi
Home Assistant
- Plex Media Server integration
configuration.yaml
# configuration.yaml
rest_command:
keepalive_plex_on:
url: "http://homebridge-server:51829/?accessoryId=KeepAlive_plex&state=true"
method: GET
keepalive_plex_off:
url: "http://homebridge-server:51829/?accessoryId=KeepAlive_plex&state=false"
method: GET
automation: !include automations.yaml
automations.yaml
# automations.yaml
plex_is_playing:
- alias: Plex is playing
trigger:
platform: state
entity_id: media_player.plex_plex_for_apple_tv_apple_tv
to: "playing"
action:
service: rest_command.keepalive_plex_on
plex_is_idle:
- alias: Plex is idle for 30m
trigger:
platform: state
entity_id: media_player.plex_plex_for_apple_tv_apple_tv
to: "idle"
for: "00:30:00"
action:
service: rest_command.keepalive_plex_off
plex_is_unavailable:
- alias: Plex is unavailable
trigger:
platform: state
entity_id: media_player.plex_plex_for_apple_tv_apple_tv
to: "unavailable"
action:
service: rest_command.keepalive_plex_off