Template Sensor failing to load

Hello everyone,

So I have been trying to configure energy monitoring in my house, I’m using shelly 1pm and shelly 2.5 and since they report energy in Watt-Min, I decided to use the integration sensor with the power consumption measured in Watts my shelly.

All in all, this seems fine, but when I created a sensor called sensor.total_energy and used template sensor to map and sum all sensors with KWh unit_of_measurement, home-assistant seems to be failing to load it.

When I type the exact same template_value on the template_value tester, it works fine, any tips anyone?

- platform: template
  sensors:
    total_energy:
      friendly_name: 'Total Power Consumption'
      unit_of_measurement: "KWh"
      value_template: "{{ states.sensor|selectattr('attributes.unit_of_measurement', 'equalto', 'KWh')|rejectattr('entity_id', 'equalto', 'sensor.total_energy')|map(attribute='state')|map('float')|sum|round(3) }}"

ERROR:

Log Details (WARNING)

Template sensor total_energy has no entity ids configured to track nor were we able to extract the entities to track from the value template(s). This entity will only be able to be updated manually.

You have to use this format instead:
“{{ state_attr(‘sun.sun’, ‘elevation’) }}”

See the considerations/startup section of this page: https://www.home-assistant.io/components/template/

Sorry I’m not more helpful but when I had to change mine I had to play with the location of float and round a little to get it to calculate properly. I’m sure someone here knows the proper way but it would take me a little fooling around to get it.

You need to specify the entity ids the template uses so that HA knows what to monitor for changes and when to update the template. As per the documentation: https://www.home-assistant.io/components/template/#entity_id

- platform: template
  sensors:
    total_energy:
      friendly_name: 'Total Power Consumption'
      entity_id:
        - sensor.your_kwh_sensor1_here
        - sensor.your_kwh_sensor2_here
        - sensor.your_etc...
      unit_of_measurement: "KWh"
      value_template: "{{ states.sensor|selectattr('attributes.unit_of_measurement', 'equalto', 'KWh')|rejectattr('entity_id', 'equalto', 'sensor.total_energy')|map(attribute='state')|map('float')|sum|round(3) }}"

If there are a large number of sensors and you don’t want to list them all you can use the time sensor (if you have that defined) so it updates every minute.

- platform: template
  sensors:
    total_energy:
      friendly_name: 'Total Power Consumption'
      entity_id: sensor.time
      unit_of_measurement: "KWh"
      value_template: "{{ states.sensor|selectattr('attributes.unit_of_measurement', 'equalto', 'KWh')|rejectattr('entity_id', 'equalto', 'sensor.total_energy')|map(attribute='state')|map('float')|sum|round(3) }}"

Thank you!

That solved :wink:

Had to map every entity on entity_id entry.