Help with setting time condition on multiple entities

I’m trying to create an alarm automation where the alarm will arm when ALL four entities have been off for 20 minutes, not just one to ensure that no-one is still downstairs i.e. when the last entity is off for 20 minutes it checks the other 3 have been off for at least 20 minutes.

I can’t get it to work and I’m guessing I’m caught in a loop where my ‘OR’ isn’t working properly or something. Help please

- alias: 'Good Night'
  trigger:
        platform: state
        entity_id: input_boolean.media_player_living_room, input_boolean.media_player_play_room, group.motion_downstairs, group.living_room_lights
        to: 'off'
        for:
            minutes: 20
  condition:
        condition: and
        conditions:
          - condition: time
            after: '00:00:00'
            before: '05:00:00'
            weekday:
            - sun
            - mon
            - tue
            - wed
            - thu
            - fri
            - sat
          - condition: state
            entity_id: 'input_boolean.media_player_play_room'
            state: 'off'
            for:
                minutes: 20
          - condition: state
            entity_id: 'input_boolean.media_player_living_room'
            state: 'off'
            for:
                minutes: 20
          - condition: state
            entity_id: group.motion_downstairs
            state: 'off'
            for:
                minutes: 20
          - condition: state
            entity_id: group.living_room_lights
            state: 'off'
            for:
                 minutes: 20
  action:
      - service: alarm_control_panel.alarm_arm_home
        entity_id: alarm_control_panel.house

a few pointer to help you simplify your automation:

  • conditions are AND by default, so you could remove the lines
        condition: and
        conditions:

and bring your indents back
if you want your automation to run every day, you don’t need to list all days in weekday

For your actual issue, I would group your entities and trigger off the group.

1 Like

Thanks for the pointers.

Is there any way to group the entities so that the group is only ‘on’ when all are 'on and vice versa, rather than when just one entity is ‘on’?

Edit: I’m trying to avoid a scenario where the lights have been off for 20 mins as there’s been no motion, but someone is watching tv in the dark and is just sitting very still or is watching tv in the playroom where there aren’t any automated lights

a group will be on if at least one device is on and will only be off if all entities are off.
Similarly, a group of device trackers will be home if all device trackers are home, else it’ll be not_home if at least 1 device tracker is out of the house

that’s exactly where I’m stuck - how do I check if all are OFF or ON?

add them all to 1 group. Group state will be on if at least one entity is on, else it’ll be off
check how you created group.motion_downstairs and create a new group:

group:
  good_night_group:
    name: Good Night Group
    entities:
      - input_boolean.media_player_living_room
      - input_boolean.media_player_play_room
      - group.motion_downstairs
      - group.living_room_lights
1 Like

duh - i get it. I need to reverse my thinking and if the group has been off for 20 mins, then they’ve all been off.

Thanks

1 Like