Motion detector with timer

Hi,

I am testing with a IKEA motion detector to turn on lights in the basement and I want to turn them off after a period of time e.g. 5 min

So I have this timer:

timer:
  timer_light_basement:

and the following automation:

- alias: Light ON light if motion
  trigger:
  - platform: state
    entity_id: binary_sensor.ikea_motion_moveable_motion
    to: 'on'
  action:
    - service: timer.start
      entity_id: timer.timer_light_basement
      data:
        duration: "00:05:00"
    - service: switch.turn_on
      entity_id: switch.fibaro_plug_4_3

- alias: Turn OFF lights at end of timer
  trigger:
    - platform: event
      event_type: timer.finished
      event_data:
        entity_id: timer.timer_light_basement
  action:
    - service: switch.turn_off
      entity_id: switch.fibaro_plug_4_3

I have an entity card to monitor the entities and I have noted the following behavior:

  • When motion is detected, light turns on :slight_smile:
  • Timer starts at 4:24 not as 5:00 as expected (if I set "00:01:00" it starts at 0:24)
  • When timer is 0 (zero) it goes into "active" and no "finished" event is triggered but after additional 36 sec it is fired

So…

  • Is this correct behavior of the timer, shouldn’t it start at the duration configured?
  • The IKEA motion detector is set to on (Detected) for 3 min, is it possible to change the time when it goes to off (Clear)?

Have you tried to trigger lights at motion, then use “wait for time to pass (delay)” for 5 minutes, then turning them off again?

Something like this:

mode: single
trigger:
  - platform: state
    entity_id:
      - binary_sensor.motion_occupancy
    to: "on"
condition: []
action:
  - service: light.turn_on
    data: {}
    target:
      entity_id: light.blabla
  - delay:
      hours: 0
      minutes: 5
      seconds: 0
      milliseconds: 0
  - service: light.turn_off
    data: {}
    target:
      entity_id: light.blabla

This will not reset the timer when motion is detected before the 5 minutes have passed though. This blueprint will. https://github.com/home-assistant/core/blob/02c27d8ad27e1ca94020b96d77cd1a491c253edd/homeassistant/components/automation/blueprints/motion_light.yaml

I just use two different automations. One to turn the lights on, and one to turn the lights off when there has been no motion for a period of time:

- id: '1679711111131'
  alias: Light - On
  description: ''
  trigger:
  - type: motion
    platform: device
    entity_id: binary_sensor.lumi_lumi_sensor_motion_aq2_iaszone
    domain: binary_sensor
  condition: []
  action:
  - type: turn_on
    entity_id: light.lightbulb_mintransitionlight_2
    domain: light
    brightness_pct: 100
  mode: single
- id: '1679791111113'
  alias: Light - Off
  description: ''
  trigger:
  - type: no_motion
    platform: device
    entity_id: binary_sensor.lumi_lumi_sensor_motion_aq2_iaszone
    domain: binary_sensor
    for:
      hours: 0
      minutes: 1
      seconds: 30
  condition: []
  action:
  - type: turn_off
    entity_id: light.lightbulb_mintransitionlight_2
    domain: light
  mode: single

Yeah that’s the way to do it (though I would recommend not using device triggers and actions). There used to be an example like this in the docs (it is a common question);

automation:
- alias: Turn on kitchen light when there is movement
  trigger:
    platform: state
    entity_id: sensor.motion_sensor
    to: 'on'
  action:
    service: homeassistant.turn_on
    entity_id: light.kitchen_light

- alias: Turn off kitchen light 10 minutes after last movement
  trigger:
    platform: state
    entity_id: sensor.motion_sensor
    to: 'off'
    for:
      minutes: 10
  action:
    service: homeassistant.turn_off
    entity_id: light.kitchen_light

I’m sure there are automation blueprints for this too if you search.

Out of curiosity, why do you recommend against using device triggers and actions? They seem to work fine, but I’m always looking to learn new and better ways!

Toml named the reasons in the linked thread.
Entity_ids are renameable, if you change the device that triggers the automation you can simple rename the entity_id of the new device to that one of your old, and the automation runs as before.
Using a device trigger you have to change the automation.
And device triggers don’t support templating, so it’s a good thing to get used used to the way toml suggested…
In your example imho it would imho now make no difference, both will do the same. But for things in future it would be more flexible and is easier to code in yaml for example.

I see. Makes sense.

For the record, there was no link in his post when I replied (must have been an edit), but thank you for spelling it out for me. :slight_smile:

Glad i could help :slightly_smiling_face:

Thanks all for your input and ideas!
I’ll be testing out some more implementations as you suggested before using it live.
But the fact is that my configuration is doing its job but don’t understand the logic behind the timer’s start point at xx:24 before going to active for additional 36 secs and then the finished event is fired…but somethings are just to get used to :smiley:

There are a lot of ways to accomplish this, but you might consider using Automation for it. I used to script mine but found Automation better for my needs because I wanted to be able to re-trigger that 5 minute time out rather than let the light(s) go out, wait for motion and turn them back on again.