Switch automation not working as intended

I’m trying to have a noisy air purifier that I keep in my office turn on and off automatically. The YAML below is what I’m using and it works to turn the air purifier off but not to come back on again. The issue seems to be the two minute delay. I want the air purifier to turn off immediately when it detects a presence, but not turn on unless presence is clear for 2 minutes. This is because occasionally it will detect presence is clear when I’m still sitting there, so the delay is to prevent those reactions to false reads. I have tried messing with the mode but that didn’t seem to help.

alias: Office Air
description: >-
  Turn on the office air purifier if presence is detected and off if no presence
  is detected.
triggers:
  - trigger: state
    entity_id:
      - binary_sensor.office_presence_sensor_occupancy
  - trigger: state
    entity_id:
      - binary_sensor.tv_couch_presence_occupancy
conditions: []
actions:
  - if:
      - condition: or
        conditions:
          - condition: state
            entity_id: binary_sensor.tv_couch_presence_occupancy
            state: "on"
          - condition: state
            entity_id: binary_sensor.office_presence_sensor_occupancy
            state: "on"
    then:
      - type: turn_off
        device_id: 9686c92b85e2a03e4b0352ca937bc313
        entity_id: e8571243bf1b32e903aaf7ef8fcafaf8
        domain: switch
  - if:
      - condition: and
        conditions:
          - condition: state
            entity_id: binary_sensor.tv_couch_presence_occupancy
            state: "off"
            for:
              hours: 0
              minutes: 2
              seconds: 0
          - condition: state
            entity_id: binary_sensor.office_presence_sensor_occupancy
            state: "off"
            for:
              hours: 0
              minutes: 2
              seconds: 0
    then:
      - type: turn_on
        device_id: 9686c92b85e2a03e4b0352ca937bc313
        entity_id: e8571243bf1b32e903aaf7ef8fcafaf8
        domain: switch
mode: queued
max: 10

Your automation doesn’t work because there’s no trigger that then satisfies the “turn-on” test of both sensors having been “off” for 2 minutes. Sequence is:

  • sensor goes off
  • automation triggered
  • first if statement (either sensor on) is false, skip that
  • second if statement (both sensors off for two minutes) is false, skip that
  • wait for the next sensor change

You could add an additional trigger that fires when the sensors have been off for two minutes, but here’s how I’d do it, using an entity action rather than the horrible device action, you’ll need to fill in the switch’s entity ID (switch.something).

Triggers off either sensor detecting motion, and either sensor being clear for two minutes.

The condition blocks checks if we’ve detected motion, or that both sensors have been clear for at least two minutes. I’ve written it with shorthand template conditions, but you could use an OR block containing a trigger condition and an AND block containing two state conditions as per the ones in your second if statement.

The action block runs the appropriate action. (EDIT: got the actions the wrong way around, now fixed!)

alias: Office Air
description: >-
  Turn on the office air purifier if presence is detected and off if no presence
  is detected.
triggers:
  - trigger: state
    entity_id:
      - binary_sensor.office_presence_sensor_occupancy
      - binary_sensor.tv_couch_presence_occupancy
    to: "on"
    id: "off"
  - trigger: state
    entity_id:
      - binary_sensor.office_presence_sensor_occupancy
      - binary_sensor.tv_couch_presence_occupancy
    to: "off"
    for: "00:02:00"
    id: "on"
conditions:
  - or:
    - "{{ trigger.id == 'off' }}"
    - "{{ (now()-(states['binary_sensor.office_presence_sensor_occupancy']['last_updated'],states['binary_sensor.tv_couch_presence_occupancy']['last_updated'])|max).total_seconds() >= 120 }}"
actions:
  - action: switch.turn_{{ trigger.id }}
    target:
      entity_id: switch.ENTITY_ID

That’s how I prefer to write automations, with as much logic as possible in the condition block and a very straightforward action block.

1 Like