The problem is, sensor.energy_consumption sometimes decreases in value when a sensor is unavailable. This messes up the reporting from utility meter. Is there any way to ensure that sensor.energy_consumption can only increase in value? Or can I make a new sensor using a template that only increases in value?
Ideally, I would have some kind of template which compares the new value to the previous value, and if the new value is less than the previous value, discard it.
You need to add an availability_template to your template sensor.
The simplest way would be to multiply all the dependant sensors together and check the result is > 0. If any of the sensors are unavailable the |float filter for that sensor will return 0, thus the whole multiplication will be zero.
Unfortunately, this does not work because sometimes the value of sensor.fibarowpXY_electric_consumed_kwh is actually 0 (some wall plugs are not yet used. thus 0 is correct).
which should be simple to adapt for upwards-only logic. Uses an input_number as a proxy for a sensor — you can always create a template sensor from it if needed.
Yes, and then adjust it depending on the incoming new value. I’ve just remembered I do something closer to what you want with my house “maximum power” record:
Again, this is an input_number, and it is storing the maximum house power draw, updated via this automation which triggers whenever the prior maximum is exceeded. I wrote this some while ago: should probably have the template in the trigger instead of the condition, and there’s no need for data_template any more, as data works fine.
Thank you so much! I was able to come up with a solution based on yours.
Step 1: Create an input number to store the old sensor value (input_number.energy_consumption_old_state)
Step 2: Create an automation to update the input number input_number.energy_consumption_old_state only when the sensor value of sensor.energy_consumption increases
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 }}"
Step 3: create a sensor (sensor.energy_consumption) with a value template that checks the old state stored in input_number.energy_consumption_old_state, if the new value is smaller than the old value, keep using the old value. If not, update to the new value.