Help with configuring Trend sensor

I’ve created my first Trend sensor in my HA config YAML file with the following:

binary_sensor:
  - platform: trend
    sensors:
      oil_drop:
        entity_id: sensor.distance_to_oil
        sample_duration: 30
        max_samples: 30
        min_gradient: 0.01

This is with the intention of detecting if the level of heating oil in my tank starts to drop at a rate much higher than normal usage rate with the heating on. This will hopefully indicate a sudden leak or theft for example.

In the event of one of these happening, the distance from the oil surface to the sensor will increase, so I have made the min_gradient positive, and as a starting point, based it on a change of 0.01m per second (guessing the rate would be higher than this in the event of a theft/being siphoned out).

With the sensor on my desk plugged into my ESPhome board I’m trying to get this Trend sensor setup and tested before fitting the level sensor to the tank, but I’m a bit unsure about the results I am seeing.

Upon a reboot of my ESPHome device, the Trend sensor shows a status of ‘unknown’ for around a minute or so. Am I correct in in my understanding that this would remain in an unknown state until there is at least some change in my distance reading from the sensor, at which time it will stay in an ‘off’ state until the readings create a gradient of at least 0.01? When this happens, how long will the Trend sensor remain in the ‘on’ state? Until the gradient from 30 samples goes below 0.01? As my sensor takes a reading every 1 second (just for testing purposes), then it will take a minimum of 30 seconds for the sensor to go off again if the gradient has reduced?
If that’s the case, I’m not sure what’s wrong, as during testing, and invoking a positive change in distance, my sensor showed a state of ‘on’ but remained in this state for over an hour - even though the distance did not change at all during this hour.

I haven’t found many useful examples of this sensor when searching on here or google, so perhaps it isn’t that widely used. I managed to do some testing and with a lot of trial and error have settled on what I think will be a suitable config:

binary_sensor:
  - platform: trend
    sensors:
      oil_drop:
        entity_id: sensor.distance_to_oil
        sample_duration: 20
        max_samples: 20
        min_gradient: 0.002

If I’ve worked this out correctly this equates to 0.02m increase in distance from the oil to the sensor in a time of 10 seconds. This is much more than the rate of use of oil under normal circumstances, but hopefully a steep enough gradient to catch a significant leak or theft of oil via siphoning etc.

The binary sensor once triggered stays in an ‘on’ state until there is a change in the distance reading from the distance sensor, so when I was triggering this previously and then leaving the sensor in position for an hour, that’s why the binary sensor stayed on, as there was no change in the distance.

Now I can set up an alert via an automation to trigger a push notification or similar when this sensor is triggered, and I can get the sensor fitted in place finally and start gathering real data about the level and remaining tank %.

1 Like