I’m using an integration that does frequent state changes, and the states are delta values (of water volume) since the last update. What I really want is the total, but not at the resolution (and thus frequency) the integration is reporting the values.
These states, along with my dependent template and utility meter sensors were a significant percentage of my states table.
I still want to total up all the (small) volumes, but only want to update my total sensor when the sum is greater than some value, then reset the running sum.
Is there a better way to do it than this?
recorder:
exclude:
entities:
- sensor.droplet_total_excluded
- sensor.droplet_e67c_water
template:
# Keep a total of all the Droplet websocket updates, but exclude from recorder
# And reset on an event
- triggers:
- trigger: state
entity_id: sensor.droplet_e67c_water
- trigger: event
event_type: clear_droplet_sum
id: clear
sensor:
- name: Droplet Total Excluded
unique_id: droplet_total_excluded
unit_of_measurement: "gal"
device_class: water
state_class: total
state: >
{{
0 if trigger.id == 'clear'
else states('sensor.droplet_e67c_water') | float(0) + this.state | float(0)
}}
# Update the state if the above sum is over the volume defined below and then clear sum
# Otherwise, no state change.
- triggers:
- trigger: state
entity_id: sensor.droplet_total_excluded
not_to:
- unavailable
- unknown
actions:
- variables:
cur_sum: "{{ states('sensor.droplet_total_excluded') | float(0) }}"
- if:
- condition: template
value_template: "{{ cur_sum > 0.05 }}"
then:
- event: clear_droplet_sum
sensor:
- name: Total House Gallons
unique_id: droplet_total_house_gallons
unit_of_measurement: "gal"
device_class: water
state_class: total
state: >
{{
( cur_sum + this.state | float(0) ) | round(2)
if cur_sum > 0.05
else this.state
}}
The reason I want to keep the running sum (verses just adding and using: |round(1) is to record small flows over a long time (like a leak).
One (rare edge case) issue is the template’s run in single mode, so I cannot be guaranteed that the triggers won’t interfere with each other.
The background is this:
I have a Droplet ultrasonic water meter that has a local websocket interface and an HA integration.
The devices sends periodic updates of how much water the device measured since the last update. It reports in milliliters to two decimal places.
With water in the house turned off it reports changes every 20 to 30 seconds (background noise or water “sloshing” in the pipe). With water running it reports usage every 3 to 4 seconds. This generates a lot of often-very-small state changes and a lot of updates to the states table.
The Droplet integration converts the ml readings to liters, and I’m using gallons and I don’t really care about more than tenth or so of a gallon change.