Auto Utility Meters

Hi, is there any way to auto-create daily, weekly, monthly and yearly Power Utility meters for each energy entity? Currently I have to create them all by hand under helpers :frowning: (or I could copy/paste in the yaml files). Even a HACS AddOn would be fine :smiley:

You have two main options…

Use YAML Anchors

Use the Template Editor Tool to generate yaml config that you can copy/paste into your configuration file.

But this has nothing todo with autodiscover, at least thats my understanding. I really want to auto create UtilityMeters when a new energy entity is discovered. Both examples are still manual work.

Okay, i did some progress with the Help of ChatGPT, but my Problem is a jinja2 Template:

{% for entity_id in states.sensor | selectattr('attributes.device_class', 'equalto', 'energy') | map(attribute='entity_id') %}
utility_meter:
  {{ entity_id }}_daily:
    source: {{ entity_id }}
    cycle: daily
{% endfor %}

This one causes a lot of errors i totally dont understand:

found character ‘%’ that cannot start any token in “/config/templates/utility_meter_template.yaml”, line 2, column 2

From HA itself

missed comma between flow collection entriesYAML

From VSC

Have a look at Powercalc.

Yes… As far as I know, this is what is possible currently.

I wouldn’t describe having ChatGPT lie to you as “progress”…

As discussed in the link I posted, that template is for use in the Template Editor tool only. It will not work if placed directly in configuration… no matter what ChatGPT says.

To cover all three cycle types you would use the following in the Template editor, then copy/paste the output to your utility_meter_template.yaml file:

utility_meter:
{%- for x in ['daily', 'weekly', 'monthly'] %}
{%- for entity_id in states.sensor | selectattr('attributes.device_class', 'defined')
| selectattr('attributes.device_class', 'eq', 'energy') | map(attribute='entity_id') %}
  {{ entity_id | replace('sensor.', '') }}_{{x}}:
    source: {{ entity_id }}
    cycle: {{x}}
{% endfor %}{% endfor %}

I will try that, together with a small automation. Thanks so far.

Template that works:


utility_meter:
{%- for x in ['daily', 'weekly', 'monthly', 'yearly'] %}
{%- for entity_id in states.sensor | selectattr('attributes.device_class', 'defined')
| selectattr('attributes.device_class', 'eq', 'energy') | selectattr('name', 'search', 'energy$') | map(attribute='entity_id') %}
  {{ entity_id | replace('sensor.', '') }}_{{x}}:
    source: {{ entity_id }}
    cycle: {{x}}
    unique_id: {{ entity_id | replace('sensor.', '') }}_{{x}}
{% endfor %}{% endfor %}

Next step: Automation for that and avoid duplicates

Ok, looks like iam lost. The template is working fine in the template editor, but howto USE it now? the documentation is sooo unhelpful, iam lost here :frowning: (sorry but for template using the documentation is completly useless so far)