Automations with Sunshine: Is there a service that runs when a streaming session is initiated and terminates when it ends? #209
Replies: 0 comments 2 replies
-
The do/undo commands are the best we have right now. We've been thinking of the idea of webhooks or something along those lines, but no work has been done on this as of yet as it's not a big priority. |
Beta Was this translation helpful? Give feedback.
-
This might help. I have two scripts In sunshine settings, edit Desktop or what ever application, look for Add something like this for Do Command:
Add something like this for Undo Command:
Enable-Light.ps1: $BaseUrl = "http://homeassistant.local:8123"
$AccessKey = "<Your Token Here>"
$Header = @{ Authorization = "Bearer $AccessKey"; "Content-Type" = "application/json" }
$Entity = "switch.<Some Switch Id Here>"
$On = "/api/services/switch/turn_on"
$Body = @{entity_id = $Entity } | ConvertTo-Json
$uri = "$BaseUrl$On"
Invoke-RestMethod -Method Post -Uri $uri -Headers $Header -Body $Body Disable-Light.ps1: $BaseUrl = "http://homeassistant.local:8123"
$AccessKey = "<Your Token Here>"
$Header = @{ Authorization = "Bearer $AccessKey"; "Content-Type" = "application/json" }
$Entity = "switch.<Some Switch Id Here>"
$Off = "/api/services/switch/turn_off"
$Body = @{entity_id = $Entity } | ConvertTo-Json
$uri = "$BaseUrl$Off"
Invoke-RestMethod -Method Post -Uri $uri -Headers $Header -Body $Body |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
I like to run automations using Home Assistant with Sunshine/Moonlight when a stream is initiated or terminated. For example, I'll automatically change the output on my HDMI switch to a dummy plug, change the desktop resolution to match my client, and turn off my TV when I start a stream. The reverse will happen when I end the stream. It essentially gives my gaming PC "docking" functionality like a Nintendo Switch, but wireless.
Anyway, this requires a reliable indication of when a stream starts and ends. With NVIDIA Gamestream it was simple, because the Gamestream service/exe would only start when a stream was initiated, and stop when the stream ended (which also worked during a crash). Therefore, I could just watch for this service in my automations. However, the Sunshine service seems to run constantly in the background (which has it's own benefits like connection speed), which means I can't tell when a client connects or disconnects directly.
I currently have it working with a dummy service that runs before and stops after a stream via Command Preparations. However, this can be quite buggy when something unexpected happens like a crash, which will leave the dummy service running (because the "Undo" command won't run) and cause the prep-commands to fail on future stream attempts. Before I spend time programming around this with Batch scripting, I was wondering if there was a more direct method of doing this with Sunshine?
Beta Was this translation helpful? Give feedback.
All reactions