esphome / feature-requests

ESPHome Feature Request Tracker
https://esphome.io/
422 stars 27 forks source link

Add relative (percentile) change to sensors. #1788

Closed shtorm93 closed 2 years ago

shtorm93 commented 2 years ago

Describe the problem you have/What new integration you would like Add relative (percentile) change to sensors.

Please describe your use case for this integration and alternatives you've tried: Home automation systems like KNX have an option in sensors to send measured value periodically but also on relative change. This eliminates unnecessary traffic and increases precision when the sensor reading changes drastically but for a short period of time.

Additional context For example, "PZEM-004T V3 Energy Monitor" as well as other sensors have "update_interval: 60" setting. In this case if there is an overload in the 60 second period between updates - the overload value will not be sent to the home assistant. Same with short lived temperature changes from temperature sensors. Changing this value to something like 1 or 2 second will fix the issue in most situations, but this solution will bring more unnecessary traffic and duplicate measurements in the database.

Introducing another option of "send_on_relative_change" with an option of sending the sensor value on a specific % change from the previous reading will fix these issues. In this case we can set "update_interval: 600" to get a periodic reading every 10min and "send_on_relative_change: 5" so we get a reading on changes more than 5%.

nagyrobi commented 2 years ago

Not easy request. update_interval refers not only to how often data is pushed to HA, but also how often the sensor itself is queried by the ESP MCU. Many I2C and 1wire sensors are sent to sleep during queries, and they are woken up at update_interval periods to request the data to minimise energy consumption. Thus measurements are actually not performed more often than update_interval, so there's no real data to calculate inner percentage from.

What you could do to mimic your behaviour is to set the sensor as internal with a small update_interval, and calculate with lambdas what you want, and manually publish more slowly (eg. with an incremental counter) to a template sensors which could be exposed to HA - this way you avoid unnecessary network traffic between the node and HA but still get intermediate values which you can use.

Note that feasibility differs from sensor to sensor - there are sensors which have to be left alone certain amounts of time to avoid false measurements due to self-heating etc.

shtorm93 commented 2 years ago

Then ideal option whould be to have separate update_interval, send_interval and send_on_relative_change.

Adding this functionalyty manually and then excluding this variable from the database manually to every sensor is not very appealing.

ESP checks the value of the sensor on update_interval, then sends it if send_interval has passed or if sensor change is higher then send_on_relative_change percent.

Is this dificult to implement?

nagyrobi commented 2 years ago

As I said, it requires to be implemented for each sensor type ESPHome supports, depending on the capabilities of each sensor. It's not a universal change that can be applied in one go to all sensors. That's a lot of work, pretty unlikely to be done centrally.

But can be achieved by yourself with the power of lambdas, for the sensors you need this capability.

nagyrobi commented 2 years ago

I also suggest to read what filters can do: https://esphome.io/components/sensor/index.html?highlight=filter#sensor-filters Maybe you can find a suitable filter for your needs, with a fast update interval.

shtorm93 commented 2 years ago

I understand. Thank you.