Trigger.to_state.state returns inverted value

HI,

I’m trying make an automation using trigger.to_state.state, but ran into trouble.
As I understood, trigger.to_state.state should return the new state that triggered trigger.
But in below simple automation, automation works correctly only when I change values in inverted (as shown).
i.e. without inverting (‘on’ to ‘off’ and ‘off’ to ‘on’), when state of motion sensor changes to ‘on’ (detected), switches turns off and and vice versa.

      trigger:
      - platform: state
        entity_id: binary_sensor.occupancy
      action:
      - choose:
        - conditions: >
            {{ trigger.to_state.state == 'off' }}
          sequence:
          - service: switch.turn_on
            target:
              entity_id:
              - switch.l1
              - switch.l2
        - conditions: >
            {{ trigger.to_state.state == 'on' }}
          sequence:
          - service: switch.turn_off
            target:
              entity_id:
              - switch.l1
              - switch.l2

Is this correct behavior or bug?

See here: https://www.home-assistant.io/docs/automation/trigger/#state-trigger

State trigger
Fires when the state of any of given entities changes. If only entity_id is given, the trigger will fire for all state changes, even if only state attributes change. If at least one of from, to, not_from, or not_to are given, the trigger will fire on any matching state change, but not if only attributes change. To trigger on all state changes, but not on changed attributes, set at least one of from, to, not_from, or not_to to null.

You can try this:


  trigger:
  - platform: state
    entity_id: binary_sensor.xyz
    from:
    - 'on'
    - 'off'
    to:
    - 'on'
    - 'off'

  action:

  - service: switch.turn_{{ trigger.to_state.state }}
    target:
      entity_id: 
      - switch.xyz
      - switch.abc

1 Like