poudenes
(Poudenes)
October 14, 2020, 1:17pm
1
Hi All,
IS there a way or template script what i can use to calculate the total of entities state?
I want combine all Zwave kWh usages in 1 sensor.
sensor.node_3_energy (4kWh)
sensor.node_4_energy (23 kWh)
sensor.node_5_energy (19 kWh)
sensor.node_6_energy (7 kWh)
sensor.node_7_energy (9 kWh)
All together the sensor will show 62 kWh and update all the time because the sensors will change.
The history_stat is not what i want. That show only history of 1 entitiy
The Min/Max sensor should get you what you want: https://www.home-assistant.io/integrations/min_max/
sensor:
- platform: min_max
entity_ids:
- sensor.node_3_energy
- sensor.node_4_energy
- sensor.node_5_energy
- sensor.node_6_energy
- sensor.node_7_energy
The other solution would be a simple template sensor:
sensor:
- platform: template
sensors:
total_energy:
value_template: "{{ float(states('sensor.node_3_energy')) + float(states('sensor.node_4_energy')) +float(states('sensor.node_5_energy')) +float(states('sensor.node_6_energy')) +float(states('sensor.node_7_energy')) }}"
poudenes
(Poudenes)
October 14, 2020, 1:42pm
3
Thanks. WIth your second option snow i understand the float option learning every day…
1 Like
petro
(Petro)
October 14, 2020, 1:47pm
4
Do not use that template, it will cause errors on startup. Using float()
is not suggested because it’s not error proof. Using the | float
filter is.
"{{ states('sensor.node_3_energy') | float + states('sensor.node_4_energy') | float+states('sensor.node_5_energy') | float +states('sensor.node_6_energy') | float +states('sensor.node_7_energy') | float }}"
{{ float('z') }} # Returns z, will cause errors when adding to a number.
{{ 'z' | float }} # Returns 0, will not cause errors.
@code-in-progress FYI
1 Like
Thank you sir. Much appreciated and noted. I guess I need to update some of my templates that I’m still using the float()
function in.
poudenes
(Poudenes)
October 14, 2020, 1:54pm
6
To understand it as well. Now you have float
just itself that’s 0
and put the states
to it with a +
So if the state is not ready the float will be 0 until state is ready and update the total?
123
(Taras)
October 14, 2020, 2:03pm
7
Alternative way of calculating the sum of several entity states:
- platform: template
value_template: >
{{ [states.sensor.node_3_energy, states.sensor.node_4_energy,
states.sensor.node_5_energy, states.sensor.node_6_energy,
states.sensor.node_7_energy] | map('float') | sum }}
EDIT
Corrected version:
- platform: template
value_template: >
{{ [states.sensor.node_3_energy, states.sensor.node_4_energy,
states.sensor.node_5_energy, states.sensor.node_6_energy,
states.sensor.node_7_energy]
| map(attribute='state') | map('float') | sum }}
1 Like
petro
(Petro)
October 14, 2020, 2:07pm
8
A filter is applied to the item that precedes it
{{ 'z' | float }}
^
|
float is applied to this
2 Likes
I love seeing @123 and @petro display their genius templates. Seriously, you guys KILL it when it comes to templating.
1 Like
123
(Taras)
October 14, 2020, 2:10pm
10
petro is way ahead of me. I started using Home Assistant 2 years ago and petro helped me sort out my first template.
1 Like
poudenes
(Poudenes)
October 14, 2020, 2:12pm
11
@petro thanks for the info. He is helping me out also many times. And every time i learn more and more!!
1 Like
Don’t shortchange yourself. I’ve been using HA as long as you have and I still learn something new every time I see you post one of your templates or answer a question about one.
I’m convinced @petro is an evil genius though
petro
(Petro)
October 14, 2020, 2:18pm
13
I think I only started helping people a few months before you. I was a lurker for way too long. I don’t see a difference in our skills.
1 Like
Agreed!
It’s ridiculuous, I started 1 year earlier and I’m not even close to the skill level of Taras and Petro
1 Like
PMaksymiuk
(Piotr Maksymiuk)
October 15, 2020, 9:19am
15
This is absolutely phenomenal! I’d also like to ask then - how would one also make a nice availability_template for a sum of these?
Edit: It seems it doesn’t work for me, returns 0.0 Probably because when I use {{ states.sensor.shellymeter_0_energy }} i get back
<template state sensor.shellymeter_0_energy=60.52; unit_of_measurement=kWh, friendly_name=ShellyMeter 0 Energy, device_class=energy @ 2020-10-15T11:24:08.332922+02:00>
instead of just the value
petro
(Petro)
October 15, 2020, 12:24pm
16
Yes, the template is missing a piece
{{ [states.sensor.node_3_energy, states.sensor.node_4_energy,
states.sensor.node_5_energy, states.sensor.node_6_energy,
states.sensor.node_7_energy] | map(attribute='state') | map('float') | sum }}
and the availability template
{% set items = [states.sensor.node_3_energy, states.sensor.node_4_energy,
states.sensor.node_5_energy, states.sensor.node_6_energy,
states.sensor.node_7_energy] %}
{{ items | count == items | rejectattr('state', 'in', ['unknown','unavailable']) | list | count }}
1 Like
123
(Taras)
October 15, 2020, 12:33pm
17
Yup, in my haste, I missed a step. The original version I had used for testing consisted of a list of strings. When I converted it to use states objects, I forgot to filter it with map(attribute='state')
. petro’s post has the correction and more.
1 Like