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
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
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
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).
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
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.
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
But okay, HA is soooo good, we will make do
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?
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 }}
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):
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.
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 )
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
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.