I have a sensor which sums the power used by all devices that I have got sensors for. It has to exclude some which are not usage sensors but power delivery sensors. It also exclude some sensors which are created from the original (eg tariff splits), also a sensor which is the aggregate of all my lights,
I then subtract this from the power delivered to my home to provide a sensor for all other power consuming items.
This works most of the time but I sometimes find errors in the log that I cannot understand.
Can anyone suggest if the script below could/should be made smarter.
- name: Untracked Power Usage
state: >
{% set ns = namespace(states=[]) %}
{% for s in states.sensor | selectattr('attributes.unit_of_measurement', 'defined') %}
{% if s.attributes.unit_of_measurement == 'W'
and s.object_id[0:6] != 'shelly'
and s.object_id[0:5] != 'other'
and s.object_id[0:4] != 'aca_'
and s.object_id[0:4] != 'acb_'
and s.object_id[-4:] != 'peak'
and s.object_id[-8:] != 'shoulder'
and 'battery' not in s.object_id
and s.object_id[0:9] != 'power_tot'
and s.object_id[0:10] != 'office_x34'
and s.object_id[0:9] != 'untracked'
and s.object_id[0:10] != 'all_lights' %}
{% set ns.states = ns.states + [ s.state | float(0) ] %}
{% endif %}
{% endfor %}
{{ (states('sensor.power_total') | float(0) - (ns.states | sum | float(0)))|round(2) }}
unit_of_measurement: "W"
device_class: "power"
The log says this:
Template loop detected while processing event: <Event state_changed[L]: entity_id=sensor.untracked_power_usage, old_state=<state sensor.untracked_power_usage=77.72; unit_of_measurement=W, device_class=power, friendly_name=Untracked Power Usage @ 2023-02-06T13:14:00.347839+11:00>, new_state=<state sensor.untracked_power_usage=76.52; unit_of_measurement=W, device_class=power, friendly_name=Untracked Power Usage @ 2023-02-06T13:14:01.346732+11:00>>, skipping template render for TemplateâŚ
This does not answer your actual question and I havenât actually tried it out yet myself, but I think this might be a good case for the new sensor groups. Although the sensors canât be dynamically added like your templateâŚ
Is there a reason why you are loading all those object id checks into the for loop instead of reject them beforehand? Or, even better, not bothering with a loop at all⌠just use the built-in filters.
I love your simplification which I will immediately use.
However my underlying issue is why do I get the error. Could it be down to the availability of a value as it assembles the sum? I have been assuming that |float(0) sorted that.
Does this error persist for a while (or is it easily reproducible)? If so, when this happens, but your template in the template editor with a {{ ... }} construct to âprintâ the values of interest.
Itâs also quite easy to add another filter to Drewâs suggestion to filter for unknown or unavailable entities, if thatâs the issue.
It does this every few minutes. I have added script to list the selected entity_ids in Template but nothing revealing shows up. I am puzzled how this error message also shows old and new values despite the error.
Youâll always get a loop detection error here. You can safely ignore it if you filter it out. But that wonât stop the error from occurring. If you wanted to get rid of the error, youâd have to make an automation that creates a group with the entities you want using group.set. Then your template sensor would expand that group.