Add off_delay to binary sensor added via UI

For a binary sensor entity, automatically added by an ONVIF camera integration via the UI, is it possible to add an off_delay parameter?

Similar functionality as described here, but I can’t do anything in the configuration.yaml file as the device was added via the UI (if I understood correctly).

My Hikvision camera never sends an event when motion is no longer detected (unlike my doorbell), so I’d like to reset the binary sensor after 10 seconds or so.

I’m on 2021.10.4 in a docker container.

It is not possible. You can add an automation on the state change, and use the set_state.py script to turn it off again.

Thanks for your reply.
That seems like a possible solution. I’ve been reading through that thread, have enabled python_scripts, created the folder and now I need to find the latest version of that script.
Why isn’t this part of HA?

Then I need to create an automation that triggers that script, 10 seconds after movement was detected?

Because the alternative way which doesn’t require adding anything to HA, is still perfectly possible, creating a templated binary sensor in YAML and using the autooff delay (which is what I intend to do, because my camera’s motion sensor goes off after about 2 seconds, but I want to use it for identifying that the house is occupied).

template:
  - trigger:
      - platform: state
        to: 'on'
        entity_id: binary_sensor.mymotiondetectorrule_cell_motion_detection
    binary_sensor:
      - name: Livingroom Motion
        state: "{{ on }}"
        auto_off: "00:15:00"
        device_class: motion

Replace with your actual motion detector from your camera, and replace the auto_off with how long you want to wait for the sensor to turn off. - Mine here is set to 15 minutes.

1 Like

Oh, so if I understand correctly you are using the camera’s motion sensor to trigger another binary sensor with the auto_off settings (not off_delay like the MQTT binary sensor).

Yes, please note I have just edited the post too, because I incorrectly used “state” in the template trigger instead of “to:”.

I have now successfully got this config running on the livingroom and bedroom motion detectors provided from the camera over the ONVIF integration.


and here it is doing exactly what I wanted - I triggered the bedroom camera motion ONCE and then left and 15 minutes later, it changed to clear. (Note I am using device_class: occupancy instead of device_class: motion, that’s why it has different text to motion)

Wonderful! What happens if you trigger motion again during the 15 minutes cooldown?
Does it restart the countdown?

Yup - I’m in the livingroom and that one has never switched off yet -
My config:

  - trigger:
      - platform: state
        to: 'on'
        entity_id: binary_sensor.mymotiondetectorrule_cell_motion_detection
    binary_sensor:
      - name: Livingroom Presence
        state: 'on'
        auto_off: "00:15:00"
        device_class: occupancy

and the result:

1 Like

That seems to be exactly what I need then, thanks a lot!

1 Like

So I tried to implement this, but it’s not working as intended.
This is the template:

template:
  - trigger:
      - platform: state
        to: 'on'
        entity_id: binary_sensor.myfielddetector1_field_detection
    binary_sensor:
      - name: Hikvision garage human detected
        state: 'on'
        auto_off: 10
        device_class: motion

The new binary sensor indeed resets after 10 seconds, but because the trigger doesn’t change (the Hikvision camera only sends “IsInside: true” events, not “false”), the new binary sensor doesn’t go to on after the first time.

These are the ONVIF events the Hikvision camera is sending:


It only sends “IsInside: true” messages.

Compared to what my Amcrest doorbell does:

So I need the template sensor to retrigger when motion is detected, even if the state doesn’t change.
(do I make sense? :slight_smile: )
How can I fix this?

1 Like

You could try removing the to: so it reads:

      - platform: state
        entity_id: binary_sensor.myfielddetector1_field_detection

This will make it trigger not just on state change, but also when the attributes change, which should include last changed. But this is probably going to come with some downsides.

Docs here: Automation Trigger - Home Assistant

1 Like

Unfortunately that didn’t work.

I have been doing some more testing and have also enabled normal motion detection in the camera.
Those events also send a “false” message when motion ends, unlike the fielddetector (called “intrusion detection” by Hikvision).

So I have a few options I think:

  1. I use motion detection instead of intrusion detection
  2. I file a bug report at Hikvision, asking them to fix this, but I’ll be barking up a very big tree
  3. invest in an NVR, like blue iris and let that do all the detection and send events to HA… Although I must say the human detection on board the camera is spot on, I’ve had one false positive in months of usage.

I’ll probably do 1 and 2 for now. My goal was to do 3 anyway, once I’ve bought a server.

2 Likes

Well if nothing else, you at least know what you can do if you have a sensor that reports off, far too quickly for you.

I plan to go down route 3 too, because car headlights driving along the road outside the house set off the motion detection, especially in the bedroom - which is clearly no good for the occupancy detection.

Going to have a play tonight, but I am thinking using NodeRed with the onvif nodes contrib ( https://github.com/bartbutenaers/node-red-contrib-onvif-nodes ) may be a way to sort your problem, because we then send an MQTT message to Home Assistant, and your motion detector could be an MQTT sensor instead. With NodeRed we have more logic available so we can do stuff like send an OFF message from NodeRed after x number of seconds.

Hmm interesting. Installing node-red is on my shortlist to do. It will be a challenge once again because I’m using docker so no add-on store…

If node-red can intercept the onvif message and I can cut the onvif integration altogether, that’d work.

Node red can either run in docker or…

The one line installer designed for Raspberry Pi, but runs on all Debian based systems…

bash <(curl -sL https://raw.githubusercontent.com/node-red/linux-installers/master/deb/update-nodejs-and-nodered)

Is all you need to install it

I installed it last night in docker, but now I need to find a way to have it talk to HA. All guides are focusing on installing node-red via the add-on store, which I don’t have in HA container.

You can either create a long lived access token and communicate with Home Assistant that way, or the way I do it on all the other machines - is just use MQTT and create an MQTT sensor in my Home Assistant YAML.

@mobile.andrew.jones, I’ve been experimenting with the onvif nodes in Node-Red and have now filtered out the event message coming from the camera when intrusion is detected. I’m sure I can devise something that acts as a boolean sensor, going on when that message arrives and off after x seconds. Next step is to convert this to an MQTT message that HA can understand.

Quick question though, as I’m new to MQTT. Am I correct I need an MQTT broker like Mosquitto?
The alternative is using websocket? And that’s where the access tokens come into play? I have these nodes installed: node-red-contrib-home-assistant-websocket (node) - Node-RED
If you could give me those pointers, I will be able to figure it out, thanks.

By the way, do you also get these messages after some time?

I have installed the onvif nodes using:

npm install [email protected]

But somehow they don’t seem to stick around…