Conditional actions based on entity_id?

Hi all,
I seem to be struggling with writing an automation, and I’m not sure where I’m going wrong.

I’m trying to write automations that, based on the entity_id which triggered the automation, will execute a different action. I’m trying to get away from copying automations and having much duplicates of the same thing. I’m also trying to write this in a separate package for separation.

What I’m currently working with:

timer:
  ## A timer for each light group
  light_gameroom:
    duration: '00:03:00'
  light_bedroom:
    duration: '00:03:00'

automation:
  ## Test conditional - Clear & Start timer when movement detected
- alias: lights movement start timer light_groups
  id: lights_movement_start_timer_light_groups
  trigger:
  - platform: state
    entity_id:
    - binary_sensor.ccamdevice_4_1_111 # bedroom
    - binary_sensor.ccamdevice_3_1_108 # gameroom
    to: "on"
  action:
  - service: timer.cancel
    data_template:
      entity_id: >
        {% if trigger.entity_id == 'binary_sensor.ccamdevice_3_1_108' %}
            timer.light_gameroom
        {% elif trigger.entity_id == 'binary_sensor.ccamdevice_4_1_111' %}
            timer.light_bedroom
        {% endif %}
  - service: timer.start
    data_template:
      entity_id: >
        {% if trigger.entity_id == 'binary_sensor.ccamdevice_3_1_108' %}
            timer.light_gameroom
        {% elif trigger.entity_id == 'binary_sensor.ccamdevice_4_1_111' %}
            timer.light_bedroom
        {% endif %}
      duration: 10

  ## When timer is started, turn on light
- alias: lights timer started light_groups
  id: 'lights_timer_started_light_groups'
  trigger:
  - platform: event
    event_type: timer.started
    event_data:
      entity_id:
      - timer.light_gameroom
      - timer.light_bedroom
  action:
  - service: light.turn_on
    data:
      # gameroom
      # bedroom
      entity_id: >
        {% if trigger.entity_id == 'timer.light_gameroom' %}
            light.yeelight_4
        {% elif trigger.entity_id == 'timer.light_bedroom' %}
            light.yeelight_5
        {% endif %}
      transition: 2
      brightness: 255
      kelvin: 3000

The first automation seems to work, and I can fire the timers as expected.

The second automation does not seem to work for various reasons.

Firstly, the triggers don’t seem to want to accept combined entity_ids, so I saw that it works with:

  trigger:
  - platform: event
    event_type: timer.started
    event_data:
      entity_id: timer.light_gameroom
  - platform: event
    event_type: timer.started
    event_data:
      entity_id: timer.light_bedroom

Secondly, the actions section doesn’t seem to work, which I think is related to the trigger.entity_id not resolving anything. From the docs that seems like its supposed to work.

Is there anyway to resolve/read the trigger payload of an automations trigger?
I tried using the developer tools entities page and this is what I get when I listen to timer.started:

Event 0 fired 0:19:
{
    "event_type": "timer.started",
    "data": {
        "entity_id": "timer.light_gameroom"
    },
    "origin": "LOCAL",
    "time_fired": "2021-01-05T23:19:30.939864+00:00",
    "context": {
        "id": "f9f55fe55d0de01a0bd1104d92a684d5",
        "parent_id": null,
        "user_id": null
    }
}

I’m kinda stuck, not sure what to do.

Any advice?

You are triggering on an event, therefore trigger.entity_id doesn’t exist, the entity_id is part of the event data. Change

trigger.entity_id

To

trigger.event.data.entity_id

Thanks @Burningstone that worked…

But now I’m encountering another error that is even more confusing…

I’m trying to build a third automation:

  ## When timer is finished, reset timer to light_duration value
- alias: lights_timer reset light_groups
  id: 'lights_timer_reset_light_groups'
  trigger:
  - platform: event
    event_type: timer.finished
    event_data:
      entity_id: timer.light_gameroom
  - platform: event
    event_type: timer.finished
    event_data:
      entity_id: timer.light_bedroom
  action:
  - service: timer.cancel
    entity_id: >
        {% if trigger.event.data.entity_id == 'timer.light_gameroom' %}
            timer.light_gameroom
        {% elif trigger.event.data.entity_id == 'timer.light_bedroom' %}
            timer.light_bedroom
        {% endif %}
  - service: timer.start
    entity_id: >
        {% if trigger.event.data.entity_id == 'timer.light_gameroom' %}
            timer.light_gameroom
        {% elif trigger.event.data.entity_id == 'timer.light_bedroom' %}
            timer.light_bedroom
        {% endif %}
    data_template:
      duration: 10

I’ve tried simplifying the action code to:

  action:
  - service: timer.cancel
    entity_id: '{{ trigger.event.data.entity_id }}'
  - service: timer.start
    entity_id: '{{ trigger.event.data.entity_id }}'
    data_template:
      duration: 10

Same issue, seems that when I try to use trigger.event.data.entity_id it throws up an error in the logs:

Invalid config for [automation]: not a valid value for dictionary value @ data['action'][0]['entity_id']. Got None. (See ?, line ?).

I know that the trigger.event.data.entity_id is present, because if I use it in a notification, it works:

  action:
  - service: notify.pushbullet
    data:
      message: 'debug {{ trigger.event.data.entity_id }}'

I get the notification and can see the entity_id as expected.

Any advice?

Also, do you know if there is anywhere I can read more about using trigger entity_ids, and event payloads? Seems that documentation and details on this is quite thin.

If you want to use a template for the entity_id, you have to put it in data (you don’t need data_template anymore since v.0.113) like this:

action:
  - service: timer.start
    data:
      entity_id: '{{ trigger.event.data.entity_id }}'
      duration: 10

Also I as far as I know you don’t need to cancel the timer first when you start a new one right afterwards. It should cancel rhe existing timer autonatically.

Damn it, I already knew that templates need to be in data but I missed that one :frowning:

Got it to work now, thanks for pointing that out!

As for the timer cancelling, I’m not sure why, but I’ve seen that it can be unreliable to restart a timer mid-way reliably… so I added the timer.cancel then timer.start and after that it worked every time.

Thanks for all your help @Burningstone, but I hope I can ask one more question.

I’m trying to add a list of lights to the second automation from my first post.
I’m not getting any errors but none of the lights in the list turn on/off, but work correctly manually otherwise from within HA.
The lights in the other 2 rooms are working as expected.

This is what I’m trying to do:

  action:
  - service: light.turn_on
    data:
      # gameroom
      # bedroom
      # bathroom
      entity_id: >
        {% if (trigger.event.data.entity_id == 'timer.light_gameroom') %}
          light.yeelight_4
        {% elif (trigger.event.data.entity_id == 'timer.light_bedroom') %}
          light.yeelight_5
        {% elif (trigger.event.data.entity_id == 'timer.light_bathroom') %}
          - light.tradfri_bulb_9
          - light.tradfri_bulb_10
          - light.tradfri_bulb_11
          - light.yeelight_2
        {% endif %}
      transition: 2
      brightness: 255
      kelvin: 3000

I got it to work by changing the list format:

  action:
  - service: light.turn_on
    data:
      # gameroom
      # bedroom
      # bathroom
      entity_id: >
        {% if (trigger.event.data.entity_id == 'timer.light_gameroom') %}
          light.yeelight_4
        {% elif (trigger.event.data.entity_id == 'timer.light_bedroom') %}
          light.yeelight_5
        {% elif (trigger.event.data.entity_id == 'timer.light_bathroom') %}
          light.tradfri_bulb_9,light.tradfri_bulb_10,light.tradfri_bulb_11,light.yeelight_2
        {% endif %}
      transition: 2
      brightness: 255
      kelvin: 3000

Another way would be to create a light group and use this instead of listing all lights.

I also suggest to read up on variables. This can help you making your automation more readable. E.g.

This:

action:
  - service: light.turn_on
    data:
      # gameroom
      # bedroom
      # bathroom
      entity_id: >
        {% if (trigger.event.data.entity_id == 'timer.light_gameroom') %}
          light.yeelight_4
        {% elif (trigger.event.data.entity_id == 'timer.light_bedroom') %}
          light.yeelight_5
        {% elif (trigger.event.data.entity_id == 'timer.light_bathroom') %}
          light.tradfri_bulb_9,light.tradfri_bulb_10,light.tradfri_bulb_11,light.yeelight_2
        {% endif %}
      transition: 2
      brightness: 255
      kelvin: 3000

Becomes this:

action:
  - variables:
      timer: "{{ trigger.event.data.entity_id }}"
  - service: light.turn_on
    data:
      # gameroom
      # bedroom
      # bathroom
      entity_id: >
        {% if (timer == 'timer.light_gameroom') %}
          light.yeelight_4
        {% elif (timer == 'timer.light_bedroom') %}
          light.yeelight_5
        {% elif (ttimer == 'timer.light_bathroom') %}
          light.tradfri_bulb_9,light.tradfri_bulb_10,light.tradfri_bulb_11,light.yeelight_2
        {% endif %}
      transition: 2
      brightness: 255
      kelvin: 3000