Why won't this trigger.to_state.state evaluate correctly?

Continuing the discussion from If statement in automation to determine triggers "for:" time:

HI,
Using this automation:

  - alias: 'Living lights off when no motion'
    id: 'Living lights off when no motion'
#    initial_state: 'on'
    trigger:
     - platform: state
       entity_id: binary_sensor.auditorium_motion_sensor
       to: 'on'
     - platform: state
       entity_id: binary_sensor.auditorium_motion_sensor
       to: 'off'
    condition:
      condition: template
      value_template: >
        {{ trigger.to_state.state in ['on','off'] }}
    action:
      - service: script.turn_off
        entity_id: script.switch_off_lights_delay
      - condition: template
        value_template: >
          {{ is_state('binary_sensor.auditorium_motion_sensor', 'off') }}
#          {{ trigger.to_state.state == 'off' }}
      - service: script.switch_off_lights_delay

works perfectly if I use the conditional value_template in the action part. Trying to make it more generic, I replaced it with the now commented {{ trigger.to_state.state == 'off' }} but that simple won’t work. Why would that be? @petro @pnbruckner do you spot the error ?
Its all the more strange since the condition in the condition part uses the same trigger, which works just as expected.

Appreciate any help/hint.
thanks!

what’s not working about it? I don’t see why it wouldn’t work.

exactly. But it simply doesn’t fire the action. No error, nothing anywhere. simply mute… that’s just it

well, first off, the whole automation seems… over complicated. I would expect the same result from this automation:

  - alias: 'Living lights off when no motion'
    id: 'Living lights off when no motion'
#    initial_state: 'on'
    trigger:
      - platform: state
        entity_id: binary_sensor.auditorium_motion_sensor
    action:
      - service: script.turn_off
        entity_id: script.switch_off_lights_delay
      - condition: template
        value_template: >
          {{ trigger.to_state.state == 'off' }}
      - service: script.switch_off_lights_delay

What is your end goal here btw?

that’s what I had before, but inthis post, under the EDIT__UPDATE is explained why it had to be ‘complicated’…

the motion sensor has many attributes that trigger the automation unwantedly

this has become a study in how to switch off lights when motion has stopped after a humidity dependable delay/switch off time… and build in a restart if motion is noticed during that delay.

all documented in the linked thread

how so? The constraints you are adding aren’t really doing anything unless the state is changing to something other than on/off

its the same as with presence automation based on state change, where the devices battery and gps coordinates also cause the state to change, while the state.state is the same…
Really, I’ve tested extensively: without these constraints the motion_sensor triggers the automation on each light_level change and several other attributes:

39

it does so, even when the motion_sensor itself is switched off.

ah ok, you still shouldn’t need the condition if your triggers only look for on or off.

for giggles. Make a notification in the action section that sends you the trigger.to_state.state.

works just fine…

  - alias: 'Living lights off when no motion'
    id: 'Living lights off when no motion'
#    initial_state: 'on'
    trigger:
     - platform: state
       entity_id: binary_sensor.auditorium_motion_sensor
       to: 'on'
     - platform: state
       entity_id: binary_sensor.auditorium_motion_sensor
       to: 'off'
    condition:
      condition: template
      value_template: >
        {{ trigger.to_state.state in ['on','off'] }}
    action:
      - service: notify.notify
        data_template:
          message: >
            {{ trigger.to_state.state }}
      - service: script.turn_off
        entity_id: script.switch_off_lights_delay
      - condition: template
        value_template: >
          {{ is_state('binary_sensor.auditorium_motion_sensor', 'off') }}
#          {{ trigger.to_state.state == 'off' }}
      - service: script.switch_off_lights_delay
      - service: notify.notify
        data_template:
          message: >
            Again: {{ trigger.to_state.state }}

give me an ‘on’ on motion detection, and then nothing, which is correct. when motion detection switches to off, I receive an ‘off’, followed by an Again: off, indicating the script has been called.

Ok, uncomment {{ trigger.to_state.state == ‘off’ }} and try again.

I did, and after an initial state of not showing the script as on (on the all_scripts page)
it suddenly started to do so.

took out the notify of the automation and added to the script:

  switch_off_lights_delay:
    alias: Switch off lights with delay
    sequence:
      - service: notify.notify
        data:
          message: >
            turning on delay
      - delay:
          minutes: >
            {{ states('input_number.lighting_timer' )|int }}
      - service: light.turn_off
        entity_id: light.living
      - service: notify.notify
        data:
          message: >
            lights turned off after delay

now gives me 2 sweet notifications! and the lights switch Off. A miracle…

A similar thing happened to me today. I had an automation with conditions based on trigger.to_state.state that never triggers. I simply modified the automation adding an action outside the conditions and then deleted the action and now it works