OR condition with nested ANDs

Hi all, I’m having trouble with this OR condition that has nested ANDs. Overall problem: Using camera motion detection to trigger a light between 10pm and sunrise, but when the automation runs and the light turns off after 2 minutes, the camera sensor changes back to “Detected” which re-triggers the automation over and over. I read best to use a “last state change” condition so the automation doesn’t run when the light has just turned off… giving the camera motion detector time to get back to “Clear”:

condition:
    condition: or
    conditions: 
        condition: and
        conditions: 
        - condition: time
        after: "22:00:00"
        - condition: template
        value_template: '{{ (as_timestamp(now()) - as_timestamp(states.light.entrance_porch_light.last_changed)) > 10 }}'
        condition: and
        conditions: 
        - condition: sun
        before: sunrise
        - condition: template
        value_template: '{{ (as_timestamp(now()) - as_timestamp(states.light.entrance_porch_light.last_changed)) > 10 }}'

I didn’t look too close into your code, but the way I’d go about it is this:
Automations can be configured to run in Single mode, so if the automation is still running it will not trigger again. So if you make sure the automation still runs, you’re safe:

In single mode: if the camera detects motion then turn light on, wait two minutes, turn light off, wait 10 more seconds. (to give the camera time to see the light turn off)

Another simple solution (maybe even better) would be to put in a condition that the light must have been off for at least 10 seconds in order for the automation to run. You put that duration in the condiotion for the light must be off.

I’ve got it running in restart. Thought this was necessary for motion triggered lights so the 2 minute on time re-commences while motion is continually detected?

Please post the full automation.
You speak of triggers but you don’t tell us what they are.

The code you shown is definitely invalid.

This should be the correct syntax based on your given code:

condition:
  - condition: or
    conditions: 
      - condition: and
        conditions: 
          - condition: time
            after: "22:00:00"
          - condition: template
            value_template: '{{ (as_timestamp(now()) - as_timestamp(states.light.entrance_porch_light.last_changed)) > 10 }}'
      - condition: and
        conditions: 
          - condition: sun
            before: sunrise
          - condition: template
            value_template: '{{ (as_timestamp(now()) - as_timestamp(states.light.entrance_porch_light.last_changed)) > 10 }}'

Restart is only needed if during the execution of the flow the trigger happens again, and you want it to abort the old and start a new. But note that continued motion is something else than a new trigger for motion (which implies that motion stopped in between). If you want continued motion to work as well you should use a timer (that you can stop), and start the timer when motion ends:

  1. If motion starts, and light is either on or has been off for at least 10 secs, and other conditions are met then turn on the light and abort the 2min timer if there is one running
  2. If motion stops, and the light is on, start a 2min timer
  3. If the 2min timer runs out, turn off the light.

What this says is: if you move the lights go on. If you stop moving you have 2 mins to start moving again, or else the light will go off. After that motion is ignored for 10 secs because the camera is fooled by the lights that go off.

The only problem is: if you keep moving within those 10 secs and after, the lights won’t go on again until motion is no longer detected and start moving again later. There’s no easy fix for that, other than to check for existing movement status on the camera a few seconds after the lights go off.

So maybe a €15 costing PIR might not be a bad investment :wink:

Some good advice already given. Just chiming in to say I’ve solved a similar issue. I just added a fairly simple template condition.

- alias: "Take Foyer Motion Snapshots"
  initial_state: true
  trigger:
    platform: state
    entity_id: binary_sensor.foyer_motion_homebridge
    to: "on"
  condition:
    - condition: state
      entity_id: binary_sensor.anybody_home
      state: "off"
    - condition: template
      value_template: >-
        {# prevent the action of the light turning off after the last trigger to trigger the motion sensor again immediately #}
        {{ (utcnow() - states.light.foyer.last_changed).seconds >= 1 }}
  action:
    - service: script.turn_on
      entity_id: script.security_camera_create_snapshot

Full code here, I fixed the formatting with the “last_triggered” but still re-triggers when the light turns off and camera flicks back to “Detected” for 5-10 seconds.

alias: Entrance Porch Light Sensor
description: ''
mode: restart
max_exceeded: silent
trigger:
  platform: state
  entity_id: binary_sensor.entrance_camera
  from: 'off'
  to: 'on'
condition:
  - condition: or
    conditions: 
      - condition: and
        conditions: 
          - condition: time
            after: "22:00:00"
          - condition: template
            value_template: '{{ (as_timestamp(now()) - as_timestamp(states.light.entrance_porch_light.last_changed)) > 10 }}'
      - condition: and
        conditions: 
          - condition: sun
            before: sunrise
          - condition: template
            value_template: '{{ (as_timestamp(now()) - as_timestamp(states.light.entrance_porch_light.last_changed)) > 10 }}'
action:
- service: light.turn_on
  target: {}
  data:
    target: light.entrance_porch_light
- wait_for_trigger:
    platform: state
    entity_id: binary_sensor.entrance_camera
    from: 'on'
    to: 'off'
- delay: 120
- service: light.turn_off
  data: {}
  target:
    entity_id: light.entrance_porch_light