MQTT Binary sensor - Is there a way to automatically turn off

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!

Hi @rossdargan, why should a script be wrong?
Here’s a thread with the same problem. Maybe for a start.

You can try it like this:

Automation:

- alias: PIR1
  trigger:
    platform: state
    entity_id: binary_sensor.office_pir
    to: 'on'
  action:
    service: homeassistant.turn_on
    entity_id: script.timed_lamp

Scripts:

timed_lamp:
  alias: "Turn on light and set timer"
  sequence:
    # Cancel potentially old running timer
    - service: script.turn_off
      data:
        entity_id: script.timer_off
    - service: light.turn_on
      data: 
        entity_id: light.hue_color_1_voor
    # Set new timer
    - service: script.turn_on
      data:
        entity_id: script.timer_off

timer_off:
  alias: "Turn off light after 1 min no motion"
  sequence:
    - delay:
        minutes: 1
    - service: light.turn_off
      data: 
        entity_id: light.hue_color_1_voor

Does it even make sense to use it as a binary sensor, rather than simply a trigger? The state itself isn’t meaningful.

I think the trigger is enough.

Thanks for your help. I’m nearly there!

  • alias: PIR Timeout
    trigger:
    • 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…

@rossdargan that’s why I’m saying don’t use a binary sensor at all, use an MQTT trigger like this:

trigger:
 platform: mqtt
 topic: /your/pir_sensor

This will fire each time the PIR detects movement.

AH!! I get it… I’ll give this a try :slight_smile:

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

and my automation looks like this:

  - alias: Lounge PIR Timeout
    trigger:
      platform: mqtt
      topic: /PIR/Lounge
      payload: 'Trigger'
    action:
      service: script.timed_switch_turn_off_pir_lounge

@rossdargan What are you trying to achieve?

So at the minute I need for every PIR sensor:-

An automation
A script to restart the timer
A script to turn off a sensor
The actual sensor

It would be great if I could reduce that somewhat perhaps using data templates, My guess is I can only have one instance of a script “active”.

Hi, i made my automation with appdaemon. The answer is in the link:

https://community.home-assistant.io/t/rf-433-mhz-pir-motion-sensor-raspberry-pi-configuration/11153/30?u=dronkeol

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.

Thanks again