Creating a derivative/integral helper from a counter helper entity

Hi,
Is there any way to create a derivative (or Riemann integral, for that case) helper from a counter helper?
I have the following use case:
My Foscam Camera has a built-in motion detector, and it can send a simple GET request when motion is detected.
I wanted to use this in order to show in HA when motion is detected, and created a counter helper, which is incremented by an automation every time when a webhook is called by the camera.
I already created a template binary sensor which shows if there was movement in the last minute, but I also wanted to make a derivative to show the rate of movement, i.e. in the last minute/hour/etc.
Is there some better way to do this which I missed, or a reason why this isn’t possible to choose counter entities for a derivative helper?
Thanks!

You could create the binary sensor directly from the webhook:

configuration.yaml

template:
  - trigger:
      - platform: webhook
        webhook_id: long_uuid
    binary_sensor:
      - name: "Movement"
        state: "{{ trigger.json.some_key == 'whatever' }}" # or trigger.data if not a json payload
        device_class: movement
        delay_off: 60 # if you want it to be active for a minimum of one minute

Then you can feed this sensor to a history stats sensor to see how much movement there was in the last hour.

sensor:
  - platform: history_stats
    name: Movement in last hour
    entity_id: binary_sensor.movement
    state: "on"
    type: time # or count
    duration: 
      hours: 1
    end: "{{ now() }}"
1 Like

Thanks for the quick and helpful reply!
I just did a small few changes for the binary_sensor,
using “auto_off” instead of “delay_off” and “motion” as the device class.
Then I used a history_stats sensor to count the total number of events, and an additional derivative helper on that history_stats sensor to show the amount of movement events per hour.

This is off-topic for the question you asked but would you mind giving an explanation of how you set up the GET request for the camera to send motion alerts?

Right now I’m using polling to check the motion activation status of the camera so it’s not very resource efficient. Using the camera to send the motion event itself would be way better.

Thanks in advance.

I installed the “Foscam VMS” on my Windows computer,
from there you can connect to the camera and modify the detector settings.
The GET request is defined here:

Thank you.

Is that only from the Foscam VMS system or does that capability reside on the camera itself?

IOW, do I need to keep Foscam VMS running at all times to get the GET notification from the camera or can I close that app out once I set up the webhook alarm link?

Lastly, (maybe…) I assume the concealed part of the screenshot is the webhook id created in your HA instance?

The capability is in the camera itself, I only use Foscam VMS to set it up and then I close it again.
Maybe there is a different way to do that, but I haven’t looked deeper into it.
And yes, the concealed part is the webhook id from HA, and the IP address is of course just the IP of my HA host.

1 Like

I looked at the camera settings from the camera webserver itself and didn’t see the ability to set that there so it must be only configurable via the VMS.

I’ll get it setup there and see how it goes.

Thanks again.

What is the webhook payload you are using to configure the binary sensor state template?

I have the automation set up to increment the counter and that is working fine.

But I don’t see how to find out the webhook data to put into the template binary sensor.

could you post the config for you binary sensor please?

I have the binary_sensor which turns on when the webhook is called and stays on for 15 seconds,
and the count sensor which has the total number the binary_sensor was on.

template:
  - trigger:
    - platform: webhook
      webhook_id: id
      allowed_methods:
      - GET
      local_only: true
    binary_sensor:
    - name: "Camera Movement"
      state: "{{ true }}"
      device_class: motion
      auto_off: 15

sensor:
  - platform: history_stats
    name: Camera Movement Count
    entity_id: binary_sensor.camera_movement
    state: "on"
    type: count
    start: "{{ 0 }}"
    end: "{{ now() }}"
1 Like

Ok, so you aren’t actually pulling any data from the GET webhook request to determine whether the binary sensor turns on. You are just telling the binary sensor to turn on when the webhook GET is received no matter the payload.

that makes perfect sense.

again…thanks, again.

:grinning_face_with_smiling_eyes: