Automation only works the first time

I have an automation in the kitchen which was working but stopped a while ago when I changed the sensors.

Essentially i am looking for the automation to do the following:

After 19.30 and before 00:30 if the kitchen lights are on for 2 mins and no motion is detected on the Kitchen or Dining Room sensor, all the lights should turn off. I have another automation which turns the lights on during this time if motion is detected so I want this to continually turn on and off during this time on trigger of motion or no motion.

  alias: Kitchen No Motion Evening (New v2)
  description: Kitchen lights off after 2 mins of no motion.
  triggers:
  - trigger: state
    entity_id:
    - light.kitchen_lights
    to: 'on'
    for:
      hours: 0
      minutes: 2
      seconds: 0
  conditions:
  - type: is_not_occupied
    condition: device
    device_id: 8432837d7f12663595646277dc57998f
    entity_id: 32628c877b79a87244899865bb118f25
    domain: binary_sensor
    for:
      hours: 0
      minutes: 2
      seconds: 0
  - type: is_not_occupied
    condition: device
    device_id: 6702be4b7e92333bc5d69263f076b33b
    entity_id: 9b747b5e633a85874d51ebd982b4f29a
    domain: binary_sensor
    for:
      hours: 0
      minutes: 2
      seconds: 0
  - condition: time
    after: '19:30:00'
    before: 00:30:00
    weekday:
    - sun
    - sat
    - fri
    - thu
    - wed
    - tue
    - mon
  - condition: state
    entity_id: input_boolean.guest_mode
    state: 'off'
  - condition: state
    entity_id: input_boolean.flip_mode
    state: 'off'
  actions:
  - type: turn_off
    device_id: 63100c864650976e5ee028cdcdb7d120
    entity_id: 3af9b50cacdbdf8508f0939b26fb0448
    domain: light
  - type: turn_off
    device_id: 27a82779aa61114da85fad319f3ee2d9
    entity_id: 2d5ede666d72ed8dd8e5e21fc95c3721
    domain: light
  - type: turn_off
    device_id: 0fc582e96373c45d0941ab64d28833bd
    entity_id: d685ae9c38cb89ee90e12d56407c2508
    domain: light
  - type: turn_off
    device_id: efb3a51e4350ef7461fc928602c8cc30
    entity_id: 8ffbdc04b9af3785364797f837ad4694
    domain: light
  mode: single
type or paste code here

The lights seem to turn off the first time it is triggered but then after the first time it will never turn off again.

What am i doing wrong?

Your automation logic seems quite flawed. The trigger will only fire once, when the light has been on for two minutes, and if at that particular time there is no motion the lights will turn off. If there is motion however, the lights will never turn off, as the trigger will never fire again.

You are testing the state of the group. What is the state of the group after turning the lights off? Is it what is expected?

Ah - good spotting…

Thanks. Would this work?

- id: '1739227153614'
  alias: Kitchen No Motion Evening (New v2)
  description: Kitchen lights off after 2 mins of no motion.
  trigger:
    - platform: state
      entity_id: light.kitchen_lights
      to: 'on'
      for:
        minutes: 2
  condition:
    - condition: state
      entity_id: binary_sensor.kitchen_aqara_motion
      state: 'off'
    - condition: state
      entity_id: binary_sensor.dining_room_motion
      state: 'off'
    - condition: time
      after: '19:30:00'
      before: '00:30:00'
    - condition: state
      entity_id: input_boolean.guest_mode
      state: 'off'
    - condition: state
      entity_id: input_boolean.flip_mode
      state: 'off'
  action:
    - service: light.turn_off
      entity_id:
        - light.kitchen_lights
        - light.dining_room_lights
        - light.living_room_lights
        - light.hallway_lights
  mode: restart

I don’t see any difference where it matters. Why trigger when the light has been on a certain time? That makes zero sense to me. The only thing that should matter is how long it’s been since any motion was detected. I would replace the triggers with this:

triggers:
  - trigger: state
    entity_id:
      - binary_sensor.kitchen_aqara_motion
      - binary_sensor.dining_room_motion
    to: "off"
    for:
      minutes: 2

Keep all of the conditions as is, since you only want the actions to run when all motion sensors are off. Though you can combine all of the state conditions into one, as they are all required to have the same state (off).

conditions:
  - condition: state
    entity_id:
      - binary_sensor.kitchen_aqara_motion
      - binary_sensor.dining_room_motion
      - input_boolean.guest_mode
      - input_boolean.flip_mode
    state: "off"
  - condition: time
    after: "19:30:00"
    before: "00:30:00"

Edit: Or if you want to make sure that all motion detectors really have been off for those 2 minutes, and not trigger when one of them has been off for 2 minutes and the other may only have been off for a few seconds, split the conditions up into two and add a for parameter:

conditions:
  - condition: state
    entity_id:
      - binary_sensor.kitchen_aqara_motion
      - binary_sensor.dining_room_motion
    state: "off"
    for:
      minutes: 2
  - condition: state
    entity_id:
      - input_boolean.guest_mode
      - input_boolean.flip_mode
    state: "off"
  - condition: time
    after: "19:30:00"
    before: "00:30:00"
1 Like

Ah I understand now! Replacing the trigger with the sensors. Apologies.

Let me try and see how that goes.

Why not group these sensors in a helper so they act as one if you want to execute an action when no motion has been recorded for 2 minutes?

1 Like

Good call, just set one up to refer to. Thanks for that idea.

Am at work so will get this amended and tested tonight.

How does this look?

- id: '1739485877976'
  alias: Kitchen Lights off with no motion (v3)
  description: Kitchen lights off after 2 mins of no motion.
  triggers:
  - trigger: state
    entity_id:
    - binary_sensor.kitchen_dining_sensors
    to: 'off'
    for:
      hours: 0
      minutes: 2
      seconds: 0
  conditions:
  - condition: time
    after: '19:30:00'
    before: 00:30:00
    weekday:
    - sun
    - sat
    - fri
    - thu
    - wed
    - tue
    - mon
  - condition: state
    entity_id: input_boolean.guest_mode
    state: 'off'
  - condition: state
    entity_id: input_boolean.flip_mode
    state: 'off'
  - condition: state
    entity_id: light.kitchen_lights
    state: 'on'
  - condition: state
    entity_id: binary_sensor.0x00158d000af2c00f_occupancy
    state: 'off'
    for:
      hours: 0
      minutes: 2
      seconds: 0
  - condition: state
    entity_id: binary_sensor.0x00158d000af2aae4_occupancy
    state: 'off'
    for:
      hours: 0
      minutes: 2
      seconds: 0
  actions:
  - type: turn_off
    device_id: 63100c864650976e5ee028cdcdb7d120
    entity_id: 3af9b50cacdbdf8508f0939b26fb0448
    domain: light
  - type: turn_off
    device_id: 27a82779aa61114da85fad319f3ee2d9
    entity_id: 2d5ede666d72ed8dd8e5e21fc95c3721
    domain: light
  - type: turn_off
    device_id: 0fc582e96373c45d0941ab64d28833bd
    entity_id: d685ae9c38cb89ee90e12d56407c2508
    domain: light
  - type: turn_off
    device_id: efb3a51e4350ef7461fc928602c8cc30
    entity_id: 8ffbdc04b9af3785364797f837ad4694
    domain: light
  mode: single

All sorted now as I am back from my holiday. I think I may add a condition for it only to come on when its under a certain light level.

Thanks for your help.