Trouble Matching `trigger.event_id` in `choose` Action with `event` `state` Entity Trigger

I have the following automation:

alias: Linked Circuit Toggles
description: ""
trigger:
  - platform: state
    entity_id:
      - event.living_room_5_can_4_way_scene_003
      - event.living_room_5_can_scene_003
      - event.living_room_1_can_scene_003
      - event.dining_room_inner_lights_scene_003
      - event.dining_room_outer_lights_scene_003
      - event.garage_2_fixture_3_way_scene_003
      - event.garage_2_fixture_lights_scene_003
      - event.garage_4_fixture_lights_scene_003
condition:
  - condition: template
    value_template: "{{ state_attr(trigger.entity_id, 'event_type') == 'KeyPressed' }}"
    alias: Trigger was KeyPressed
action:
  - choose:
      - conditions:
          - condition: or
            conditions:
              - condition: template
                value_template: >-
                  {{ trigger.entity_id ==
                  'event.living_room_5_can_4_way_scene_003' }}
              - condition: template
                value_template: "{{ trigger.entity_id == 'event.living_room_5_can_scene_003' }}"
        sequence:
          - service: light.toggle
            metadata: {}
            data: {}
            target:
              entity_id: light.living_room_5_can
        alias: Toggle Living Room 5 Can
      - conditions:
          - condition: template
            value_template: "{{ trigger.entity_id == 'event.living_room_1_can_scene_003' }}"
        sequence:
          - service: light.toggle
            metadata: {}
            data: {}
            target:
              entity_id: light.living_room_1_can
        alias: Toggle Living Room 1 Can
      - conditions:
          - condition: template
            value_template: "{{ trigger.entity_id == 'event.dining_room_outer_scene_003' }}"
        sequence:
          - service: light.toggle
            metadata: {}
            data: {}
            target:
              entity_id: light.dining_room_4_can_switch
        alias: Toggle Dining Room Outer
      - conditions:
          - condition: template
            value_template: "{{ trigger.entity_id == 'event.dining_room_inner_scene_003' }}"
        sequence:
          - service: light.toggle
            metadata: {}
            data: {}
            target:
              entity_id: light.dining_room_2_can_switch
        alias: Toggle Dining Room Inner
      - conditions:
          - condition: or
            conditions:
              - condition: template
                value_template: >-
                  {{ trigger.entity_id ==
                  'event.garage_2_fixture_3_way_scene_003' }}
              - condition: template
                value_template: >-
                  {{ trigger.entity_id ==
                  'event.garage_2_fixture_lights_scene_003' }}
        sequence:
          - service: light.toggle
            metadata: {}
            data: {}
            target:
              entity_id: light.garage_2_fixture_lights
        alias: Toggle Garage 2 Fixture
      - conditions:
          - condition: template
            value_template: >-
              {{ trigger.entity_id == 'event.garage_4_fixture_lights_scene_003'
              }}
        sequence:
          - service: light.toggle
            metadata: {}
            data: {}
            target:
              entity_id: light.garage_4_fixture_lights
        alias: Toggle Garage 4 Fixture
mode: single

When I trigger the automation via an event that matches the conditions, the choose action is bypassed because the choices don’t match. The choices were originally formatted like value_template: "{{ trigger.entity_id == event.dining_room_inner_lights_scene_003 }}" (no quotes on the right side of the comparison) and some searching lead me to believe that was the problem, but adjusting to the format in the example above didn’t change the behavior. Here is an example of the “changed variables” section of a trace when I triggered the automation:

trigger:
  id: '0'
  idx: '0'
  alias: null
  platform: state
  entity_id: event.dining_room_inner_lights_scene_003
  from_state:
    entity_id: event.dining_room_inner_lights_scene_003
    state: '2023-12-16T01:38:02.404+00:00'
    attributes:
      event_types:
        - KeyHeldDown
        - KeyPressed
        - KeyPressed2x
        - KeyPressed3x
        - KeyPressed4x
        - KeyPressed5x
        - KeyReleased
      event_type: KeyPressed
      value: 0
      friendly_name: Dining Room Inner Lights Scene 003
    last_changed: '2024-03-24T20:42:53.973549+00:00'
    last_updated: '2024-03-24T20:42:53.973549+00:00'
    context:
      id: 01HSS37C4NRR1E39034DFQYH9W
      parent_id: null
      user_id: null
  to_state:
    entity_id: event.dining_room_inner_lights_scene_003
    state: '2024-03-26T14:32:12.476+00:00'
    attributes:
      event_types:
        - KeyHeldDown
        - KeyPressed
        - KeyPressed2x
        - KeyPressed3x
        - KeyPressed4x
        - KeyPressed5x
        - KeyReleased
      event_type: KeyPressed
      value: 0
      friendly_name: Dining Room Inner Lights Scene 003
    last_changed: '2024-03-26T14:32:12.477181+00:00'
    last_updated: '2024-03-26T14:32:12.477181+00:00'
    context:
      id: 01HSXJT1XX32GK18XAK4QGMH0V
      parent_id: null
      user_id: null
  for: null
  attribute: null
  description: state of event.dining_room_inner_lights_scene_003

It looks to me like this clearly shows trigger.entity_id existing (and matching), so I’m not sure if/why I should specifically choose to use trigger.to_state.entity_id or trigger.from_state.entity_id when my trigger doesn’t require the state be changing in a specific way. I would like to understand why even if that is what I need to do, but I also suspect this will be a syntax issue. Unfortunately, I’m not getting the errors I usually get with syntax issues, so I’m not really certain. I know I could separate out the triggers and use trigger.id, but I’d like to understand why this isn’t working even if that is the only solution. Can anyone point me in the right direction here?

  - condition: template
    value_template: "{{ trigger.to_state.attributes.event_type == 'KeyPressed' }}"

for your other conditions, just use

{{ trigger.to_state.entity_id == 'event.living_room_5_can_4_way_scene_003' }}

EDIT: however you should just put that in your trigger. (does not work that way, see below)

Attribute triggers don’t work here, I wish they did. It makes much more sense for event entities. It works the first time. If you trigger the same scene again, attribute is the same and state is ignored.

When the attribute option is specified, the trigger only fires when the specified attribute changes. Changes to other attributes or the state are ignored.

https://www.home-assistant.io/docs/automation/trigger/#triggering-on-attribute-changes

1 Like

Look at that nugget of info. Interesting.

Yeah, I think we talked on Discord and that’s why I didn’t use to_state. The automation is triggering fine, though, the issue is the templates in the action section not matching even though the stuff shows up in the trace. I’m not sure why it would need to change, doesn’t it just need to match?

To elaborate, the problem is a failure to match here:

action:
  - choose:
      - conditions:
          - condition: template
            value_template: "{{ trigger.entity_id == 'event.dining_room_inner_scene_003' }}"

even though it looks to me like the trigger in the trace has that here:

trigger:
  id: '0'
  idx: '0'
  alias: null
  platform: state
  entity_id: event.dining_room_inner_lights_scene_003

Also, in case that still isn’t clear, here is the trace timeline:

Triggered by the state of event.dining_room_inner_lights_scene_003 at March 26, 2024 at 01:32:12

Trigger was KeyPressed

Choose: No action taken

Finished at March 26, 2024 at 01:32:12 (runtime: 0.02 seconds)

See specifically “Choose: No action taken”

While I would still have to use a condition for the reason you edited, I could create 8 separate triggers and match the choose based on trigger ID(s), so I can already work around this. The purpose of this thread is to help me understand why the specific thing in my post above isn’t working.

Post the full trace of your automation.

When you are on the trace where it should have ran, go to the 3 dots and click download trace. Paste the results here.

{
  "trace": {
    "last_step": "action/0/choose/5/conditions/0",
    "run_id": "c3b6e81d14c31ea29bf8016b970a4772",
    "state": "stopped",
    "script_execution": "finished",
    "timestamp": {
      "start": "2024-03-26T14:32:12.482178+00:00",
      "finish": "2024-03-26T14:32:12.500183+00:00"
    },
    "domain": "automation",
    "item_id": "1710801588032",
    "trigger": "state of event.dining_room_inner_lights_scene_003",
    "trace": {
      "trigger/0": [
        {
          "path": "trigger/0",
          "timestamp": "2024-03-26T14:32:12.482299+00:00",
          "changed_variables": {
            "this": {
              "entity_id": "automation.linked_circuit_toggles",
              "state": "on",
              "attributes": {
                "id": "1710801588032",
                "last_triggered": "2024-03-26T13:51:58.365241+00:00",
                "mode": "single",
                "current": 0,
                "friendly_name": "Linked Circuit Toggles"
              },
              "last_changed": "2024-03-26T14:30:51.203479+00:00",
              "last_updated": "2024-03-26T14:30:51.203479+00:00",
              "context": {
                "id": "01HSXJQJJ3Q9KWA9BTNG4HA0ZX",
                "parent_id": null,
                "user_id": null
              }
            },
            "trigger": {
              "id": "0",
              "idx": "0",
              "alias": null,
              "platform": "state",
              "entity_id": "event.dining_room_inner_lights_scene_003",
              "from_state": {
                "entity_id": "event.dining_room_inner_lights_scene_003",
                "state": "2023-12-16T01:38:02.404+00:00",
                "attributes": {
                  "event_types": [
                    "KeyHeldDown",
                    "KeyPressed",
                    "KeyPressed2x",
                    "KeyPressed3x",
                    "KeyPressed4x",
                    "KeyPressed5x",
                    "KeyReleased"
                  ],
                  "event_type": "KeyPressed",
                  "value": 0,
                  "friendly_name": "Dining Room Inner Lights Scene 003"
                },
                "last_changed": "2024-03-24T20:42:53.973549+00:00",
                "last_updated": "2024-03-24T20:42:53.973549+00:00",
                "context": {
                  "id": "01HSS37C4NRR1E39034DFQYH9W",
                  "parent_id": null,
                  "user_id": null
                }
              },
              "to_state": {
                "entity_id": "event.dining_room_inner_lights_scene_003",
                "state": "2024-03-26T14:32:12.476+00:00",
                "attributes": {
                  "event_types": [
                    "KeyHeldDown",
                    "KeyPressed",
                    "KeyPressed2x",
                    "KeyPressed3x",
                    "KeyPressed4x",
                    "KeyPressed5x",
                    "KeyReleased"
                  ],
                  "event_type": "KeyPressed",
                  "value": 0,
                  "friendly_name": "Dining Room Inner Lights Scene 003"
                },
                "last_changed": "2024-03-26T14:32:12.477181+00:00",
                "last_updated": "2024-03-26T14:32:12.477181+00:00",
                "context": {
                  "id": "01HSXJT1XX32GK18XAK4QGMH0V",
                  "parent_id": null,
                  "user_id": null
                }
              },
              "for": null,
              "attribute": null,
              "description": "state of event.dining_room_inner_lights_scene_003"
            }
          }
        }
      ],
      "condition/0": [
        {
          "path": "condition/0",
          "timestamp": "2024-03-26T14:32:12.482369+00:00",
          "result": {
            "result": true,
            "entities": [
              "event.dining_room_inner_lights_scene_003"
            ]
          }
        }
      ],
      "action/0": [
        {
          "path": "action/0",
          "timestamp": "2024-03-26T14:32:12.486098+00:00",
          "changed_variables": {
            "context": {
              "id": "01HSXJT1Y2ZCWR0RY9MVS92JH3",
              "parent_id": "01HSXJT1XX32GK18XAK4QGMH0V",
              "user_id": null
            }
          }
        }
      ],
      "action/0/choose/0": [
        {
          "path": "action/0/choose/0",
          "timestamp": "2024-03-26T14:32:12.487504+00:00",
          "result": {
            "result": false
          }
        }
      ],
      "action/0/choose/0/conditions/0": [
        {
          "path": "action/0/choose/0/conditions/0",
          "timestamp": "2024-03-26T14:32:12.487607+00:00",
          "result": {
            "result": false
          }
        }
      ],
      "action/0/choose/0/conditions/0/conditions/0": [
        {
          "path": "action/0/choose/0/conditions/0/conditions/0",
          "timestamp": "2024-03-26T14:32:12.487711+00:00",
          "result": {
            "result": false,
            "entities": []
          }
        }
      ],
      "action/0/choose/0/conditions/0/conditions/1": [
        {
          "path": "action/0/choose/0/conditions/0/conditions/1",
          "timestamp": "2024-03-26T14:32:12.488169+00:00",
          "result": {
            "result": false,
            "entities": []
          }
        }
      ],
      "action/0/choose/1": [
        {
          "path": "action/0/choose/1",
          "timestamp": "2024-03-26T14:32:12.488537+00:00",
          "result": {
            "result": false
          }
        }
      ],
      "action/0/choose/1/conditions/0": [
        {
          "path": "action/0/choose/1/conditions/0",
          "timestamp": "2024-03-26T14:32:12.488612+00:00",
          "result": {
            "result": false,
            "entities": []
          }
        }
      ],
      "action/0/choose/2": [
        {
          "path": "action/0/choose/2",
          "timestamp": "2024-03-26T14:32:12.488946+00:00",
          "result": {
            "result": false
          }
        }
      ],
      "action/0/choose/2/conditions/0": [
        {
          "path": "action/0/choose/2/conditions/0",
          "timestamp": "2024-03-26T14:32:12.489014+00:00",
          "result": {
            "result": false,
            "entities": []
          }
        }
      ],
      "action/0/choose/3": [
        {
          "path": "action/0/choose/3",
          "timestamp": "2024-03-26T14:32:12.489329+00:00",
          "result": {
            "result": false
          }
        }
      ],
      "action/0/choose/3/conditions/0": [
        {
          "path": "action/0/choose/3/conditions/0",
          "timestamp": "2024-03-26T14:32:12.489400+00:00",
          "result": {
            "result": false,
            "entities": []
          }
        }
      ],
      "action/0/choose/4": [
        {
          "path": "action/0/choose/4",
          "timestamp": "2024-03-26T14:32:12.490098+00:00",
          "result": {
            "result": false
          }
        }
      ],
      "action/0/choose/4/conditions/0": [
        {
          "path": "action/0/choose/4/conditions/0",
          "timestamp": "2024-03-26T14:32:12.490201+00:00",
          "result": {
            "result": false
          }
        }
      ],
      "action/0/choose/4/conditions/0/conditions/0": [
        {
          "path": "action/0/choose/4/conditions/0/conditions/0",
          "timestamp": "2024-03-26T14:32:12.493298+00:00",
          "result": {
            "result": false,
            "entities": []
          }
        }
      ],
      "action/0/choose/4/conditions/0/conditions/1": [
        {
          "path": "action/0/choose/4/conditions/0/conditions/1",
          "timestamp": "2024-03-26T14:32:12.493799+00:00",
          "result": {
            "result": false,
            "entities": []
          }
        }
      ],
      "action/0/choose/5": [
        {
          "path": "action/0/choose/5",
          "timestamp": "2024-03-26T14:32:12.494260+00:00",
          "result": {
            "result": false
          }
        }
      ],
      "action/0/choose/5/conditions/0": [
        {
          "path": "action/0/choose/5/conditions/0",
          "timestamp": "2024-03-26T14:32:12.494344+00:00",
          "result": {
            "result": false,
            "entities": []
          }
        }
      ]
    },
    "config": {
      "id": "1710801588032",
      "alias": "Linked Circuit Toggles",
      "description": "",
      "trigger": [
        {
          "platform": "state",
          "entity_id": [
            "event.living_room_5_can_4_way_scene_003",
            "event.living_room_5_can_scene_003",
            "event.living_room_1_can_scene_003",
            "event.dining_room_inner_lights_scene_003",
            "event.dining_room_outer_lights_scene_003",
            "light.garage_2_fixture_3_way",
            "light.garage_2_fixture_lights",
            "light.garage_4_fixture_lights"
          ]
        }
      ],
      "condition": [
        {
          "condition": "template",
          "value_template": "{{ state_attr(trigger.entity_id, 'event_type') == 'KeyPressed' }}",
          "alias": "Trigger was KeyPressed"
        }
      ],
      "action": [
        {
          "choose": [
            {
              "conditions": [
                {
                  "condition": "or",
                  "conditions": [
                    {
                      "condition": "template",
                      "value_template": "{{ trigger.entity_id == 'event.living_room_5_can_4_way_scene_003' }}"
                    },
                    {
                      "condition": "template",
                      "value_template": "{{ trigger.entity_id == 'event.living_room_5_can_scene_003' }}"
                    }
                  ]
                }
              ],
              "sequence": [
                {
                  "service": "light.toggle",
                  "metadata": {},
                  "data": {},
                  "target": {
                    "entity_id": "light.living_room_5_can"
                  }
                }
              ],
              "alias": "Toggle Living Room 5 Can"
            },
            {
              "conditions": [
                {
                  "condition": "template",
                  "value_template": "{{ trigger.entity_id == 'event.living_room_1_can_scene_003' }}"
                }
              ],
              "sequence": [
                {
                  "service": "light.toggle",
                  "metadata": {},
                  "data": {},
                  "target": {
                    "entity_id": "light.living_room_1_can"
                  }
                }
              ],
              "alias": "Toggle Living Room 1 Can"
            },
            {
              "conditions": [
                {
                  "condition": "template",
                  "value_template": "{{ trigger.entity_id == 'event.dining_room_outer_scene_003' }}"
                }
              ],
              "sequence": [
                {
                  "service": "light.toggle",
                  "metadata": {},
                  "data": {},
                  "target": {
                    "entity_id": "light.dining_room_4_can_switch"
                  }
                }
              ],
              "alias": "Toggle Dining Room Outer"
            },
            {
              "conditions": [
                {
                  "condition": "template",
                  "value_template": "{{ trigger.entity_id == 'event.dining_room_inner_scene_003' }}"
                }
              ],
              "sequence": [
                {
                  "service": "light.toggle",
                  "metadata": {},
                  "data": {},
                  "target": {
                    "entity_id": "light.dining_room_2_can_switch"
                  }
                }
              ],
              "alias": "Toggle Dining Room Inner"
            },
            {
              "conditions": [
                {
                  "condition": "or",
                  "conditions": [
                    {
                      "condition": "template",
                      "value_template": "{{ trigger.entity_id == 'event.garage_2_fixture_3_way_scene_003' }}"
                    },
                    {
                      "condition": "template",
                      "value_template": "{{ trigger.entity_id == 'event.garage_2_fixture_lights_scene_003' }}"
                    }
                  ]
                }
              ],
              "sequence": [
                {
                  "service": "light.toggle",
                  "metadata": {},
                  "data": {},
                  "target": {
                    "entity_id": "light.garage_2_fixture_lights"
                  }
                }
              ],
              "alias": "Toggle Garage 2 Fixture"
            },
            {
              "conditions": [
                {
                  "condition": "template",
                  "value_template": "{{ trigger.entity_id == 'event.garage_4_fixture_lights_scene_003' }}"
                }
              ],
              "sequence": [
                {
                  "service": "light.toggle",
                  "metadata": {},
                  "data": {},
                  "target": {
                    "entity_id": "light.garage_4_fixture_lights"
                  }
                }
              ],
              "alias": "Toggle Garage 4 Fixture"
            }
          ]
        }
      ],
      "mode": "single"
    },
    "blueprint_inputs": null,
    "context": {
      "id": "01HSXJT1Y2ZCWR0RY9MVS92JH3",
      "parent_id": "01HSXJT1XX32GK18XAK4QGMH0V",
      "user_id": null
    }
  },
  "logbookEntries": [
    {
      "name": "Linked Circuit Toggles",
      "message": "triggered by state of event.dining_room_inner_lights_scene_003",
      "source": "state of event.dining_room_inner_lights_scene_003",
      "entity_id": "automation.linked_circuit_toggles",
      "context_id": "01HSXJT1Y2ZCWR0RY9MVS92JH3",
      "when": 1711463532.482898,
      "domain": "automation"
    }
  ]
}

the triggering event for that trace is event.dining_room_inner_lights_scene_003, which isn’t defined in your automation.

You programmed

{{ trigger.entity_id == 'event.dining_room_inner_scene_003' }}
1 Like

Sure enough, and the typo was only on the ‘event.dining_room…’ choose conditions, the others didn’t have the typo, but I tried them prior to adding the quotes around the entities and then these after not taking into consideration that I was thusly changing multiple variables at once. Thanks for the help.