Incoming Template: Turn on/off and set brightness based on JSON data

I have a command that I want to execute that looks like this:

curl -X POST -H “Content-Type: application/json” http://localhost:8123/api/services/script/plex_webhook -d ‘{“event”: “media.stop”, “Player”: { “uuid”: “sample-uuid” } }’

And here’s a script that looks something like this:

  plex_webhook:
    alias: "Plex Webhooks"
    sequence:
      - condition: template
        value_template: '{{ Player.uuid == "sample-uuid" }}'
      - service: light.toggle
        data:
          entity_id: light.living_room_floor_lamps

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:

  plex_webhook:
    alias: "Plex Webhooks"
    sequence:
      - condition: template
        value_template: '{{ Player.uuid == "sample-uuid" }}'
      - service: script.turn_on
        data:
          entity_id: script.play_resume_webhook
      - service: script.turn_on
        data:
          entity_id: script.pause_webhook
      - service: script.turn_on
        data:
          entity_id: script.stop_webhook

and then those 3 scripts would look like this:

  play_resume_webhook:
    alias: "Play and Resume Webhooks"
    sequence:
      - condition: template
        value_template: '{{ event == "media.play" or event == "media.resume" }}'
      - service: light.turn_off
        data:
          entity_id: YOUR LIGHT
  pause_webhook:
    alias: "Pause Webhooks"
    sequence:
      - condition: template
        value_template: '{{ event == "media.pause" }}'
      - service: light.turn_on
        data:
          entity_id: YOUR LIGHT
          brightness: 128
  stop_webhook:
    alias: "Stop Webhooks"
    sequence:
      - condition: template
        value_template: '{{ event == "media.stop" }}'
      - service: light.turn_on
        data:
          entity_id: YOUR LIGHT

PS: I would encourage you to use scenes instead of using brightness levels directly. Far easier to add lights down the road this way :slight_smile:

~Cheers

1 Like

I’ve used scenes in the past and yeah the lack of if/else conditions are really awkward.

Thanks.

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:

http://MY_HOME_ASSISTANT_IP:8123/api/events/plex_webhook

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:

{
    “player_uuid”: “MY_PLEX_PLAYER_UUID”,
    "event": "media.resume"
}

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.

- action:
  - data:
      entity_id: light.tradfri_group
    service: light.turn_off
  alias: Theatre Mode On
  condition: []
  id: ‘theatre_mode_on’
  trigger:
  - event_data:
      event: media.play
    event_type: plex_webhook
    platform: event
  - event_data:
      event: media.resume
    event_type: plex_webhook
    platform: event

- action:
  - data:
      entity_id: light.tradfri_group
    service: light.turn_on
  alias: Theatre Mode Off
  condition: []
  id: ‘theatre_mode_off’
  trigger:
  - event_data:
      event: media.pause
    event_type: plex_webhook
    platform: event
  - event_data:
      event: media.stop
    event_type: plex_webhook
    platform: event

This all works really well for me! Hope this is helpful to someone. I’ll try to post back here once I’ve got my project up in GitHub.

Samus:

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)

Did you ever get a chance to post your project on Github? I’d be very interested in seeing how you did it.