Help with Timer and Lights on Motion

Hey all,

Ive setup the below, which works well to turn the lights on.
However im confused about the timer.
When there is motion in the room, the timer then starts to count down, however i was expecting that more motion in that room, would keep resetting the timer back to 30 minutes?
however it just keeps counting down? which means when it gets down to 0, all lights go off, even if there has been motion the room?

do i need to add in a stop function as well? i dont see a “reset” option?
where am i going wrong?

Host system

Hostname hassio
System HassOS 3.5

.

configuration:
timer:
  timer_motion_living_room:
    duration: '00:30:00'


automation:
############### living room ###############
- id: livingroomlightsonmotion
  alias: living room lights on motion
  trigger:
  - entity_id: binary_sensor.living_room
    from: 'off'
    platform: state
    to: 'on'
  condition: 
    condition: state
    entity_id: sun.sun
    state: below_horizon
  action:
  - data_template:
      entity_id: group.room_living_room_all_lights 
    service_template: switch.turn_on 
  - service: timer.start
    data:
      entity_id: timer.timer_motion_living_room

- id: livingroomlightsoffafter30minnomotion
  alias: living room lights off no motion 30min
  trigger:
    platform: event
    event_type: timer.finished
    event_data:
      entity_id: timer.timer_motion_living_room
  action:
  - data_template:
      entity_id: group.room_living_room_all_lights 
    service_template: switch.turn_off

You would need to set the timer to start with the new time.

But you’re overcomplicating this scenario anyway, get rid of the timer and change the trigger of the second one to

trigger:
  platform: state
  entity_id: binary_sensor.living_room
  to: 'off' 
  for:
    minutes: 30
4 Likes

Thanks - this does work!

i thought using a timer would be easier, as it means i can load this into the UI and have the misses then check/update it, if she wants more dynamically

You can still do that. Put an input_number on the ui and use its number for the for

for:
  minutes: "{{ states('input_number.light_timer')|int }}"
1 Like

What makes you say the timer keeps counting down? Are you just viewing it in the UI? Don’t believe it. Have you actually tested it – i.e., caused motion, waited, say 15 minutes, then caused more motion, and then timed how long it takes for the lights to go off? I suspect you’ll find that the timer is restarting when new motion is detected (assuming, of course, the binary sensor goes off and back on. If the binary sensor is staying on, that’s a different story.)

Huh? What do you mean, “set the timer”? Calling the timer.start service will restart a timer that is already running.

That’s a good suggestion! :slightly_smiling_face: However, note that this doesn’t take into account whether the sun is up or down, and whether or not the first automation actually triggered (meaning it might turn the light off even if it was manually turned on during the day.)

1 Like

This is good to know!!

as you say, its the front end that shows the timer not stopping, however good point to do a test, will try it now

I mean I frequently still keep getting notifications for the bug about the timer.start service not working as documented, so I presumed it still hasn’t been fixed, and the workaround was to start it with the new time.

Bug report

Workaround

service: timer.start
data:
  entity_id: timer.whatever
  duration: "00:30:00"

I didn’t venture in to it further as its a moot point as the trigger duration is accurate, reliable and a better fit for this scenario.

1 Like

Wow, that issue is extremely old, and it seems all the fuss may just be about the frontend not properly showing a restarted timer. I’ve never had an issue with restarting a running timer by using the timer.start service.

The only thing unexpected I’ve seen with timers is that restarting a running timer does not cause a timer.restarted event, but rather a timer.started event. See this post for more details.

1 Like

hi,im using this one in my automations,my binary_sensors are xiaomi/aqara door/window sensor so they refresh state after 2 mins,i dont know about refesh time of yours but maybe you can play with the wait_template

- id: 'Auto Luz Garaje'
  alias: Auto Luz Garaje
  initial_state: true
  trigger:
  - entity_id: binary_sensor.sensor_movimiento_garaje
    platform: state
    to: 'on'
  condition:
  - condition: state
    entity_id: sun.sun
    state: below_horizon  
  action:
  - delay: 00:00:01
  - data:
      entity_id: switch.luz_garaje
    service: switch.turn_on
  - wait_template: "{{ is_state('binary_sensor.sensor_movimiento_garaje', 'off') }}"
  - data:
      entity_id: switch.luz_garaje
    service: switch.turn_off

in my case the light does not go out while there is movement
regards

2 Likes