Hi,
I have a sensor that gives me total energy consumption (kWh). I grab its value every 15 minutes, and of course value is getting higher and higher. I’d really love to display it through a bar graph in mini-graph card lovelace UI, but it doesn’t make really sense to have a bar graph with endless growing bars. So, I would like to display difference between 2 measures.
I guess I have to create a template sensor based on the original one (am I wrong?), but I don’t know how to get previous values from original sensor to make calculation. Actually last ten measures would be better, in order to do a sliding average.
Could anyone help me to progress on that?
Thank you
Ok, I have tried to solve the difference between 2 following values with a statistics and a template sensor, this way:
- platform: statistics
entity_id: sensor.c_l2_energy
name: c_l2_energy_diff
sampling_size: 4
- platform: template
sensors:
energy_diff:
value_template: "{{ sensor.c_l2_energy_diff['change'] }}"
unit_of_measurement: 'kWh'
where c_l2_energy is the original sensor. Unfortunately energy_diff sensor returns no value, when c_l2_energy_diff displays all its attributes fine, including change. I also have tried with:
value_template: "{{ state_attr('sensor.c_l2_energy_diff', 'change') }}
but no way. Does anybody know what is wrong with this solution?
Thanks
your spacing is off
- platform: template
sensors:
energy_diff:
value_template: "{{ state_attr('sensor.c_l2_energy_diff', 'change') }}"
unit_of_measurement: 'kWh'
But that’s not going to get you a value if the ‘change’ attribute doesn’t exist. And it likely doesn’t exist. You actually would need to subtract the current value from the previous value. Which you cannot do in a template sensor. You have to use the statistics sensor for something like that.
Also, there is a whole component for this:
Ok thanks, I had seen that change is an attribute of statistics sensor, here sensor_statistics. But anyway, my original question was actually subtract current value from previous one.
Could you give be more precise on that? How can I get the previous value of a sensor ?
Ok, if that’s a valid attribute then this should work.
- platform: template
sensors:
energy_diff:
value_template: "{{ state_attr('sensor.c_l2_energy_diff', 'change') }}"
unit_of_measurement: 'kWh'
Hurray, I got the value!
Strange thing is that I had to track for c_l2_energy_diff_mean instead of c_l2_energy_diff. No clue from where it comes from, but I found the new _mean suffix sensor in states page (<>). So right config is following:
- platform: statistics
entity_id: sensor.c_l2_energy
name: c_l2_energy_diff
sampling_size: 4
- platform: template
sensors:
energy_diff:
value_template: "{{ state_attr('sensor.c_l2_energy_diff_mean', 'change') }}"
unit_of_measurement: 'kWh'
Thanks for your help, Petro
PS: Actually, as I understand the source code of /components/statistics/sensors.py under github, init method, line 85:
if not self.is_binary:
self._name = '{} {}'.format(name, ATTR_MEAN)
means that mean is really added to sensor name. I think this information would be worth in the documentation?