Change update intervals depending on value of Sensor


I wish to have a sensor publish High resolution data when within a certain value set, and revert to a more manageable update rate the rest of the time.

One clear example is my water pump for the home.
It has an analog pressure Sensor fed through am ads1115 ADC, and a pulse flow meter fed through the pulse counter component.

When nobody’s consuming water, both the pressure and flow are stable, and therefore an update resolution of 1min or more is fine.

But when you open a faucet or a shower, I want 1s or less resolution on my data to from both pressure and flow.

Something like this:
When flow is > x then update interval = 1s
else update interval = 1 min.

Se applies to current sensing on motor controllers and several other cases.

(BTW, The sensor setup works great and is not a problem. Just the modification of the update interval of the sensors)

Thanks in advance!!

While I can’t contribute to a solution to your issue i am curious what you’re trying to achieve with this?

While not the OP, here’s a similar scenario:

Garage door is connected to a potentiometer, which is closed 99% of any given day. While closed there’s no sense reading/sending/recording the same value (closed) constantly, so a longer interval (ex: 60s) is good. But, when the door isn’t closed (opening/open/closing) getting the pot’s value at a short interval (ex: 1s) is needed to track position and action as it’s happening.

Same problem, different use case: I have an RGB light entity with a bunch of different effects defined. Some of them update very slowly - 10s between color updates. Other patterns update every 50ms.

I don’t much care what color is displayed/reported to home assistant when running an effect - the name of the effect is plenty good enough. If I do send 20 light updates per second it makes my phone app nearly unusable.

I have a similar need. That’s how I ended up here.

An option (which I have not yet explored) could be to log the sensor data on the esp at a high rate. And then send multiple values at once to HA over mqtt:

ESP collects 100 (or 1000) datapoints (and timestamps) over 5min. And then sends the 100 values to HA.

Would this be feasible? I basically want to end up in Excel with a data point every tenth of a second.

There are multiple ways to solve this. One solution is to create a template sensor with an update_interval: 1s. In the lambda of that sensor you decide whether the flow is high enough or whether 60 seconds have passed. If not, you do not publish, i.e. by returning {} from the lambda.

Here is an untested example (most likely with syntactical errors as I am no C++ expert):

  - platform: template
    name: "Flow Sensor with rate limit"
    update_interval: 1s
    lambda: |-
      static int no_publish_cnt = 0;
      if (id(my_flow_sensor).state < 5 && no_publish_cnt++ < 60) {
        return {};
      } else {
        no_publish_cnt = 0;
        return id(my_flow_sensor).state;

… or simply use a Delta filter on your sensors.

1 Like