Trying to restart timer from second sensor

Hey Everyone,
I am a little stuck on a timer for my hallway. I have a 2 way lighting circuit that works fine in HA and a timer that turns off the light after 2 mins, that also works fine. What I can not figure out is how to restart the timer if movement is detected by a PIR ‘On’.
Here is my automation-:

#Hallway Timer
- alias: Turn off hallway light 2 minutes after last movement
  trigger:
    platform: state
    entity_id: binary_sensor.hall_lights, binary_sensor.hall_pir
    to: 'on'
    for:
      minutes: 2
  action:
    service: input_boolean.turn_off
    entity_id: input_boolean.2way_toggle5

The binary_sensor.hall_lights is a current sensor that actually tells me when the light is physically ‘On’ and the other binary sensor is the PIR.
Am I correct in saying that if the PIR sensor goes to ‘On’ it will restart the 2 min timer?

Just for info, I am using the input_boolean as a virtual switch for the 2 way lighting.

What am I doing wrong?

Cheers
Simon

why don’t you put both PIRs into a group, and set the group as the trigger with the off for 2 min?
This way you need both PIRs to be off for the group to be off. when the group is off for 2 min, lights turn off. If one of the PIRs turns back on before the 2 min, the timer restarts…
By the way in the above automation I believe you need to change to: 'on' to to: 'off'

I think you are using a too “complicated” approach for this.

Create one automation that turns on the light on motion and starts the timer. When an already running timer is started, it will restart the timer.
Then create a second automation that turns off the light when the timer has finished.

Hi, @lolouk44 There is only one PIR and one CT coil sensor that tells me when the light is physically on. The light going on I want as the trigger, I am just trying to reset the timer back to 2 min if the PIR is triggered again.

@Burningstone I dont want a trigger to turn the light on. The idea behind all this is that if the kids leave the hall light on, it turns off after 2 mins unless more motion is detected by the PIR. I am trying to make it simple but with the 2way lighting and control via Alexa, this makes it more complex.
Hope this makes sense.
I have previously had it working very well in Openhab but am struggling in HA…

Then create a group of the PIRs as @lolouk44 suggested and the Then just change ‘on’ to ‘off’ in your automation, like this:

- alias: Turn off hallway light 2 minutes after last movement
  trigger:
    platform: state
    entity_id: group.pirs_hallway
    to: 'off'
    for:
      minutes: 2
  action:
    service: input_boolean.turn_off
    entity_id: input_boolean.2way_toggle5

This way the light will turn off, once both motion detectors are ‘off’ for 2 minutes.

so 1 PIR sensor only, and you use the binary_sensor.hall_light to tell you the lights are on right?
Then

- alias: Turn off hallway light 2 minutes after last movement
  trigger:
    platform: state
    entity_id: binary_sensor.hall_pir
    to: 'off'
    for:
      minutes: 2
  condition:
    - condition: state
      entity_id: binary_sensor.hall_lights
      state: 'on'
  action:
    service: input_boolean.turn_off
    entity_id: input_boolean.2way_toggle5

Thank you for your help but there is only one PIR as I said above. The timer is triggered when the CT sensor goes On, the PIR is there for motion/prasense detection.

would the PIR turn automatically on when you turn the lights on?
If yes, then above is right, if not, put the coil and PIR in 1 group and remove the condition

Ah yes that’s correct, thanks I see how now I think. Will give it a go tomorrow.

So if I understand correctly, doing it this way if the PIR is retriggered within the two mins the timer restarts? Thats all part of the timer function within HA?

Cheers

Simon

Correct as the trigger will only fire the automation if the entity has been in that state for 2 min

Ok I am still struggling with this. I sort of understand how the above code works with the condition but it does not work. The light doesn’t turn off at all. What’s the best way of going about debugging this?

@lolouk44 can I put the
for: minutes: 2
Part in the condition?

Can you show the code please?

@Burningstone hi, it’s exactly the same as @lolouk44 posted last 10 posts up.

Cheers

- alias: Turn off hallway light 2 minutes after last movement
  trigger:
    platform: state
    entity_id: binary_sensor.hall_pir
    to: 'off'
    for:
      minutes: 2
  condition:
    - condition: state
      entity_id: binary_sensor.hall_lights
      state: 'on'
  action:
    service: input_boolean.turn_off
    entity_id: input_boolean.2way_toggle5

No you can’t do it that way.
This would be the format :

    - condition: template
      value_template: '{{(as_timestamp(now()) - as_timestamp(states. binary_sensor.hall_pir.last_updated | default(0)) | int > 3600 )}}'

Why would you want that though?

@lolouk44 to be honest I am not sure now I have thought about it again!:crazy_face:
Should the timer not be triggered by the current sensor instead of the pir? Then the timer restarted or reset by the pir if it sees movement? I just can’t say that in HA automation speak…

Something like this?

- alias: Turn off hallway light 2 minutes after last movement
  trigger:
    platform: state
    entity_id: binary_sensor.hall_lights
    to: 'on'
    for:
      minutes: 2
  condition:
    - condition: state
      entity_id: binary_sensor.hall_pir
      state: 'off'
  action:
    service: input_boolean.turn_off
    entity_id: input_boolean.2way_toggle5

I come back to my original suggestion, slightly modified.
Create an automation that starts a timer when the light is turned on OR motion is detected. Then in the second automation turn the light off when the timer is finished.

Timer:

timer:
  hall_timer:
    duration: '00:02:00'

Automation 1:

- alias: "Start/restart timer for hallway lights"
  trigger:
    platform: state
    entity_id: 
      - binary_sensor.hall_lights
      - binary_sensor.hall_pir
    to: 'on'
  action:
    service: timer.start
    entity_id: timer.hall_timer

Automation 2:

- alias: "Turn off lights after 2 minutes of no motion"
  trigger:
    platform: event
    event_type: timer.finished
    event_data:
      entity_id: timer.hall_timer
  action:
    service: input_boolean.turn_off
    entity_id: input_boolean.2way_toggle5

Please note that I typed this on the phone, so it may have some errors.

1 Like

Well you were right on the mark, that works perfectly thank you. I was looking at the start and stop timer function last thing yesterday but ran out of time. Anyway, thanks for the assistance and I have certainly learnt a lot.

1 Like