How to switch off a PIR after x seconds, help!

Hi.
I have some PIRs that are automatically created and updated in HA via RESTful.

The problem I have is that the PIRs only send a movement trigger, there is no Off.

They are configured as binary_sensor’s. I know that auto_off is a template option, but I have not had any luck setting this up at all.

I’ve also tried setting up an automation to do this, but again it doesn’t appear to work.
The automation is being triggered, but the state never changes back to Off.

Here is the automation code that was automatically generated in HA.


- id: '1621096094293'
  alias: testing pir
  description: ''
  trigger:
  - platform: state
    entity_id: binary_sensor.thorian_pir1
    to: 'On'
    from: 'Off'
    for: 00:00:10
  condition: []
  action:
  - condition: state
    entity_id: binary_sensor.thorian_pir1
    state: 'Off'
  mode: single

Hope someone can help. I’ve played about with this for many hours now.
Thanks.

Don’t capitalise the states

    to: 'on'
    from: 'off'
  - condition: state
    entity_id: binary_sensor.thorian_pir1
    state: 'off'

And you also can’t set the state of a binary sensor in an automation.

if you look at your action code above that isn’t what that piece of code is doing.

What it is doing is checking the condition of the binary sensor as being “Off” (which as noted needs to be lower case) before running the follow-on actions…which there aren’t any.

I’m kind of surprised the config check passed on that since there are no real actions.

But I saw that automations configured thru the UI don’t need triggers either.

So it looks like bug to me but it may be intended. :man_shrugging:

However, you can use the new “trigger” binary sensors to set the state.

Thanks for the information folks.
I’ve altered all the off’s, on’s to lower case.

Is it possible you could show me the code needed to make this work?
Apologies, I’ve only been using HA for a week.

Many thanks for your time.
Ian.

You say that you are using REST to get the values of your PIR, so you would be pulling values?
But then you say that “PIRs only send a movement trigger”, which implies pushing.

Maybe show the configuration of your PIR in HA…

Hi koying.
I believe its push.
There is no configuration in HA for this, although I have tried to set it up in customize.yaml so that it would remember a friendly name, but this hasn’t worked.

here’s what’s being sent to HA from the PIR via rest:

Method POST
http://192.168.0.59:8123/api/states/binary_sensor.thorian_temp13
Headers:
authorization: Bearer <my long term token>
content-type: application/json
Body:
{
  "state": "on",
  "attributes": 
    {
     "icon": "mdi:motion-sensor",
     "auto_off": "00:00:10"
    }
}

Auto off appears to be ignored.

On starting HA, the sensor in the UI show as ‘Entity not available: binary_sensor.thorian_pir1’
This is until an update ‘on’ has been received from the PIR itself.

I’m clueless as to how to make these time out after 10 seconds, or to give them a permanent friendly name in HA.
If this isn’t possible, maybe if I convert them to switches it would make life easier?

Thanks,
Ian.

Oh, ok. And can you change the webhook used by you PIR?

If so, you could combine 2021.5: Stability, performance, triggers, color modes! - Home Assistant (home-assistant.io) and Template - Home Assistant (home-assistant.io)

Thanks koying.
Unfortunately there is no webhook. Its generally a one way conversation from pir to host.

I’ve done some testing since my last message and found that I can get everything to work if I configure the pir’s as switches.
I would prefer HA to recognise them as binary movement sensors and will keep trying.

Thanks,
Ian.

Try the template based trigger binary sensor as I recommended above.

Hi again.
I’ve quickly Googled webhooks. It would seem this is what I’m doing, one way traffic.

I’m still looking at the example code from your 2021.5 link.
Maybe this should work. I’ve pasted in my own take on it below, but its not working.

template:
  - trigger:
      - platform: event
        event_type: netatmo_event
        event_data:
          type: movement
    binary_sensor:
      - name: "thorian_pir1"
        # We use auto_off, so just set it to true on each trigger
        state: "true"
        device_class: motion
        # Automatically turn off 10 seconds after the last event
        auto_off: 10

Do you think I’ve missed anything?
Thanks.

What I meant is: Is there a way to change the REST call made from the PIR?

The template you show expects an event, so it would work if the PIR would do a POST /api/events/netatmo_event (see REST API | Home Assistant Developer Docs (home-assistant.io))

Hi.
Yes, I can send as an event instead.
I’m just experimenting in a rest terminal sending stuff to HA.

I’m pretty new at this. I’m not sure exactly what I should be sending or how to handle it in HA.
I’ve tried this

POST
http://192.168.0.59:8123/api/events/thorian_pir1
The usual headers...

BODY
{
  "state": "on"
}

I’m getting the response:
“message”: “State thorian_pir1 fired.”

Sorry, I’ve not done anything with events. Not sure how to get HA to interpret this.

Kind regards,
Ian.

Just use http://192.168.0.59:8123/api/events/netatmo_event with a body of:

{
  "type": "movement"
}

to match your template above

Oh wow, its working, along with the timeout that I set in the config file.

I have several PIRs, but I’m not sure how to get the others to work.
I’ve tried giving the event a different name for a different pir instead of netatmo_event, but I’m not sure how to put this into the template for multiple sensors?

Ignore me, I figured it out with something along the lines of this:

template:
  - trigger:
      - platform: event
        event_type: thorian_pir1_event
        event_data:
          type: movement
    binary_sensor:
      - name: "Kitchen PIR"
        state: "true"
        device_class: motion
        auto_off: 30
  - trigger: 
      - platform: event
        event_type: thorian_pir2_event
        event_data:
          type: movement
    binary_sensor:
      - name: "Garden PIR"
        state: "true"
        device_class: motion
        auto_off: 30   

Many thanks for your time on this, it is very much appreciated.

Kind regards,
Ian.

1 Like