Trying to calculate things like power-use, rainfall, temperature-change per hour.
It doesn’t seem like there is any way to do this right now because the only change-over-time seems to be seconds which is always zero when I try to use it with such small numbers.
Examples:
Rain gauge goes from 1.2 to 1.4 inch in readings 15 minutes apart, its raining at a rate of 0.8 inches per hour.
Power meter goes from reading 12345.6 kWH to 12345.9 kWH across readings 2 minutes apart, I’m using 9kW average power
To accurately compute this, I have to use oldest/newest sample time (because the meters don’t beacon on a fixed interval) and then look at delta seconds between them per elapsed time and multiply that by 3600 to get “per hour” rate.
I tried this with the change_seconds and then multiply it by some number of seconds to extrapolate per-hour except it apparently doesn’t output high enough resolution past the decimal to give any readings for these slow changing values, its always zero, which is useless.
I also in some cases though want units other than per-hour. For some things like temp and rainfall change rate it can be useful to know like per-partial-hour or per-day.
So far, this is the only way I’ve found to make this work but now requires 4 separate stats sensors to compute the same value…
# Compute real time (approximate) usage by measuring the
# time difference between changes and amount of change
sensor:
- platform: statistics
entity_id: sensor.power_company_meter_buffered
name: "Power Company Meter Stats Change"
state_characteristic: change
sampling_size: 3
# age defines how long previous value waits without change assumed zero
max_age: '00:10:00'
- platform: statistics
entity_id: sensor.power_company_meter_buffered
name: "Power Company Meter Stats Count"
state_characteristic: count
sampling_size: 3
# age defines how long previous value waits without change assumed zero
max_age: '00:10:00'
- platform: statistics
entity_id: sensor.power_company_meter_buffered
name: "Power Company Meter Stats Newest"
state_characteristic: datetime_newest
sampling_size: 3
# age defines how long previous value waits without change assumed zero
max_age: '00:10:00'
- platform: statistics
entity_id: sensor.power_company_meter_buffered
name: "Power Company Meter Stats Oldest"
state_characteristic: datetime_oldest
sampling_size: 3
# age defines how long previous value waits without change assumed zero
max_age: '00:10:00'
- platform: template
sensors:
power_meter_current_power:
friendly_name: "Power Meter Current Power"
unit_of_measurement: "kW"
icon_template: hass:flash
value_template: >-
{% if states('sensor.power_company_meter_stats_count') | int(0) < 2 %}
0
{% else %}
{% set rate_calc = ( states('sensor.power_company_meter_stats_change')|float(0) /
((as_timestamp(states('sensor.power_company_meter_stats_newest')) - as_timestamp(states('sensor.power_company_meter_stats_oldest')))/3600)
) | round(2) %}
{% if rate_calc < -100 or rate_calc > 100%}
0
{% else %}
{{ rate_calc }}
{% endif %}
{% endif %}
And the output does align with what a clamp meter reports when I check it so its computing approximately correctly but its a really horrible convoluted way to do it now, and I don’t see any better way possible with what’s offered.
I’ve not had rain to attempt to rebuild my rainfall-rate sensors since the changes happened but I expect them to be similar if I reuse my old calculations…and have the same problem of small numbers meaning the built in change_seconds will always be zero. And same with temperature change numbers.