Howto: Turn lights on/off using motion sensors, with a single call

Hi, All. I’m posting this because I gathered many bits and pieces of info from this forum to do exactly what I wanted, and in case anyone else finds that useful, well, here you go.

GOAL: one motion.yaml file which will turn “on” a set of lights when it detects motion, and will turn those same lights “off” when motion clears, after a configurable time delay.

WHY NOT USE THE INCLUDED BLUEPRINT: The blueprint for motion is very limited in its capabilities. It only shows me a few of my available sensors and lights. By no means will that blueprint configure a complex set of lights.

So, here is what I did:

alias: Hue Motion Test
description: ''
  - platform: state
    entity_id: binary_sensor.hue_motion_sensor_4_motion
    from: 'off'
    to: 'on'
  - type: no_motion
    platform: device
    device_id: 7375a30343104473848853921475430a
    entity_id: binary_sensor.hue_motion_sensor_4_motion
    domain: binary_sensor
      hours: 0
      minutes: 0
      seconds: 10
      milliseconds: 0
condition: []
  - service: homeassistant.turn_{{trigger.to_state.state}}
        - light.r1
        - light.r2
        - light.r3
        - light.r4
mode: single

This will simply poll the desired motion sensor, turn the lights on if it detects motion, and turn them back off 10 seconds after motion clears. Obviously that’s just a test, you’d want a more meaningful number like 120 seconds there for production.

With my Wyze motion sensors, this is instant. With Hue, since the integration only polls every 5 seconds by design, the sensors are slower, but the automation still works just fine.

Note I used a different trigger for seeing and clearing motion, that was by choice because the Hue has the fancy “motion has cleared” option. You can use the same trigger twice if you want.

I use Jinja to yank the word “off” or “on” from the entity state, and glob that on the end of the call to homeassistant.turn_on/off service and whambalam. The lights do what they are told. :slight_smile:

What I have done with my production motion.yaml file is the same thing for all 4 sensors in my home. One file - 4 sensors - many lights on or off.

Hope this helps someone.

I was busy lately with some new lights and sensors, so this is timely.

I think using a helper to set the timeout from the ui might be a useful addition?

Someone with more understanding of the internals might be able to tell us which is easier on the processor. I could see a helper being useful, but since HA is polling constantly, I’m just watching for a state to go on or off, then waiting to act. It works, but it might be done better witih less memory and system calls, IDK.

Thanks for the idea! If you flesh it out, please post!