Is there an easier way? (I have over 30 power-entities).
If this can be something dynamically that would even be better. That way I can’t forget to add a new powerplug when it is added.
Tricky:
I have sensors that end in “_power_management”
I have sensors that contain “_battery_powered”
I tried the solution that is proposed in the other topic.
It is working (I think)
Is there a way to see what entities are taken as input ?
It is responding with a delay, changing the status of an entity takes about 30 seconds to reflect in the sum of this newly created sensor. Is there a way to update it every 5 seconds instead of 30 (?) seconds ?
this is a valid method, but, it is expensive. also, as I have experienced using something like that before, depending on your instance and startup sequence, it might need some tinkering to create the group only after all sensors are available.
for this particular reason, I made the group manually (we dont add sensors all the time, so this group is rather stable), and use this simple template sensor:
There is absolutely a difference for me in the speed that the sensor is updated.
I created a group as proposed by @Mariusthvdb. This updates instant.
When using the template what @VDRainer told me to look at, it takes over 30 seconds before the sensor changes.
There is an absolute advantage in having all ‘_power’ entities (and not forgetting one) automatically, but in the end it’s what you see in the UI. So I go for manually creating the groups (one for the zwave plugs, and one for the entities that are imported from OpenHAB, where all my QBus switches and lights are defined in).
You could build a template that iterates over all the sensor entities, looks for the ones where the entity_id matches some pattern and then accumulate a total.
However, I think this will be expensive since the template will get evaluated whenever any sensor changes value. Maybe that’s OK for your use-case.
You can play around in the “TEMPLATE” developer tool (like I did) and see if you can get it to work by matching the entities you’re interested in. For example, I have some power sensors, and this adds them all up:
{% set ns = namespace(total = 0) -%}
{% for pwr in states.sensor -%}
{% if pwr.entity_id | regex_search('^sensor\.power_.*$') -%}
{% set ns.total = ns.total + pwr.state | float -%}
{% endif -%}
{% endfor -%}
{{ ns.total | round(1) }}
This isn’t as elegant as @Mariusthvdb’s solution, being a more procedural approach to the problem. Perhaps the jinga2 regjectattr() filter can be applied in there to so something similar…
This is a proof of concept; you’ll need to tweak to meet your particular circumstances. In particular, you’ll need to change the pattern to something like regex_search('^sensor\..*_power$') to match _power appearing at the end of the entity_id.
I think it’s probably a better bet to have the ones you’re interested in in a group (or even enumerated in the template) so the template evaluation doesn’t fire every time any sensor entity changes state.
do remember that the solution for creating a group at startup following the template you set, and then using the expand() template on that group might be best if you want automatic group setup.
This would have the advantage of not having to look at it at all, when adding new sensors. Since this group is created in the HA states, it should behave identical from that moment on. Not causing any delays at all.
Still, as I have experienced, it isn’t as smooth and reliable as a manually created group, (which survives restarts added to that)
since all your Watt power sensors use the same unit, it should do nothing at all In which case it is superfluous and takes a minute bit of processing power/time. Negligable, probably, but still…
No, I grouped kWh sensors there too. Thats why I have W selection there But that part is not in this example. With this specific code, it can be safely removed of course.
I created a similar template sensor using @Mariusthvdb his example. The sum works fine and I am able to calculate the ‘uncounted’ power usage using those sensors. However, and not sure if others experience this too, if you try to calculate the current power usage in Watts, since it is not measured over a period like kWh, my total power usage goes in to ‘minus’ sometimes due to (I think) the DSMR (smart meter) giving values at different times than my shelly devices. And that kind of makes the whole purpose defeating the way how I wanted to use this. I think with energy, the only way to really look at measurable results you will have to use the energy sensors in stead of the power sensors and calculate consumption over time.