How to properly configure Utility Meter for multiple devices

Hello everyone,
I would like to setup Utility Meter to monitor all consumers in my home.
Now, my first test was simply

utility_meter:
  daily_energy:
    source: sensor.plasma_energy_power
    cycle: daily
  weekly_energy:
    source: sensor.plasma_energy_power
    cycle: weekly
  monthly_energy:
    source: sensor.plasma_energy_power
    cycle: monthly
  yearly_energy:
    source: sensor.plasma_energy_power
    cycle: yearly

But now I want to add a whole bunch of sensors to monitor.
If I simply add more sources I get an error message and if I simply repeat, I get an error message.
So

utility_meter:
  daily_energy:
    source: sensor.plasma_energy_power
    source: sensor.fridge_energy_power
    cycle: daily

and

utility_meter:
  daily_energy:
    source: sensor.plasma_energy_power
    cycle: daily

utility_meter:
  daily_energy:
    source: sensor.fridge_energy_meter
    cycle: daily

both bring error messages (e.g. duplicate key).

What is the correct way to do this? Ideally with the least amount of code possible because it becomes a nuisance to maintain if you have to repeat and repeat the same code.
Maybe there is a way to create a placeholder/list?
Maybe something of the type

sensors_to_monitor=sensor.plasma_energy_power;sensor.fridge_energy_power
utility_meter:
  daily_energy:
    source: {{sensors_to_monitor}}
    cycle: daily

?

Thank you for your suggestions :slight_smile:
Alex

Looks like you tried everything but the correct one :wink:

Just do,e.g,

utility_meter:
  daily_energy_tv:
    source: sensor.plasma_energy_power
    cycle: daily
  daily_energy_fridge:
    source: sensor.fridge_energy_power
    cycle: daily
[...]

I don’t see a way to “automate” the creation of the meters myself.

1 Like

Thank you :slight_smile:
Sometimes HA is simply to rigid.
Any chance of at least defining the cycle just once for a block of sources?
Something like

utility_meter:
  daily_energy_tv:
    source: sensor.plasma_energy_power
  daily_energy_fridge:
    source: sensor.fridge_energy_power
  cycle: daily

For true automations etc. one really needs a syntax that allows one to address a lot of things with a minimum of code.

1 Like

Addendum: is it maybe possible to use Automations to feed the Utility Meter? That way one could use Jinja lists etc.

Honestly? Perhaps try understanding all the tools available to you first. What gave you the impression you could have multiple sensors under a utility meter?

Your problem can be solved quite neatly by creating a group with all your sensors and then creating a template sensor to sum the values by iterating over the group (there are jinja2 functions and many posts about this kind of thing). Once you have that, use it for your utility meter. If you get an additional power meter, just add it to the group.

It was not meant as an insult to anyone. And I said sometimes, because you then need to find workarounds outside the place you are writing the code. So you can create a group but you cannot create it where you are using it. You need to define global groups which are maybe not needed anywhere but that one piece of code.
I had a similar issue with adding a bunch of sensors to telegram/signal notification.
Plus, you cannot use the group itself, you need to create a new, fake sensor which you can then use. So not the leanest, most straight forward and intuitive way of programming :slight_smile:

Again, no insult, just a personal view of why it is more difficult to get it right. Because it is not thr logical, lean approach;-)
HA is obviouslx still awesome and I love using it and learning it, but sometimes it is rigid in the sense that it understands code but does not allow it everwhere. Only in defined parts of HA).

Maybe try to see it from another perspective.

Defining sensors et al is configuration, not coding.
The “code” is the automations, and there you now have blueprints to do the kind of “code once, reuse often” approach you’re looking for.

Yes, I understand the idea and I accept the idea. I just think that it would be leaner to not distinguish in this strict way.
When it comes to the UI, then of course, separate them. But when you start manually editing the code, it to me would make sense to be able to use Jinja everywhere :slight_smile:

The strict separation, for me personally, regularly causes confusion or rather makes thing less intuitive because you have a lot of restrictions within a certain area (e.g. in this case the configurations).
But I do understand the idea behind it. And if does make the UI of course cleaner and more manageable. It just makes the coding less lean because you are now defining “one task” in 2-3-4 different places and need to be aware of where else you had code snippets that are needed for this one task.

EDIT1:
Quick on topic question:
If I define a group of sensors, will they then be monitored as one sensor? Because I want to be able to plot each of the actual sensors individually and not just track the consumption as a total of all sensors.

Completely agree. This is definitely the most confusing part of HA:
When can you use templates, with what keyword, … :slight_smile:

Re your question, I don’t think you can (or even if it makes sense) to create utility_meter groups, if that’s what you mean.

Well, groups would make sense when I want to monitor a room, but unfortunately, due to the code separation, it is too much hassle. The thing is that now the Utility Meter configuration will be huuuuge :slight_smile:
But okay, HA is soooo good, we will make do :slight_smile:

Hi @parautenbach.

I am still learning HA, and the thing you describe here is the one thing I am looking for. I want to group the kWh usage of ALL my houses lights into one sensor, which I then can use in both Utility Meter and the Energy tab from HA, but so far I have not been successful in getting this to work.

Do you happen to have the procedure to do this, maybe even with some examples?

Thank you!

I’m assuming you have consumption info for each light? I don’t have such lights so I don’t know which attribute on the entities to use or whether such lights create their own power/consumption sensors.

But, this is how you would list all the light entities to work with:

{{ states.light | map(attribute='entity_id') | list }}

You can sum like this:

{{ states.light | sum(attribute='some_attr') }}

You can make a template sensor with the above. Just make sure where you use power (W) vs energy (Wh) and do the necessary conversions.

If you show more of what you’ve tried I can possibly help more.

Hi. Thank you for getting back to me. So basically what I have is a list of Z-wave devices/entities where they report the used power in kWh format, where all the devices are named such as this list (just putting 4 of them in interest of saving space):

sensor.bad_nede_tak_electric_consumption_kwh
sensor.bad_oppe_tak_electric_consumption_kwh_3
sensor.bad_speil_electric_consumption_kwh_2
sensor.eirik_tak_electric_consumption_kwh

Then I use those sensors in a template like this one I have here (Pastebin):

When I do, I can use that virtual sensor (template sensor) in the Energy dashboard, but it does not show any used kWh even after a few hours there.

I also tried doing this in a slightly different way, through my sensors.yaml where I have other sensors defined already, it also gives me the additive result with the correct number of kWh, but I cannot find that entity to use in the Energy dashboard.

So I am a bit stumped, not sure why the last doesn’t show up in the Energy dashboard dropdown, or why the first does not count upwards over time.

Just a note: Power’s base unit is W (watt), which is the rate of energy consumption/generation in joule/sec. Wh is energy (in joule). Power != energy. 1Wh is the amount of energy at a rate of 1W over 1 hour.

Can any of the individual sensor be added to energy management? In other words, do they show up as entities that can be added?

Can you chart either of your template sensors just using a normal history graph chart? In other words, do you know that there actually is any data?

What is the output for your templates in the template editor under the dev tools?

Yes, all of these are accumulated kWh, none of them are Power sensors, they are accumulated usage. They can all be used in the Energy dashboard, just tested a couple of them to make sure. They are all the same format, none of them are Power sensors (W).

It may be that the problem is due to the below error message. I have tried to add the Last_update statement, but I cannot find a way to get it to work, it throw errors the way I have tried.

image

Show your config. It’s much easier to help that way.

You say last_update but it must be last_reset.

I did show my config above in the pastebin links, was it not working?

And yes, I wrote from my memory but I did copy it from another suggestion, was it was last_reset. Just remembered wrong when I wrote the post above. When I put it in the same code as the ones posted above in the other post, it failed with a red message in Home Assistant log.

I will post whatever config you’re referring to, but the config for the virtual sensors/template sensors was pasted in the links.

Edit: Changed from Homeseer to HA (used HS3 for 7-8 years so its still ingrained in my fingers :slight_smile: )

Neither of those pastebins contains a last_whatever so I don’t know what you did.

And now you refer to Homeseer?

And why not post the error message?

I’m not a mind reader!

I will post all relevant config and error message here later today, thank you.

And yes, edited post over, I have been in HS3 world for many many years, so even when my brain thinks Home Assistant, my fingers tend to write Homeseer :slight_smile:

I did some more testing and I found a way to get this to work in the end. I haven’t had the customization.yaml file before as I’ve not needed it, and so I created it and put in the following code:

sensor.lyskilder_total_kwh:
  device_class: energy
  state_class: total_increasing

I then removed the classification from the sensor setup in configuration.yaml, and instead used my first attempt in sensors.yaml with the value_template in it (posted before here). No errors on restart, and after about 1 hour I started getting data into the sensor in Energy.

No need for the last_reset as long as the state_class is total_increasing.

Thanks for your help above.

1 Like