As a totally newb on Home Assistant I want to thank You for providing this example. I didn’t get it to work at first, trying to google if object_id is the same as Entity ID (which it apparently isn’t). So I modified Your code a little, and adjusted the name of my shelly sensors which is sensor.shellyNAME_power and this seems to work for me. Thanks!
sensor:
- platform: template
sensors:
shelly_current_consumption:
friendly_name: Current Total Power
entity_id: sensor.time
unit_of_measurement: W
state_class: measurement
value_template: >
{% set ns = namespace(states=[]) %}
{% for s in states.sensor %}
{% if s.entity_id.startswith('sensor.shelly') and s.entity_id.endswith('_power') %}
{% set ns.states = ns.states + [ s.state | float ] %}
{% endif %}
{% endfor %}
{{ ns.states | sum | round(2) }}
utility_meter:
energy:
source: sensor.shelly_total_consumption
cycle: daily
Is it possible to use the Entity ID sensor.shelly_current_consumption in (edit: statistical) graphs after adding it to configuration.yaml? I can’t use it in (edit: statistical) graphs right now. edit: I could use the sensor in a History Graph though.
When trying to add this synthetic sensor to a statistical graph I get this; Expected data source not listed - Home Assistant
You’re configuring a statistic but you couldn’t find your source in the dropdown? That’s caused by a bug in the integration providing the entity. Integrations need to configure their entities correctly so Home Assistant knows that we need to track statistics for it and how.
Open an issue with the author of the integration and link them to Sensor Entity | Home Assistant Developer Docs.
Any hints on how I could improve this to get that to work? I tried adding the following line to the sensor-description above, but that alone didn’t do it.
state_class: measurement
editedit:
Apparently there’s an old way and a new way to create templates. This method above is the old way, and cannot accept a state_class. Thus, the template has to be rewritten to the new format, which does accept state_class. I did it through the UI helper, so I don’t know how to show it in YAML, but essentially You create a new Helper in settings, choose Template, Template a sensor, enter a name, enter the code-block below (edited to Your needs) as State template. W as unit of measurement, Device class: Power and State class: Measurement. Now the sensor can be used in historical graphs too.
{% set ns = namespace(states=[]) %}
{% for s in states.sensor %}
{% if s.entity_id.startswith('sensor.shelly') and s.entity_id.endswith('_power') %}
{% set ns.states = ns.states + [ s.state | float ] %}
{% endif %}
{% endfor %}
{{ ns.states | sum | round(2) }}
Phew, this first post of my became unncessarily long, sorry for that. Hope this helps someone.