Issue with Automation Condition

Hi,

Adding 2 very simple automations for office lights. One to turn them ON and one to turn them OFF. Turning on works fine, triggered by door open switch or motion detected.

However, I am struggling to get the OFF one to work. It is fine just using the motion not detected for X mins but ideally I wanted to AND this with door closed. I therefore added a condition that the door is closed but the automation does not turn off the lights at all. Am I right in thinking that as the door needs to be closed BEFORE the automation can run then the transition from detected to clear isn’t detected and therefore it fails? If so, what is the best way around it. I know I could just use a blueprint but want to try and learn YAML etc a bit.

Steve

Post your yaml if you want some help as its difficult to guess what you have tried. My guess though is that the door closing is not a condition but a trigger to turn off the light the condition is motion not detected and door closed.

1 Like

Try this……

alias: Office Lights Off Test
description: “”
triggers:

  • trigger: state
    entity_id:
    • binary_sensor.hue_motion_sensor_1_motion
      from: “on”
      to: “off”
      for:
      hours: 0
      minutes: 0
      seconds: 10
      conditions:
  • condition: state
    entity_id: binary_sensor.office_door_door
    state: “off”
    actions:
  • action: switch.turn_off
    metadata: {}
    data: {}
    target:
    entity_id: switch.shelly_1_plus_office_lights_switch_0
    mode: single

the door needs to be closed before the automation is triggered by the motion sensor.
a way to use the door as a trigger is to add a wait_for_trigger in your actions.

How do I achieve this?

Conditions are not Waits. Conditions check the status at that moment and either pass or fail. If you need to track a sequence of events, you need to use either a Wait for Trigger or Wait template.

If the door may or may not be closed within your 10 seconds of no motion, use a Wait template. If the door closing should always happen after the motion has been stopped for 10 seconds, use a Wait for Trigger.

something like this:

alias: Office Lights Off Test
description: ""
triggers:
  - trigger: state
    entity_id:
      - binary_sensor.hue_motion_sensor_1_motion
    from: "on"
    to: "off"
    for:
      hours: 0
      minutes: 0
      seconds: 10
conditions: []
actions:
  - if:
      - condition: state
        entity_id: binary_sensor.office_door_door
        state: "on"
    then:
      - wait_for_trigger:
          - trigger: state
            entity_id:
              - binary_sensor.office_door_door
            from: "on"
            to: "off"
      - action: switch.turn_off
        metadata: {}
        data: {}
        target:
        entity_id: switch.shelly_1_plus_office_lights_switch_0
    else:
      - action: switch.turn_off
        metadata: {}
        data: {}
        target:
        entity_id: switch.shelly_1_plus_office_lights_switch_0
mode: single

Waiting for long periods of time in an automation is not best practice. The light may stay on if the automation is interrupted by a restart or reload.

Here’s a simpler method that just triggers on the door closing as well:

alias: Office Lights Off Test
description: ""
triggers:
  - trigger: state
    entity_id: binary_sensor.hue_motion_sensor_1_motion
    from: "on"
    to: "off"
    for:
      hours: 0
      minutes: 0
      seconds: 10
  - trigger: state
    entity_id: binary_sensor.office_door_door
    from: "on"
    to: "off"
conditions:
  - condition: state
    entity_id: binary_sensor.office_door_door
    state: "off"
actions:
  - action: switch.turn_off
    metadata: {}
    data: {}
    target:
      entity_id: switch.shelly_1_plus_office_lights_switch_0
mode: single

So this will turn off the light if there has been no movement for 10 seconds and the door is closed.

It will also turn off the lights when the door is closed.

Hi,

Understand both bits of code above and understand having a long delay in an Automation can be problematic. However, to expand on the requirement……

It is an office, so on entering the lights will turn on based on the door opening OR motion being detected. I would then close the door (air con on), keeping the lights on. I could be potentially in there for hours working with the lights on. At the end of the day I would require the lights to switch off based on no movement and door closed.

Then use Krivatri’s suggestion.

you could also combine the two automations (on and off).
in this automation, the lights turn on when the door or the motion sensor changes state.
after 10 seconds it will check if there is still someone in the room to keep the lights on, if not they turn off.
notice the mode is “restart” wich means the automation will restart when a change is detected, the 10 second wait is then reset.

alias: Office Lights
description: ""
triggers:
  - trigger: state
    entity_id:
      - binary_sensor.hue_motion_sensor_1_motion
  - trigger: state
    entity_id:
      - binary_sensor.office_door_door
conditions: []
actions:
  - action: switch.turn_on
    target:
      entity_id: switch.shelly_1_plus_office_lights_switch_0
    data: {}
  - delay:
      hours: 0
      minutes: 0
      seconds: 10
      milliseconds: 0
  - if:
      - condition: state
        entity_id: binary_sensor.hue_motion_sensor_1_motion
        state: "on"
    then:
      - action: switch.turn_on
        target:
          entity_id: switch.shelly_1_plus_office_lights_switch_0
        data: {}
    else:
      - action: switch.turn_off
        target:
          entity_id: switch.shelly_1_plus_office_lights_switch_0
        data: {}
mode: restart

Yep, this solved the issue.

1 Like

Thanks guys for all your help on this issue, learnt a few new techniques which I will now put to good use in other automations I have.

Steve