Motion triggered light automation help

Hey guys this is my code that works well for the most part but there is just one little quirk I’m trying to iron out. Here is my code.

 ###################################      
##  Entry night light motion on  ##
###################################

- alias: entry night light on
  trigger:
  - platform: state
    entity_id: binary_sensor.motion_sensor_158d0002b43d99
    from: 'off'
    to: 'on'
  condition:
    condition: and
    conditions:
    -  condition: state
       entity_id: light.entry_light
       state: 'off'
    -  condition: time
       after: '22:00:00'
       before: '07:00:00'
  action:
  - service: light.turn_on
    data:
      entity_id: light.entry_light
  - service: homeassistant.turn_on
    entity_id: input_boolean.entry_light_automation

##################################    
##   entry light no motion off  ##
##################################

- alias: entry night light off
  trigger:
  - platform: state
    entity_id: binary_sensor.motion_sensor_158d0002b43d99
    from: 'on'
    to: 'off'
    for:
      minutes: 2
  condition:      
  - condition: state
    entity_id: input_boolean.entry_light_automation
    state: 'on'
  action:  
  - service: light.turn_off
    data:
      entity_id: light.entry_light
  - service: homeassistant.turn_off
    entity_id: input_boolean.entry_light_automation

The input_boolean is to stop turning the light off if for some reason it has been manually turned on. However I have a problem where in the morning I turn all the lights off and the automation won’t trigger the front door light when I’m near.

I think it’s because I’ve had the lights on walking around so the motion sensor has already been triggered on. So now when I turn the lights off I won’t get a trigger because There is no change of state with the motion sensor.

Is there a way I can code it so that If between the hours off 22:00 and 07:00 that if the light is off it will turn it on so long as the sensor has detected motion? That way as soon as I turn all the lights off it will turn the entry light on if the motion sensor is already detecting motion, It won’t have to wait for it to go off and triggered on again to trigger the automation.

Cheers

You could make another automation to trigger when you turn all the lights off. Have conditions check the time and state of the movement sensor and entry light. This assumes your movement sensor is actively “on” for a long time. Some battery powered devices send on-off then do nothing for a period of time to conserve power. It wont work in this case. You’ll have to check the last changed attribute of the sensor instead of the state.

What sort of movement detector do you have? Is power available where it is installed? Changing to a non battery powered sensor would allow movement information to be sent whenever it occurs, not just every five minutes or whatever the one you have now is doing.

It’s an xiaomi sensor so it takes a little bit to update since this is the only quirk I have with the whole automation I might even change my automation to turn off all the lights ( I use a little xiaomi wireless button near the door to turn all the main living area lights on and off ) To leave the entry light on for an extra 2 minutes before turning it off as a work around.

The way you described could work too, If the motion sensor state goes to off then it will just revert to my original automation anyway.

A lot of battery powered devices will only send out movement detection alerts every 5 minutes or so to save battery life. The assumption being that these will be used for security and not presence based lighting. You only need one alert that someone is in your house when they shouldn’t be for the alarm to sound.

The xiaomi device appears to be a little better in that it actually sends out an ‘on’ message for two minutes. So the other automation solution will work and you only need to check the movement sensor state not the last changed attribute when it triggers on you turning all the lights off.

Edit: that’s not true, see the discussion here: Xiaomi Human / Body / Motion Sensor - Timeout

Particularly this post: Xiaomi Human / Body / Motion Sensor - Timeout - #20 by Dullage

1 Like

Cheers mate thanks for the info. I’ll look into getting some better sensors for my automation in the meantime I’ve just set the entry light to remain on for an extra 2 minutes after I turn all the lights off to allow me time to get out of the house.

1 Like