Condition template not working even if in dev tools shows true

Hi,

I have the below automation which is not triggering. I’ve checked the condition template in dev tools and the output is true, but still the automation doesn’t do what it supposed to do. Would anyone be able to provide me a suggestion where I have the problem/mistake.

  - alias: 'Motion Sensor Landing after Midnight - Lights Off'
    initial_state: 'on'
    trigger:
      - platform: state
        entity_id: binary_sensor.hue_motion_sensor_1_motion, binary_sensor.0x00158d0001d632fa_occupancy
        to: 'off'
    condition:
      - condition: state
        entity_id: light.landing_lights
        state: 'on'  
      - condition: time
        after: '00:00:00'
      - condition: sun
        before: sunrise
      - condition: template
        value_template: '{{ as_timestamp(now()) - as_timestamp(states.automation.motion_sensor_landing_after_midnight_lights_on.attributes.last_triggered) | int > 300}}'
    action:
      - service: homeassistant.turn_off
        entity_id: light.landing_lights

Thank you

Please format your code properly. Use three backticks ( ``` ) above and below the code.

You are calculating whether the lights were turned on at least 300 seconds ago as a condition.

This trigger will only fire, when one of those two sensors goes from to off. If motion is only detected before that 5 minutes, and then not again after it, the trigger will not fire, and your lights will not go off. This means the lights can stay on infinitely, if no motion is detected, to produce you ‘off’ event trigger.

If you intend for the lights to go off after 5 minutes, you should use a timer to count down, and then have the motion sensor to: ‘on’ state reset the timer back to five minutes.

As a side note, you should probably include motion sensor state from:on as without it, this allows the sensor to disconnect and go from unavailable to off, even if its really off->off.

Thanks @callifo. indeed the problem I have is in regards to the trigger statement which is not ok. I will try to think so some other trigger.

I’ll try this for trigger and see where it gets me.

platform: template 
   value_template: "{{ is_state('binary_sensor.hue_motion_sensor_1_motion', 'off') and is_state('binary_sensor.0x00158d0001d632fa_occupancy', 'off') }}"

I managed to get to an end with this thanks to Rod Payne’s script which can be used to change the state(see the script here if interested - https://github.com/rodpayne/home-assistant/blob/3e83b42c5a1e232dfe7955ec2fd71f3f2aedf036/python_scripts/set_state.py).

Then, I have 2 automations is place:
1st - at midnight I change the scene on landing if the landing lights are on and I change the state of the motion sensors for landing - on/off. This “activates” my 2nd automation.

  • 1st automation
  - alias: 'Midnight scene automation'
    initial_state: 'on'
    trigger:
      platform: time
      at: '00:00:00'
    condition:
      - condition: state
        entity_id: light.landing_lights
        state: 'on'  
    action:
      - service: script.turn_on
        data_template:
          entity_id: script.2spots_on
      - service: python_script.set_state
        data_template:
          entity_id: binary_sensor.0x00158d0001d632fa_occupancy
          state: 'on'
      - service: python_script.set_state
        data_template:
          entity_id: binary_sensor.hue_motion_sensor_1_motion
          state: 'on'       
     # No need to set the state of the hue motion sensor back to off as it goes by default
      - delay:
          seconds: 1
      - service: python_script.set_state
        data:
          entity_id: binary_sensor.0x00158d0001d632fa_occupancy
          state: 'off'  
  • 2nd automation
  - alias: 'Motion Sensor Landing after Midnight - Lights Off'
    initial_state: 'on'
    trigger:
      - platform: state
        entity_id: binary_sensor.hue_motion_sensor_1_motion, binary_sensor.0x00158d0001d632fa_occupancy
        to: 'off'
        for:
          minutes: 5
    condition:
      - condition: state
        entity_id: light.landing_lights
        state: 'on'  
      - condition: time
        after: '00:00:00'
      - condition: sun
        before: sunrise
    action:
      - service: homeassistant.turn_off
        entity_id: light.landing_lights

I hope it can help someone in case they have similar issues as I had.