Hi all,
Firstly a big thanks to @petro for his guidance on this topic on this thread Template loop detection
I have most of what I wanted to achieve completed with only some minor issues to clean up but also make sure I’m doing it right.
First a bit of background,
I have gone and cleaned up my naming convention for my devices and entities which allows me to dynamically create groups for power monitoring devices in each room I have them in the house.
So in my automations i have (as a example of the first room)
- id: "1641380832863"
alias: Update Energy Groups
description: ""
trigger:
- platform: homeassistant
event: start
- platform: event
event_type: call service
event_data:
domain: group
service: reload
condition: []
action:
- service: group.set
data_template:
name: Rumpus Room Energy
object_id: rumpus_room_energy
entities: >
{{ states.sensor
| selectattr('object_id', 'search', '^(rumpusroom)')
| selectattr('object_id', 'search', '(_energy)$')
| map(attribute='entity_id') | list }}
then in the configuration.yaml I have the following template sensors setup (I’ll move these to a sensors.yaml at a later date and I’ll stick with the rumpus room as the example)
sensor:
#Total energy per room for monitoring
- platform: template
sensors:
total_rumpus_room_energy:
friendly_name_template: "Rumpus Room Total Energy"
value_template: >
{{ expand('group.rumpus_room_energy')
| map(attribute='state')
| map('float', none)
| reject('==', none)
| sum | round(2) }}
unit_of_measurement: "kWh"
device_class: energy
and to get this to show up in the energy dashboard I have the following
(I’m in the first few hours, the kitchen has started to graph so I’ll be patient and wait for the others)
customize_glob:
sensor.total*_energy:
last_reset: "1970-01-01T00:00:00+00:00"
device_class: energy
state_class: measurement
So the question now is to clean up a few anomalies that come up from time to time.
I want to add an availability template to each room template so I can remove any unavailable, unknown or none but looking at the post here
availability_template: >-
{% set sensors = expand('group.house_temperature_sensors') | list %}
{{ sensors
| rejectattr('state', 'in', ['unknown', 'unavailable', 'restored'])
| selectattr("attributes.attribution")
| list | count > 1
}}
value_template: >-
{% set sensors = expand('group.house_temperature_sensors')
| rejectattr('state', 'in', ['unknown', 'unavailable', 'restored'])
| selectattr("attributes.attribution")
| map(attribute='state') | map('float') | list %}
{{ (sensors | sum / sensors | count) | round(1) }}
The example above uses the rejectattr in both the availability and value templates.
I just don’t understand why, I thought it might be listing the entities and checking those individually and if at least 1 was not in the rejected attr state it would then populate the value template.
If that’s right, then would I need to ensure that I have the availability template before the value template, or it wont matter as its all part of the same code block?
Would a suitable availability template be below ?
What else do I need to add to it to cover off the non-value responses.
availability_template: >
{{ expand('group.rumpus_room_energy')
| rejectattr('state', 'in', ['unknown', 'unavailable', 'none'])
}}
Finally I want to add a range filter to stop any wild +/- values that I’ve seen over time.
Can this be done in the availability template or does it need to be its own filter and could I use the same select logic to cover all my energy entities?
Final question if you see anything hear that’s going to bite me please let me know or if you need more details please free to ask.
The idea/plan is to have a near bullet proof way of collecting stats on energy usage.
I’m realistic to know there will be issues from time to time that will throw a curve ball so if someone else can use this then collectively we’ve done some good.
Thanks for your time and involvement in advance.
regards,
Ezekiel