Best Practises for Motion Activated Lights

I’m trying to figure out what’s the best why to create an automation to turn lights on in a room when there’s motion and then turn the lights off when no motion is detected.

I used to have two separate automatons, one that detected motion and turned the lights on and another that detected no motion and turned the lights off and it worked, but seemed inefficient.

Recently I created one automation that turns on lights when motion is detected, then waits for a trigger of no motion detected and then turns the light off, it seems to work well but sometimes I have issues. I just discovered one automation that wasn’t firing even when motion was detected and then I disabled and re-enabled the automation and it started working again.

Here’s an example of the automation that wasn’t working until disable/enable:

alias: Tool Bench Lights
description: ""
trigger:
  - type: motion
    platform: device
    device_id: 09ea66b8006a002cb6ffa125e6f7deeb
    entity_id: f2d3faa59b3fa5680c49eab5927b5010
    domain: binary_sensor
condition: []
action:
  - service: light.turn_on
    data:
      brightness_pct: 100
    target:
      entity_id:
        - light.work_1
        - light.work_2
        - light.work_3_socket_1
        - light.work_4
  - wait_for_trigger:
      - type: no_motion
        platform: device
        device_id: 09ea66b8006a002cb6ffa125e6f7deeb
        entity_id: f2d3faa59b3fa5680c49eab5927b5010
        domain: binary_sensor
        for:
          hours: 0
          minutes: 5
          seconds: 0
  - service: light.turn_off
    data: {}
    target:
      entity_id: light.tool_bench_lights
mode: single

I’m also concerned that if I reboot the server while one of these automations is running/waiting for trigger that it breaks the automation. Also what happens if I use the philip hue dimmer switch or alexa to turn the lights off while the automation is running? Maybe it is better to just have two separate simple automations so nothing is running or waiting?

Hoping some expert can chirp in and help me understand this.

Cheers,

Ted

I would use entities and 2 triggers:


trigger:
  - platform: state
    entity_id: binary_sensor.xyz
    from: "off"
    to: "on"
    id: "on"
  - platform: state
    entity_id: binary_sensor. xyz
    from: "on"
    to: "off"
    id: "off"
    for:
      minutes: 5
action:
  - if:
      - condition: trigger
        id: "on"
    then:
      - service: light.turn_on
        data:
          brightness_pct: 100
        target:
          entity_id: 
            - light.1
            - light.2
            - light.3
            - light.4

    else:
      - service: light.turn_off
        target:
          entity_id: light.5

1 Like

There’s an example automation in the community guides:

Hey Guys, thanks for the help, I think this is exactly what I was looking for. Never would have thought to use States as triggers with If, Then, Else actions.

Also, look for the sensor light blueprint by blacky.

Here is a link:

1 Like

Ok wow, ask and you shall receive!

I think that blueprint has everything you could ever possibly ask for. Funny I was looking for something exactly like this but could only find very basic blueprints when I was searching, this blueprint should be at the top of any search results for motion activated lights.

I see there’s an option to have the lights turn on based on LUX levels, does anyone know if this option can be used to turn lights on to a certain brightness level based on LUX level? For example in my office I have a motion sensor that triggers the lights to turn on and I have a condition that checks what the current LUX level is then sets the lights to a certain brightness level. I only have two options if LUX is below 30 or above 30.

I’ve been trying to adapt the former examples you provided to use the above LUX conditions but maybe this blueprint could make things easier.

Cheers,

Ted

Ok I’ve played around with that blueprint and it doesn’t seem like I can have conditions to set the lights to a certain brightness based on the LUX level, too bad but I guess beggars can’t be choosers, there’s probably not that many people who want to set the brightness level based on LUX level. Back to editing the previous example I guess…

Does anyone know if you can do If, Then, If, Then, Else instead of If, Then, Else? I know very little about this stuff and the previous examples are all If, Then, Else. I would need “if LUX level is above 30 then set brightness to X, if LUX level is below 30 then set brightness to X, else lights turn off” does that make sense??

Again much appreciation for the help, this community always blows my mind with the amazing help you receive from others.

Cheers,

Ted

you could use the blueprint as base.

Check the trace history, and under the tab ‘automation config’ you will find the flat automation yaml, which you can copy/paste and then edit as you want :wink:

I think using the blueprint as a base would be a good idea but maybe too advanced for my skill set. I’ve played around with the previous examples and come up with my own solution that appears to be working, not sure if this is the best way to have multiple “If Then” actions:

trigger:
  - platform: state
    entity_id:
      - binary_sensor.lumi_lumi_motion_ac02_motion
    from: "off"
    to: "on"
    id: "on"
  - platform: state
    entity_id:
      - binary_sensor.lumi_lumi_motion_ac02_motion
    from: "on"
    to: "off"
    id: "off"
    for:
      hours: 0
      minutes: 10
      seconds: 0
condition: []
action:
  - if:
      - type: is_illuminance
        condition: device
        device_id: 44ed940efb2ffbce74d3868f9f8b14d8
        entity_id: a6d7992cb2460f28670bdcf43a33957d
        domain: sensor
        above: 30
      - condition: trigger
        id: "on"
    then:
      - service: light.turn_on
        data:
          brightness_pct: 45
        target:
          entity_id: light.office_lights
  - if:
      - type: is_illuminance
        condition: device
        device_id: 44ed940efb2ffbce74d3868f9f8b14d8
        entity_id: a6d7992cb2460f28670bdcf43a33957d
        domain: sensor
        below: 30
      - condition: trigger
        id:
          - "on"
    then:
      - service: light.turn_on
        data:
          brightness_pct: 35
        target:
          entity_id: light.office_lights
  - if:
      - condition: trigger
        id:
          - "off"
    then:
      - service: light.turn_off
        data: {}
        target:
          entity_id: light.office_lights
mode: single

Cheers,

Ted

I haven’t used home assistant yet. I keep planning on doing it when I get time and money. So I’m curious could you do something like have one automation that turns the light on and off to a saved light level. Then have a second automation that changes the saved light level and changes the light level if it is not off.

You can ‘snapshot’ the state of an entity that you are going to be altering in an automation, then restore its state afterwards.

alias: on_front_door_opens
description: >-
  When the front door opens, turn on the entrance light and restore it to its
  former state after the door closes
trigger:
  - platform: state
    entity_id:
      - binary_sensor.door_entrance_02
    to: "on"
    for:
      seconds: 5
condition: []
action:
  - service: scene.create
    data:
      scene_id: scene_before_front_door_opened
      snapshot_entities: |
        {{ 
          expand (
          'light.room_light_group_entrance', 
          'light.room_light_group_porch'
          )
          | rejectattr('state', 'eq', 'unavailable') 
          | map(attribute='entity_id') 
          | list 
        }}
    alias: "Scene: Create - Remember States"
  - if:
      - condition: numeric_state
        entity_id: sun.sun
        attribute: elevation
        below: 7
        alias: If Sun Elevation below 7
    then:
      - service: light.turn_on
        data:
          brightness: 255
        target:
          entity_id: light.room_light_group_porch
        alias: Porch Lights - On
      - service: light.turn_on
        target:
          entity_id: light.room_light_group_entrance
        alias: Entrance Lights - On
      - wait_for_trigger:
          - platform: state
            entity_id:
              - binary_sensor.door_entrance_02
            to: "off"
            for:
              seconds: 10
            alias: When Front Door Closes
      - service: light.turn_off
        target:
          entity_id: light.room_light_group_entrance
        alias: Entrance Lights - Off
  - service: scene.turn_on
    target:
      entity_id: scene.scene_before_front_door_opened
  - delay:
      minutes: 1
  - service: script.phones_ping_update_entity
    data: {}
mode: restart

Hey,
I’ve been experimenting myself. Problem now is that i have a automation that works sometime and sometimes not and i can’t figure out why.
So first i want to share the automation and hope someone can spare some time to look in to it and give me, as a newby, some advise .

I will also mention the devices i use so maybe to hear if they are known to give problems.

The goal is a sensor activated light that consists of 2 light sources.

  • RGB lightbulb LIDL HG07834c
  • LIVARNO LUX SMARTLED strip HG06104 A
    and the
  • TUYA ZG204 ZL sensor.

First the LUX is detected in the room to see if the lights need to go on.
Second the time is detected to see if the lights have to go to a 100% white during the daytime or a 25 % colored light during nighttime.
There is also a delay to turn the lights of after no movement was detected

Here’s the code

alias: Nachtlicht Overloop
description: ""
mode: single
triggers:
  - entity_id:
      - binary_sensor.ms_overloop_occupancy
    from: "on"
    to: "off"
    id: Beweging Uit
    trigger: state
  - entity_id:
      - binary_sensor.ms_overloop_occupancy
    from: "off"
    to: "on"
    id: Beweging Aan
    trigger: state
conditions: []
actions:
  - choose:
      - conditions:
          - condition: trigger
            id:
              - Beweging Uit
        sequence:
          - delay:
              hours: 0
              minutes: 1
              seconds: 30
              milliseconds: 0
            enabled: false
          - if:
              - condition: state
                entity_id: binary_sensor.ms_overloop_occupancy
                state: "off"
            then:
              - delay:
                  hours: 0
                  minutes: 1
                  seconds: 30
                  milliseconds: 0
              - data: {}
                target:
                  device_id:
                    - c995d9b62ec614e6360295ff63817bef
                    - 6d322e8e59d90758a1fb33e40a7f1927
                action: light.turn_off
      - conditions:
          - condition: trigger
            id:
              - Beweging Aan
          - condition: numeric_state
            entity_id: sensor.ms_overloop_illuminance
            below: 500
            enabled: true
          - condition: time
            after: "22:00:00"
            before: "09:00:00"
        sequence:
          - data:
              rgb_color:
                - 211
                - 87
                - 254
              brightness_pct: 25
            target:
              device_id:
                - c995d9b62ec614e6360295ff63817bef
                - 6d322e8e59d90758a1fb33e40a7f1927
            action: light.turn_on
      - conditions:
          - condition: trigger
            id:
              - Beweging Aan
          - condition: numeric_state
            entity_id: sensor.ms_overloop_illuminance
            below: 500
            enabled: true
          - condition: time
            after: "09:00:00"
            before: "22:00:00"
        sequence:
          - data:
              brightness_pct: 100
            target:
              device_id:
                - 6d322e8e59d90758a1fb33e40a7f1927
            action: light.turn_on
            enabled: false
          - action: light.turn_on
            metadata: {}
            data:
              brightness_pct: 100
              kelvin: 2354
            target:
              device_id: c995d9b62ec614e6360295ff63817bef
          - action: light.turn_on
            metadata: {}
            data:
              brightness_pct: 100
              kelvin: 2354
            target:
              device_id: 6d322e8e59d90758a1fb33e40a7f1927

If more information is needed please say which.