Motion activated lights automation problem after HA reboot

Almost every time I restart or reboot HA I have a problem with my Motion activated lights automation. Usually lamp is always on after reboot and automation no longer works.

To fix it I have to disable and enable automation, sometime several times. Or physically turn off/on lamp.

I have aqara zigbee motion sensor and aqara zigbee lamp.
The automation looks like this:

  1. When motion sensor started detecting motion
  2. Then do
  • activate lamp scene (turn on lamp)
  • wait for 1 trigger: When motion-sensor changes from any state to Clear for 30 seconds
  1. Turn off lamp

Maybe it’s happening because automation was running when HA was restarted?
Any way to fix it?

Different ways to fix it.

  • condition not to run if HA just started
  • don’t trigger if State goes from unavailable to on
    You need both
2 Likes

Hi,

Please post the automation .yaml

alias: Aqara-motion-sensor automation
description: ""
triggers:
  - type: motion
    device_id: <id>
    entity_id: binary_sensor.lumi_lumi_sensor_motion_aq2_motion
    domain: binary_sensor
    trigger: device
conditions: []
actions:
  - target:
      entity_id: scene.aqara_lamp_25_cool
    metadata: {}
    action: scene.turn_on
    data: {}
  - wait_for_trigger:
      - entity_id:
          - binary_sensor.lumi_lumi_sensor_motion_aq2_motion
        to: "off"
        for:
          hours: 0
          minutes: 0
          seconds: 30
        trigger: state
        from: null
    enabled: true
    continue_on_timeout: false
  - type: turn_off
    device_id: <id>
    entity_id: light.lumi_lumi_light_aqcn02_light
    domain: light
    enabled: true
mode: single

Hello dumbdevice,

Device triggers are awful. Try an entity state trigger using

trigger:
  - platform: state
    entity_id:
      - sensor.some_sensor
    not_from: ["unavailable", "unknown"]
    not_to: ["unavailable", "unknown"]
2 Likes

How to add these 2 conditions in visual UI?

Hi! Where I should put it? Below trigger: state in my automation?

here I guess:

triggers:
  - type: motion
    device_id: <id>
    entity_id: binary_sensor.lumi_lumi_sensor_motion_aq2_motion
    domain: binary_sensor
    trigger: device

Yes, its a trigger :slight_smile:

ok I changed first trigger to:

Visual editor is not happy, but looks like automation still works!
I would never figure out to add not from and not to there.
It’s only possible in yaml mode?

I rebooted HA when automation was running and lamp was on and never goes off. I activated automation with motion sensor and it’s still running.

It stuck at waiting for 1 trigger:

  - wait_for_trigger:
      - entity_id:
          - binary_sensor.lumi_lumi_sensor_motion_aq2_motion
        to: "off"
        for:
          hours: 0
          minutes: 0
          seconds: 30
        trigger: state
        from: null

update: after some time it recovered itself


I wonder what if create 2 automations: first automation to turn lamp on trigger and second automation to turn off lamp :thinking:

I see the problem is aqara motion sensor, it stuck in Motion: Detected state after HA restart for about 10 minutes that’s why my automation doesn’t work.

I did another test, I rebooted HA when state was Motion: Clear and after reboot it worked right away!

Is there a way to set or reset item state on boot maybe?


Update 2: I set python_script integration and now can set state for motion sensor when HA starts.

I also now have two automations: first automation to turn lamp on trigger and second automation to turn off lamp.

The reason I need a second automation because I need to constantly monitor duration of Clear state, without dealing with states transition (from detected to clear).

alias: turn off lamp
description: ""
triggers:
  - type: no_motion
    device_id: <id_here>
    entity_id: <id_here>
    domain: binary_sensor
    trigger: device
    for:
      hours: 0
      minutes: 0
      seconds: 10
conditions: []
actions:
  - type: turn_off
    device_id: <id_here>
    entity_id: <id_here>
    domain: light
    enabled: true
mode: single

This trigger type: no_motion should turn off lamp after HA restart if its on.
In my testing trigger based on state changes doesn’t work for some reason.


Update 3: I rebooted HA and lamp was on and sensor state was clear. My automation failed.

I guess I need to create automation to turn off lamp when HA starts and go back to my original automation…

Had a look at the cookbook?

triggers:
  - trigger: state
    entity_id: binary_sensor.lumi_lumi_sensor_motion_aq2_motion
    to: 'on'
  - trigger: state
    entity_id: binary_sensor.lumi_lumi_sensor_motion_aq2_motion
    to: 'off'
    for:
      seconds: 30
actions:
  - action: "light.turn_{{ trigger.to_state.state }}"
    target:
      entity_id: light.your_light_here # change this

Motion activated lights automation - Community Guides - Home Assistant Community

And also:
Why and how to avoid device_ids in automations and scripts - Community Guides - Home Assistant Community

If you require a scene, you could use the ‘choose’ funtion to differentiate between on/off trgiggers In the end it would look something like this:

description: "Aqara-motion-sensor automation"
mode: single
triggers:
  - trigger: state
    entity_id:
      -  binary_sensor.lumi_lumi_sensor_motion_aq2_motion
    from: "off"
    to: "on"
    id: "On"
  - trigger: state
    entity_id:
      -  binary_sensor.lumi_lumi_sensor_motion_aq2_motion
    from: "on"
    to: "off"
    for:
      hours: 0
      minutes: 0
      seconds: 30
    id: "Off"
conditions: []
actions:
  - choose:
      - conditions:
          - condition: trigger
            id:
              - "On"
        sequence:
          - action: scene.turn_on
            metadata: {}
            data: {}
            target:
              entity_id: scene.aqara_lamp_25_cool
      - conditions:
          - condition: trigger
            id:
              - "Off"
        sequence:
          - action: light.turn_off
            metadata: {}
            data: {}
            target:
              entity_id: light.lumi_lumi_light_aqcn02_light

1 Like

thanks for the examples! I will check cookbook.

I tested example with ‘choose’ function and it works, thanks.
But it feels a bit complex with all On/Off ids. I guess you can’t create it using visual editor only.


Regarding initial problem, I have now automation that sets state to “off” (clear) for motion-sensor when HA starts and also turns off aqara lamp. And it worked in my testing.

You can name them as you like.

Yes you can (and I did) :wink:

1 Like