I have a PIR that only notifies me when it detects movement, but not when the movement stops. I tried writing the following automation to turn off this sensor after a period of time, but it looks like this service won’t work on a binary sensor.
- alias: PIR Timeout
trigger:
- platform: state
entity_id: binary_sensor.office_pir
to: "on"
for:
seconds: 10
action:
service: homeassistant.turn_off
entity_id: binary_sensor.office_pir
All I can think of now is writing a script that sends an “off” payload to the queue and set that as the action… but it just feels a bit wrong!
platform: state
entity_id: binary_sensor.lounge_pir
to: “on”
for:
seconds: 60
action:
service: mqtt.publish
data:
topic: /PIR/Lounge
payload: Close
This not will turn off the light after 60 seconds. The issue I have is that if another “on” comes in after the first one it doesn’t reset the timer. Only the inital state change does…
Ok, this works… but having to duplicate everything for all 4 PIR sensors is not ideal. Is there anything I can do with templating to reduce duplication? (I suspect no is the answer!
My scripts look as follows:-
timed_switch_turn_off_pir_lounge:
sequence:
# Cancel old pir timer
- service: script.turn_off
entity_id: script.turn_off_pir_lounge
# Set new timer
- service: script.turn_on
entity_id: script.turn_off_pir_lounge
turn_off_pir_lounge:
sequence:
- delay: '00:00:20'
- service: mqtt.publish
data:
topic: /PIR/Lounge
payload: Close
retain: true
Thanks for those examples.
I duplicated and adapted your scripts for cameras’ motion detection.
Camera uploads pics to FTP server on detected motion (Windows) -> Program detects new file (thefolderspy) -> mqtt_pub camera/room/motion 1 -> HASS turns lights on.
I am in the early stages of getting this done, but those examples definitely gave me a jump start! (especially on the delay reset).
It works perfectly, and I don’t mind duplicating the scripts for each camera.