Automation Not Running When ReTriggered

I’m pulling my hair out trying to figure out the issue here. I have the below two automations for turning on and off my kitchen lights. The lights turn on in the first instance, then turn off as expected if no motion sensed within 1 minute. However, the lights won’t turn on again if the motion sensor is re-triggered within about 5-10 seconds after the lights turning off. After that they will turn on.

I’ve tried changing the light effect call to just kelvin and calling scripts but it’s the same each time.

They are Philips Wiz bulbs connected to a Sonoff light switch, set to always on.

Can anyone see an issue with either the turn on or turn off automation that would cause this behaviour? Or could it be device specific?

- alias: Kitchen Ceiling Lights On Motion
  id: kitchen_ceiling_lights_on_motion
  mode: single
  trigger:
    - platform: state
      entity_id: binary_sensor.kitchen_motion_sensor_motion
      to: 'on'
  condition:
    - condition: template
      value_template: "{{ states('binary_sensor.kitchen_motion_sensor_motion') not in ['unknown', 'unavailable'] }}"
    - condition: template
      value_template: "{{ states('sensor.kitchen_motion_sensor_illuminance')|float(100) < 12 }}"
  action:
    - choose:
        - conditions:
            - condition: time
              after: '05:00:01'
              before: '07:30:00'
          sequence:
            - service: light.turn_on
              target:
                entity_id: light.kitchen_ceiling_lights
              data:
                effect: "Warm white"

        - conditions:
            - condition: time
              after: '07:30:01'
              before: '23:00:00'
          sequence:
            - service: light.turn_on
              target:
                entity_id: light.kitchen_ceiling_lights
              data:
                effect: "Golden white"

     
        - conditions:
            - condition: or
              conditions:
                - condition: time
                  after: '23:00:01'
                - condition: time
                  before: '05:00:00'
          sequence:
            - service: light.turn_on
              target:
                entity_id: light.kitchen_ceiling_lights
              data:
                effect: "Night light"

Have you tried changing the mode?

Some motion sensors won’t detect motion again for a while after they stop detecting motion. Does the history of the sensor show that it was detected within the 15 seconds after it cleared?

I tried all four modes with no luck.

Yes, I’ve created a card in a dashboard with just the relevant entities and I can see that all entities are on/off as expected. The Hue motion sensor takes just around 3 seconds to clear after I have left the room but no more than that.

Where is the off automation?

Have you trie looking at the automation traces?

1 Like

This is the off automation:

- alias: Kitchen Ceiling Lights Off Motion
  id: kitchen_ceiling_lights_off_motion
  mode: single
  trigger:
    platform: state
    entity_id: binary_sensor.kitchen_motion_sensor_motion
    to: 'off'
    for:
      minutes: 1
  action:
    - service: light.turn_off
      target:
        entity_id: light.kitchen_ceiling_lights

I’ve checked traces for when it fails to re-turn on the lights and get the below:

*EDIT: I tried commenting out the value_template: "{{ trigger.from_state.state not in ['unknown', 'unavailable'] }}" but it still failed. So it must relate to the only other global condition which is illuminance level. Maybe that’s not updating quickly enough??

Thanks to @tom_l for the checking traces idea.

This has helped me narrow it down to the illuminance sensor state not updating quickly enough. It seems to take 5-10 seconds to update.

Not sure, though, how I can handle this in the automation.

Make life a little easier; one automation that handles both on and off and resolves that lag.

- alias: Kitchen Ceiling Lights Motion Control
  id: kitchen_ceiling_lights_motion_control
  mode: restart
  trigger:
    - platform: state
      entity_id: binary_sensor.kitchen_motion_sensor_motion
      to: 'on'
    - platform: state
      entity_id: binary_sensor.kitchen_motion_sensor_motion
      to: 'off'
      for:
        minutes: 1
  condition:
    - condition: template
      value_template: "{{ states('binary_sensor.kitchen_motion_sensor_motion') not in ['unknown', 'unavailable'] }}"
  action:
    - choose:
        - conditions:
            - condition: state
              entity_id: binary_sensor.kitchen_motion_sensor_motion
              state: 'on'
            - condition: template
              value_template: "{{ states('sensor.kitchen_motion_sensor_illuminance')|float(100) < 12 }}"
          sequence:
            - choose:
                - conditions:
                    - condition: time
                      after: '05:00:01'
                      before: '07:30:00'
                  sequence:
                    - service: light.turn_on
                      target:
                        entity_id: light.kitchen_ceiling_lights
                      data:
                        effect: "Warm white"
                - conditions:
                    - condition: time
                      after: '07:30:01'
                      before: '23:00:00'
                  sequence:
                    - service: light.turn_on
                      target:
                        entity_id: light.kitchen_ceiling_lights
                      data:
                        effect: "Golden white"
                - conditions:
                    - condition: or
                      conditions:
                        - condition: time
                          after: '23:00:01'
                        - condition: time
                          before: '05:00:00'
                  sequence:
                    - service: light.turn_on
                      target:
                        entity_id: light.kitchen_ceiling_lights
                      data:
                        effect: "Night light"
        - conditions:
            - condition: state
              entity_id: binary_sensor.kitchen_motion_sensor_motion
              state: 'off'
          sequence:
            - service: light.turn_off
              target:
                entity_id: light.kitchen_ceiling_lights

This approach eliminates the inter-automation timing issues and the illuminance sensor lag problem since motion detection triggers immediately while illuminance only gates the turn-on logic.​​​​​​​​​​​​​​​​

I tried your automation and, unfortunately, it hasn’t resolved the issue. The lights still didn’t trigger back on.

try this one, it by passes the illuminance check for a brief period after teh lights go off:

- alias: Kitchen Ceiling Lights Motion Control
  id: kitchen_ceiling_lights_motion_control
  mode: restart
  trigger:
    - platform: state
      entity_id: binary_sensor.kitchen_motion_sensor_motion
      to: 'on'
    - platform: state
      entity_id: binary_sensor.kitchen_motion_sensor_motion
      to: 'off'
      for:
        minutes: 1
  condition:
    - condition: template
      value_template: "{{ states('binary_sensor.kitchen_motion_sensor_motion') not in ['unknown', 'unavailable'] }}"
  action:
    - choose:
        - conditions:
            - condition: state
              entity_id: binary_sensor.kitchen_motion_sensor_motion
              state: 'on'
            - condition: or
              conditions:
                - condition: template
                  value_template: "{{ states('sensor.kitchen_motion_sensor_illuminance')|float(100) < 12 }}"
                - condition: template
                  value_template: "{{ (now() - states.light.kitchen_ceiling_lights.last_changed).total_seconds() < 15 }}"
          sequence:
            # your time-based light sequences here
        - conditions:
            - condition: state
              entity_id: binary_sensor.kitchen_motion_sensor_motion
              state: 'off'
          sequence:
            - service: light.turn_off
              target:
                entity_id: light.kitchen_ceiling_lights

Post the history of that entity around the time of your traces