Template w/Multiple Sensor Values

I’ve been researching for an hour, seen a dozen ways to do this, and none have worked well.

I’d like a template sensor to add the values of every entity that ends in _power. That’s all.

Thanks in advance!

{{ states.sensor
  | selectattr('entity_id', 'search', '_power$')
  | map(attribute='state')
  | map('float', 0) | sum }}

Thank you! How do I enter this in configuration.yaml?

You have to make a template sensor.

Paste this directly into configuration.yaml

template:
- sensor:
  - name: Power Sum
    state: >
      {{ states.sensor | selectattr('entity_id', 'search', '_power$') | map(attribute='state') | map('float', 0) | sum }}

Keep in mind that it will only update at most once per second. If you have a slow system and alot of sensors, it will hog resources as well. .


The only way to avoid this is by using an automation to create a group, and then use the group to perform the calculation

Automation

- alias: Create power group at startup
  trigger:
  - platform: homeassistant
    event: start
  action:
  - service: group.set
    data:
      object_id: power_sensors
      entities: >
       {{ states.sensor | selectattr('entity_id', 'search', '_power$') | map(attribute='entity_id') | list }}

Template sensor.

template:
- sensor:
  - name: Power Sum
    state: >
      {{ expand('group.power_sensors') | map(attribute='state') | map('float', 0) | sum }}
1 Like
template:
  - sensor:
      - name: "Total Power"
        state: >
          {{ states.sensor
            | selectattr('entity_id', 'search', '_power$')
            | map(attribute='state')
            | map('float', 0) | sum }}

Depending on your specific needs, you may wish to add a device_class, unit_of_measurement, maybe state_class, etc. Refer to the documentation for Template Sensor.

1 Like

Amazing. Thanks so much!

Any ideas why this is happening?

Logger: homeassistant.helpers.template_entity
Source: helpers/template_entity.py:312
First occurred: 4:02:30 PM (1 occurrences)
Last logged: 4:02:30 PM

Template loop detected while processing event: <Event state_changed[L]: entity_id=sensor.energy_usage_w, old_state=<state sensor.energy_usage_w=232.19; unit_of_measurement=W, friendly_name=Energy Usage W @ 2022-09-15T16:02:28.988878-05:00>, new_state=<state sensor.energy_usage_w=346.19; unit_of_measurement=W, friendly_name=Energy Usage W @ 2022-09-15T16:02:29.994026-05:00>>, skipping template render for Template[{{ states.sensor | selectattr(‘entity_id’, ‘search’, ‘_power$’) | map(attribute=‘state’) | map(‘float’, 0) | sum }}]

your entity_id ends in _power and the template is finding itself.

Rename the Template Sensor to Power Total or modify the template to reject sensor.total_power.

The entity ID for this sensor is sensor.energy_usage_w

not if you used the solution that 123 posted

Even if I rename from .energy_usage_w to power_total, the template sensor is only looking for “_power”. Does it not include the underscore in the search?

Your original response was

And you were using

We were simply answering your question as to why you were getting that error. Because with a name: total power, you will get _power in your entity_id.

Since I named the sensor “energy_usage_w” after converting the example to my use case, I’m still getting that error. That’s what I’m trying to convey. The entityid doesn’t contain the word power at all.

As an experiment, copy-paste the following template into the Template Editor and let us know what it reports:

          {{ states.sensor
            | selectattr('entity_id', 'search', '_power$')
            | map(attribute='entity_id')
            | list }}

I wonder if it’s incorrectly detecting the template loop because we’re trying to do this in a single step. Try this:

        state: >
          {% set sl = states.sensor|selectattr('entity_id', 'search', '_power$') %}
          {{ sl|map(attribute='state')|map('float', 0)|sum }}
1 Like

That shouldn’t make a difference. It’s most likely occurring because he’s using the entire sensor domain.

He should make a group, then exapnd the group

- alias: Power Group Creator
  trigger:
  - platform: homeassistant
    event: start
  action:
  - service: group.set
    data:
      object_id: power
      entities: "{{ states.sensor|selectattr('entity_id', 'search', '_power$') | map(attribute='entity_id') | list }}"

Then the template sensor…

template:
  - sensor:
      - name: Energy Usage W
        state: "{{ expand('group.power') | map(attribute='state') | map('float', 0) | sum }}"
1 Like

I tried this, but the automation isn’t adding any sensors to the group:

EDIT: I didn’t catch the line break. This is working now. Thanks!

alias: "Service: Power Sensor Group"
description: ""
trigger:
  - platform: homeassistant
    event: start
condition: []
action:
  - service: group.set
    data:
      object_id: power
      entities: >-
        {{ states.sensor|selectattr('entity_id', 'search', '_power$') |
        map(attribute='entity_id') | list }}
mode: single

Spoke too soon. I see the correct entities in the new group. Some are unavailable, which is causing the status of the group to be Unknown, and therefore the new sensor isn’t updating. Possible to filter out Unknown status entities?