Use Case: sometimes you have sensors that are only allowed to increase in value, or decrease in value.
Example: a total kWh counter that takes the state of x individual kWh sensors and sums them together. Sometimes these sensors are unreliable: they can sometimes be unavailable, or report 0 values. As a result the total kWh counter can go down, which completely messes up the report generated by Utility Meter - Home Assistant.
Current Workaround: create an input_number to track the old value, and do a check in your sensor.template to see if the new value is less or more than the old value. The input number is updated using an automation. Requires a lot of confusing templating and an additional automation entry.
My request: add two filters that filter out values that are less than the previous value, or more than the previous value.
This would discard any new values that are less than the old value:
sensor:
- platform: filter
name: "filtered my total kwh"
entity_id: sensor.my_total_kwh
filters:
- filter: increasing_only
And discarding new values that are more than the old value:
sensor:
- platform: filter
name: "filtered some sensor"
entity_id: sensor.some_sensor_that_should_only_decrease
filters:
- filter: decreasing_only
How it would work in practice:
We would be able to reduce this:
input_number:
energy_consumption_old_state:
initial: 0
min: 0
max: 9999999999999999
sensor:
- platform: template
sensors:
energy_consumption:
friendly_name: "Energy Consumption"
unit_of_measurement: "kWh"
value_template: >
{% set new_state = (states('sensor.fibarowp9_electric_consumed_kwh') | float + states('sensor.fibarowp10_electric_consumed_kwh') | float + states('sensor.fibarowp11_electric_consumed_kwh') | float + states('sensor.fibarowp12_electric_consumed_kwh') | float + states('sensor.fibarowp13_electric_consumed_kwh') | float + states('sensor.fibarowp14_electric_consumed_kwh') | float + states('sensor.fibarowp15_electric_consumed_kwh') | float + states('sensor.fibarowp16_electric_consumed_kwh') | float + states('sensor.fibarowp17_electric_consumed_kwh') | float) | round(3) %}
{% if new_state >= states('input_number.energy_consumption_old_state') | float %}
{{ new_state }}
{% else %}
{{ states('input_number.energy_consumption_old_state') | float }}
{% endif %}
automation energy_consumption:
- alias: "Check new state of sensor.energy_consumption and store it in input_number.energy_consumption_old_state if it has increased"
trigger:
- platform: state
entity_id: sensor.energy_consumption
condition:
- condition: template
value_template: "{{ (states('sensor.energy_consumption') | float > states('input_number.energy_consumption_old_state') | float) }}"
action:
- service: input_number.set_value
data_template:
entity_id: input_number.energy_consumption_old_state
value: "{{ states('sensor.energy_consumption') | float }}"
To this:
sensor:
- platform: template
sensors:
energy_consumption:
friendly_name: "Energy Consumption"
unit_of_measurement: "kWh"
value_template: >
{{ (states('sensor.fibarowp9_electric_consumed_kwh') | float + states('sensor.fibarowp10_electric_consumed_kwh') | float + states('sensor.fibarowp11_electric_consumed_kwh') | float + states('sensor.fibarowp12_electric_consumed_kwh') | float + states('sensor.fibarowp13_electric_consumed_kwh') | float + states('sensor.fibarowp14_electric_consumed_kwh') | float + states('sensor.fibarowp15_electric_consumed_kwh') | float + states('sensor.fibarowp16_electric_consumed_kwh') | float + states('sensor.fibarowp17_electric_consumed_kwh') | float) | round(3) }}
- platform: filter
name: "filtered energy consumption"
entity_id: sensor.energy_consumption
filters:
- filter: increasing_only
Based on my experience here: Only allow a sensor value to increase - #10 by _Brian