Cannot trigger automation on event

Hi all
I am trying to setup a very simple automation which should trigger on a button push of a Friends Of Hue switch. For the integration of the switch, I use the “Hue sensor advanced” integration from HACS, however I don’t think my issue is related to that integration.

I can observe the event which I’m trying to listen to in the event listener, it looks as follows:

{
    "event_type": "state_changed",
    "data": {
        "entity_id": "remote.schalter",
        "old_state": {
            "entity_id": "remote.schalter",
            "state": "left_upper_release",
            "attributes": {
                "last_updated": [
                    "2020-02-23",
                    "20:12:20"
                ],
                "last_button_event": "left_upper_release",
                "friendly_name": "Schalter",
                "icon": "mdi:light-switch",
                "supported_features": 0
            },
            "last_changed": "2020-02-23T20:12:20.079984+00:00",
            "last_updated": "2020-02-23T20:12:20.079984+00:00",
            "context": {
                "id": "45bc2685c7274f14a9c00ec131cf3f54",
                "parent_id": null,
                "user_id": null
            }
        },
        "new_state": {
            "entity_id": "remote.schalter",
            "state": "left_upper_release",
            "attributes": {
                "last_updated": [
                    "2020-02-24",
                    "15:11:30"
                ],
                "last_button_event": "left_upper_release",
                "friendly_name": "Schalter",
                "icon": "mdi:light-switch",
                "supported_features": 0
            },
            "last_changed": "2020-02-24T15:11:30.057561+00:00",
            "last_updated": "2020-02-24T15:11:30.057561+00:00",
            "context": {
                "id": "2cf27fb6365345fd8f8bf0389984cd1c",
                "parent_id": null,
                "user_id": null
            }
        }
    },
    "origin": "LOCAL",
    "time_fired": "2020-02-24T15:11:30.057649+00:00",
    "context": {
        "id": "2cf27fb6365345fd8f8bf0389984cd1c",
        "parent_id": null,
        "user_id": null
    }
}

I prepared the following trigger section of the automation, however it doesn’t trigger at all:

platform: event
event_type: state_changed
event_data:
  entity_id: remote.schalter
  new_state:
    state: left_upper_release

The following simplified version of the trigger behaves as expected; it triggers on all state changes of the switch:

platform: event
event_type: state_changed
event_data:
  entity_id: remote.schalter

I feel like I I’m missing out on a basic concept of the trigger structure. In general, how can I generate a YAML automation from the output of the event log?

Edit: typo

You’re doing it the hard way. You should use a state trigger.

platform: state
entity_id: remote.schalter
to: left_upper_release

Thanks very much, that works too!

However, I don’t understand what the problem on my first approach is and would like to understand it for more complex automation - shouldn’t the event trigger work as-is?

The way the event trigger works is, if you specify any key under event_data, then if the event contains that key, the values much match exactly. You (effectively) specified that the value of new_state should be:

        "new_state": {
            "state": "left_upper_release"
        }

but it was:

        "new_state": {
            "entity_id": "remote.schalter",
            "state": "left_upper_release",
            "attributes": {
                "last_updated": [
                    "2020-02-24",
                    "15:11:30"
                ],
                "last_button_event": "left_upper_release",
                "friendly_name": "Schalter",
                "icon": "mdi:light-switch",
                "supported_features": 0
            },
            "last_changed": "2020-02-24T15:11:30.057561+00:00",
            "last_updated": "2020-02-24T15:11:30.057561+00:00",
            "context": {
                "id": "2cf27fb6365345fd8f8bf0389984cd1c",
                "parent_id": null,
                "user_id": null
            }
        }

which doesn’t match exactly. (I know, because I made the same bad assumption before! :wink:)

You could have done it this way:

automation:
  trigger:
    platform: event
    event_type: state_changed
    event_data:
      entity_id: remote.schalter
  condition:
    condition: template
    value_template: "{{ trigger.event.data.new_state.state == 'left_upper_release' }}"

But, of course, this is exactly the case that the state trigger is for.

3 Likes

Thanks very much for the explanation, this helps a lot… Now I understand it!

1 Like