Totalize all entities with '_power'

Hi

All the entities for the different energy-meters end in ‘_power’.

I want to create a sensor that is the total of all these entities.

This is how I do it manually for just 2 sensors:

- platform: template
  sensors:
    em_som_kookplaat:
      friendly_name: 'Kookplaat Power'
      entity_id:
        - sensor.em_kookplaatl1_power
        - sensor.em_kookplaatl2_power
      value_template: "{{ (states('sensor.em_kookplaatl1_power')|float|abs + states('sensor.em_kookplaatl2_power')|float|abs)|round(0) }}"
      unit_of_measurement: "W"

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 only want these ending in “_power”

Thanks

Take a look at this topic.

1 Like

Yeah, I do exactly the same thing. For both power and kWh as seen in that topic.
If you need additional info, just ask.

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 ?

You can see all entities in a group.
I dont have any delay on template sensor, it gets updated right away.

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:

      sensors_huidig_verbruik_summed:
        friendly_name: Sensors huidig verbruik summed
        unit_of_measurement: Watt
        icon_template: mdi:plus-box-outline
        value_template: >
          {{expand('group.switches_sensors_actueel')
             |map(attribute='state')|map('int')|sum}}

just an alternative…

1 Like

Hi @Tomaae

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).

Thank you all !

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.

Here is my current setup for this. But I doubt there is anything that could cause such delay.

Automation for creating group:

- alias: 'Update Group - Power'
  trigger:
  - platform: homeassistant
    event: start
  - platform: event
    event_type: 'call_service'
    event_data:
      domain: 'group'
      service: 'reload'
  - platform: event
    event_type: 'component_loaded'
    event_data:
      component: esphome
  action:
  - service: group.set
    data_template:
      name: 'Energy - Power'
      object_id: energy_power
      entities: >
          {% set ns = namespace(entities=[]) %}
          {% for s in states.sensor if s.object_id.endswith('_power') and s.attributes.unit_of_measurement == "W" and not s.attributes.meter_period %}
            {% set ns.entities = ns.entities + [ s.entity_id ] %}
          {% endfor %}
          {{ ns.entities }}

Template sensor:

sensor:
- platform: template
  sensors:
    current_consumption:
      friendly_name: Current Power
      unit_of_measurement: W
      icon_template: mdi:transmission-tower
      value_template: >
        {{ expand('group.energy_power') 
        | selectattr('attributes.unit_of_measurement', 'equalto', 'W')
        | rejectattr('state', 'in', ['unavailable', 'unknown'])
        | map(attribute='state') | map('float') | sum | round(2) }}
1 Like

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)

this seems superfluous? since you already have that in the group.set service.

Yes, its there because I had multiple groups merged into one. I just left it there in case I want to do more with it, since it does not harm.

since all your Watt power sensors use the same unit, it should do nothing at all :wink: 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 :slight_smile: 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.