Trigger on state (on or off) triggers when nothing defined

I have a switch that triggers an automation. I don’t care if it i s on or off, but I want to trigger every time someone pushes the switch. Therefore, I thought to not have any state defined (e.g. from on to off). However, if I change something at the zwave interface (disable entities) all devices get “an update” and the switch triggers.

How can I avoid this?

If you do not define a to: or from: for a state trigger then the automation will trigger on any change of state or attribute change of the entity. If you only want to trigger on any change of state and ignore attribute changes then use a null to: like this:

trigger:
  - platform: state
    entity_id: switch.your_switch_here
    to: 

It could also be because your switch state is changing from unknown to some state. In which case you could use this trigger:

trigger:
  - platform: state
    entity_id: switch.your_switch_here
    from: 
      - 'on'
      - 'off'

Note however that lists like this are only supported in yaml mode and not the GUI editor. For that you would have to do this:

trigger:
  - platform: state
    entity_id: switch.your_switch_here
    from: 
      - 'on'
  - platform: state
    entity_id: switch.your_switch_here
    from: 
      - 'off'

That’s working.

So if my devices go to unknown and I only have a value for “to”, they all trigger?

That could lead to surprises.

Yes, because they change from unknown to something (on or off). You only want to trigger when they change from on or off.

So it will also trigger from on to unknown.

So I always need something like this, right?

trigger:
  - platform: state
    entity_id: switch.your_switch_here
    from: 
      - 'on'
    to:
      - 'off'
  - platform: state
    entity_id: switch.your_switch_here
    from: 
      - 'off'
    to: 
      - 'on'

Yes indeed, and yes that is one solution. The other is to trigger on all states and to use conditions to check that the from or to state is not unknown.

Also you don’t need to use lists of states if you only have one state:

trigger:
  - platform: state
    entity_id: switch.your_switch_here
    from: 'on'
    to: 'off'
  - platform: state
    entity_id: switch.your_switch_here
    from: 'off'
    to: 'on'
1 Like

Is there such an issue as well when using input_button.xx?

It seems it doesn’t change to on, so I have to leave both fields empty. Not sure if this input can ever become unknown though.

Input buttons don’t have the state on/off they only store the date they were last pressed. For this you would have to do the other method I mentioned:

trigger:
  - platform: state
    entity_id: input_button.your_button_here
    to: 
condition:
  - "{{ trigger.from_state.state not in ['unknown', 'unavailable'] }}"
  - "{{ trigger.to_state.state not in ['unknown', 'unavailable'] }}"
  - "{{ trigger.from_state.state != trigger.to_state.state}}"
2 Likes