The condition in my automation cannot see the entire event

Hello,

I have a new cheap Tuya Zigbee button connected through a Tuya hub and SmartLife. It has imported into HA and I am seeing events in Developer Tools / Events as shown here

I have not been able to access new_state and old_state in a Condition in an automation, and eventually discovered through the trace that this seems to be the entirety of the event that’s being seen in the automation. I’m only allowed one screen shot so here’s a copy/paste:

trigger: event
event_type: state_changed
event_data:
  entity_id: event.wireless_scene_switch_button_1

As you can see all the data is missing. This is consistent with the error messages the condition produces.

Can anyone explain why this is happening and how to rectify it?

You are confusing Event triggers with event entities.

trigger: event
event_type: state_changed

Most of the times new users think they need to use an Event trigger with state_changed as the event type, they’re mistaken… and that goes double when it’s for an event entity. The whole point of event entities is to make it so people don’t have to use Event triggers for these kind of things (i.e. button presses).

This kind of “open” state_changed Event trigger is basically a poor re-creation of a states machine. But HA already has a states machine and triggers designed to monitor it for changes. So, just use a State trigger, that is what it was designed to do.

The “new state” and “old state” you see in the Event can be included as part of the trigger by using the to and from configuration variable of the State trigger. However, in most cases with event entities, it’s best to not use them. Just use a general trigger, then add conditions to check the values you are interested in.

Here’s an example of what you would do to have an automation execute actions based on a specific event_type:

triggers:
  - alias: When the button press updates the state
    trigger: state
    entity_id: event.wireless_scene_switch_button_1
    not_to: 
      - unavailable
    not_from: 
      - unavailable
conditions:
  - alias: Check that the new event type is "click"
    condition: state
    attribute: event_type
    entity_id:  event.wireless_scene_switch_button_1
    state: click
...

You can also use Templates to access the to_state and from_state of the Event entity, which are available in the trigger variable.

triggers:
  - trigger: state
    entity_id: event.wireless_scene_switch_button_1
    not_to: 
      - unavailable
    not_from: 
      - unavailable
conditions:
  - condition: template
    value_template: "{{ trigger.to_state.attributes.event_type == 'click'}}"
...
1 Like

Actually the best thing would be to use an event trigger and just add event_fata and context attributes to it.

Can you post an example of what you’re suggesting?

I have tried various configurations of event_data to detect a specific button event but all have failed.

Detecting any and all button events works because only one constraint, entity_id, is needed. However, when I add more constraints, to narrow it down to a specific button event, the Event Trigger no longer triggers.

state changed event triggers can’t be constrained. It’s all or nothing.