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.

20 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.