I want to trigger a light, script, or a scene that is based on the “event,” be it “media.play”, “media.stop”, “media.resume”, “media.pause”, etc.
So for example:
I want to execute the script/scene/light (on/off) depending on the following event:
If I play or resume playback, turn off the light.
If I pause playback, set the brightness to 128.
If I stop playback, turn on the lights.
How will I accomplish this?
I know I have started a thread about webhooks but I would like to start a thread fresh so that others can make use of the solution without scrolling down to find the right post.
Sadly you will have to use mutliple scrpits for this as HA does not allow for if/else constructs. I would create 1 Master script which looks like this:
Sorry to dig up this thread, but I was having the same exact problem as the OP and found an elegant solution. I eventually realized that the flexibility I needed was with HA events/automations rather than scripts.
I created a very simple node.js app that accepts Plex webhooks and fires events in Home Assistant via the RESTful API. I’ll be putting up my code in GitHub when I get a chance, but basically the URL I’m using to trigger the event is:
I didn’t need to define plex_webhook anywhere ahead of time… Whatever custom event you put after /events/ will get fired. In the body of the request, I put a simple JSON object:
I then created the automations “Theatre Mode On” and “Theatre Mode Off” in automations.yaml. Each of these automations has two triggers… one for each of the event types I’m looking for. Theatre Mode On is triggered if the event is media.play or media.resume, and Theatre Mode Off is triggered if the event is media.pause or media.stop. If I had more complex stuff to do in each of these modes, I might have these automations trigger scripts. For my purposes, this was enough.
I tried to configure something similar, but I need to validate 2 of the JSON payload element (both need to be present with a given value).
Have you tried doing the same if you test on 2 conditions in the JSON? E.g how would this automation look like if you wanted only to trigger if the payload in the JSON is event=media.play and player_uuid = “MY_PLEX_PLAYER_UUID" (both data element and values must be present to trigger it)