Recommendations on a method for periodic checking of state?

Here’s an example if you wanted it all in one automation:

description: "Light on when door is open, off when door is closed"
mode: single
triggers:
  - trigger: state
    entity_id: binary_sensor.door_contact
  - trigger: state
    entity_id: light.my_light
  - trigger: homeassistant
    event: start
conditions: []
actions:
  - choose:
      - conditions:
          - condition: state
            entity_id: binary_sensor.door_contact
            state: "on"
          - condition: state
            entity_id: light.my_light
            state: "off"
        sequence:
          - action: light.turn_on
            target:
              entity_id: light.my_light
      - conditions:
          - condition: state
            entity_id: binary_sensor.door_contact
            state: "off"
          - condition: state
            entity_id: light.my_light
            state: "on"
        sequence:
          - action: light.turn_off
            target:
              entity_id: light.my_light

In the choose: block, you don’t really need the conditions for the light because it shouldn’t hurt anything to send the “on” command to a light that is already on (or send “off” to a light that is “off”) but I put them there anyway.

That automation will fight you, so if the door is open and the light is on, and you manually turn it off, the automation will run and immediately turn it back on.

Now, if you wanted to achieve the same thing but using two separate automations, the “turn light on” automation would be what I suggested in my first post:

description: "Light on when door is open"
mode: single
triggers:
  - trigger: state
    entity_id: binary_sensor.door_contact
    state: "on"
  - trigger: state
    entity_id: light.my_light
    state: "off"
  - trigger: homeassistant
    event: start
conditions:
  - condition: state
    entity_id: binary_sensor.door_contact
    state: "on"
  - condition: state
    entity_id: light.my_light
    state: "off"
actions:
  - action: light.turn_on
    target:
      entity_id: light.my_light

And the “turn light off” automation is just the opposite. These two automations won’t interfere with each other due to the conditions.

1 Like