Motion activated lights automation

This gets asked a lot so here is an example for automating a light to come on with movement and go off after there is no movement for a short time.

trigger:
  - platform: state
    entity_id: binary_sensor.your_motion_sensor_here # change this
    to: 'on'
  - platform: state
    entity_id: binary_sensor.your_motion_sensor_here # change this
    to: 'off'
    for:
      minutes: 2 # adjust as necessary 
action:
  - service: "light.turn_{{ trigger.to_state.state }}"
    target:
      entity_id: light.your_light_here # change this
mode: single

The Home Assistant Cookbook - Index.

22 Likes

Hehe…you should pin this one :face_holding_back_tears:

For the Node-Red folks:

I prefer this one for NodeRed :grin:

This is great, thanks for providing it @tom_l.

I’d like to have a group of 3 lights pulse on/off for say 1 minute after motion is detected. Is there a way to achieve this using your example?

Start another topic. It’s beyond scope for this one.

1 Like

4 posts were split to a new topic: Help with motion lights

Since not all motion sensor return to an ‘off’ state, you might end up with using a delay.
In that case, you need to put the automation in restart mode, so it will reset the delay when re-triggered.
f.e. my mqtt motion detector:

alias: Hallway Downstairs Light on Motion
description: Off when no motion for 3 mins
trigger:
  - alias: When motion detected hallway downstairs Pir] was trigged by MQTT
    platform: mqtt
    topic: Tasmota/tele/RESULT
    payload: D4A0EF
    value_template: '{{ value_json.RfReceived.Data}}'
condition: []
action:
  - service: light.turn_on
    target:
      entity_id: light.hallway_downstairs
  - delay:
      hours: 0
      minutes: 3
      seconds: 0
      milliseconds: 0
  - service: light.turn_off
    data: {}
    target:
      entity_id: light.hallway_downstairs
mode: restart

1 Like

Hi,
I’m using this automation with some PIR sensors and it works pretty good. But now I have the problem and want to make the light only go on when it’s dark. So I have a new PIR/Lux sensor which reports the current Lux AFTER motion is detected. If I use motion sensor first, light will not trigger, because Lux is still from last state, when Light was on. So I need to “delay” before condition or need to trigger on lux change.
I already tried without luck:

trigger:
  - platform: numeric_state
    entity_id: sensor.pir_kuche_beleuchtungsstarke
    below: 600
  - platform: state
    entity_id: binary_sensor.pir_kuche_bewegung
    to: "off"
    for:
      minutes: 5
action:
  - target:
      entity_id: light.kuchenwand_licht
    action: light.turn_{{ binary_sensor.pir_kuche_bewegung }}
mode: single
(Error rendering service name template: UndefinedError: 'binary_sensor' is undefined)

Any Idea this could be solved?

I’m doing this too for some lights.

That’s the problem if you don’t have the right LUX value before the motion is detected.
If you want to control lights based on whether it’s dark or not, you will need a sensor that reports this, no matter what.
I have Philips Hue motion sensors and they update quite fast, even though not that precise.

For really reliable light switching, based on luminance having a sensor outside or in front of a window is your best option.
I have been looking for some time for this to my liking but have not found it yet.

Just because it’s dark outside, doesn’t mean it’s dark in the kitchen or hallway. :sweat_smile: For example if I walk down the stairs, there is already light at ground floor, so sensor is reporting 800lux after motion detection and I don’t need extra light at night. So I would need one sensor for motion and one for luminous, which needs to be connected to wire and update every second… This would be pretty bad solution.

Would be better if there is something like
If motion on/off, wait 1 second, switch based on luminous and state from trigger (or leave light on of trigger was on and not off after 2 minutes).

I use a Fibaro motion sensor for this. Reports motion, temperature, lux, but all as independent measurements. For lux and temperature, the user can set the reporting interval.

Okay, so there is no natural light where you want the detection.

Why don’t you just use the state of those lights, on/off/brightness, to decide whether you want that extra light to turn on (or not) so you don’t need an additional sensor.

Well, let’s say there is natural light, but not only that or not enough. For Example:
Over a normal, sunny day, there is enough light in the entrance area. So I don’t want these lights go on.
If it’s really cloudy or in the evening, it’s to dark, so lights need to go on.
If there is light on at the stairs to first floor or dinner table, I also don’t need light, so it should stay off.

That’s why I use a pir sensor with lux at this area (you get them for 3,69€ at Aliexpress Choice bundle). Only downside: Lux get reportet AFTER motion detection. So this is the Problem:
Motion is off ; Lux 1000
Motion on is triggert → Lux 1000 → Light stays off
Lux gets updated → 900
After 1 Minute Motion off is triggert → Lux 900
Lux gets updated → 950 (because of light)
After 2 Minutes without Motion → Light goes off → Lux still 950
Motion on is triggert → Lux 950 → Light stays off
Lux gets updated → 300 => I want the light to be on

I’m trying tonight with more complex script, hope that works, unless @tom_l comes up with a smarter way.

alias: Flur Licht
description: ""
trigger:
  - platform: state
    entity_id: binary_sensor.pir_lux_eingang_flur_bewegung
    to: "on"
    id: trigger_on
  - platform: state
    entity_id: binary_sensor.pir_lux_eingang_flur_bewegung
    to: "off"
    for:
      minutes: 2
    id: trigger_off
action:
  - delay:
      hours: 0
      minutes: 0
      seconds: 1
      milliseconds: 0
  - choose:
      - conditions:
          - condition: trigger
            id:
              - trigger_off
        sequence:
          - action: light.turn_off
            target:
              entity_id: light.flurlicht_licht
            data: {}
      - conditions:
          - condition: numeric_state
            entity_id: sensor.pir_lux_eingang_flur_beleuchtungsstarke
            below: 600
          - condition: trigger
            id:
              - trigger_on
        sequence:
          - action: light.turn_on
            target:
              entity_id: light.flurlicht_licht
            data: {}
      - conditions:
          - condition: numeric_state
            entity_id: sensor.pir_lux_eingang_flur_beleuchtungsstarke
            above: 600.1
          - condition: trigger
            id:
              - trigger_on
        sequence: []
mode: single

Well, then doing something as figured out yourself, wait a little delay after the trigger to use the value of the LUX sensor to decide what the light has to do.
On top of that, you might move the sensor to a more strategic place - if possible - to compensate that delay.

But, to be fair, for the price of those sensors you cannot expect more.
Are they reliable for motion detection, no false positives?

It seems that the answer is getting a non retarded light sensor.

True!

I think having a sensor near a window (or outside) is a better solution and even in this case, the outside brightness is playing a role.

I’m new here. I’ve been reading lots, but obviously lots more to read. Seeing lots of posts where people say “I’m new here” and proceed to ask a question that shows they’ve read nothing. I hope this doesn’t turn out to be yet another!

I’m using this automation with a Zooz ZSE70 sensor and ZEN71 switch. I’m generally happy with it. I’ve even managed to add a condition so that it only triggers after sunset/before sunrise.

My only issue with it is that the “motion off” trigger turns off the light regardless of how it was turned on. I’d also like to be able to turn the light on manually and have it stay on indefinitely.

After some reading I decided the way to achieve this was to use a template to make a copy of my switch that I could add a custom field to called, say, turned_on_by_automation. I’d then break this in to two automations - an “on” an “off”. The “on” automation would set turned_on_by_automation to True and my “off” automation would test for turned_on_by_automation=True before turning the light off and resetting it back to False.

When I tried to implement this plan it became clear I really didn’t understand templates and I was probably on the wrong path.

Is my plan workable if I can figure out how to implement it? Is there some other way I can store this turned_on_by_automation state?

Basically it is very hard to archive this with pir sensor. This is because pir sensor just have two states - motion detected, motion clear. It is great solution for stairs but it is not so good for dinning room or kitchen.
You can use ie. timer helper in automatons to keep the light on but this only work if sensor change state from off to on while automation is running.
The only solution to this problem, imho, is to use presence detection sensor.
Then you can turn on off light based on presence add illuminance to it etc.

I’m not seeing how this would help. I turn the light on manually because I want it on, I then leave the room so my presence is no longer detected and the automation would turn the light off?? This is not what I want - I want it to stay on forever if manually turned on.