Hello,
I found the following code on a YouTube channel, but unfortunately it doesn’t work for me. I hope someone here can help me:
Template:
- name: "Gesamtanzahl AA Batterien"
state: >
{% set total = namespace(count=0) %}
{% for entity in states.sensor
if 'battery_plus' in entity.entity_id
and 'battery_type' in entity.attributes
and 'battery_quantity' in entity.attributes %}
{% if entity.attributes.battery_type == 'AA' %}
{% set total.count = total.count + (entity.attributes.battery_quantity | int(0)) %}
{% endif %}
{% endfor %}
{{ total.count }}
unit_of_measurement: "Stück"
I’m using Battery Notes and would like to use it to calculate the total number of batteries I need.
The problem I’m having is that I have multiple entries with battery_type == ‘AA’, each containing 2 batteries. However, I’m getting a total of 2 batteries, as if the query aborted after the first run.
I’m still a beginner at coding, so please bear with me if I overlook a simple mistake.
Thank you!
Torsten
template:
should not be capitalized
sensor
key is missing
name:
is at the wrong level and indentation
template:
- sensor:
- name: "Gesamtanzahl AA Batterien"
state: >
{% set total = namespace(count=0) %}
{% for entity in states.sensor
if 'battery_plus' in entity.entity_id
and 'battery_type' in entity.attributes
and 'battery_quantity' in entity.attributes %}
{% if entity.attributes.battery_type == 'AA' %}
{% set total.count = total.count + (entity.attributes.battery_quantity | int(0)) %}
{% endif %}
{% endfor %}
{{ total.count }}
unit_of_measurement: "Stück"
Though, by using updated template functions you can achieve the same thing more efficiently without the loop, the namespace, or checking every sensor
state object:
template:
- sensor:
- name: "Gesamtanzahl AA Batterien"
state: >
{{ integration_entities('battery_notes')
| select('match', 'sensor.*battery_plus')
| expand
| selectattr('attributes.battery_quantity', 'defined')
| selectattr('attributes.battery_type', 'defined')
| selectattr('attributes.battery_type', 'eq', 'AA')
| map(attribute = 'attributes.battery_quantity')
| sum }}
unit_of_measurement: "Stück"
For what it’s worth, state-based template sensors like this can be configured in the GUI under the Helpers menu. Just make sure that you only paste the template in the “State Template” field, not the whole YAML configuration shown above.
Unfortunately, neither adjustment helped.
With both variants, I always get a response of “2.”
Even though I have 6 entries with battery_type = AA and battery_quantity = 2.
So the response should be 12.
Also tested in the template editor
Regards, Torsten
I’m getting a correct count on my instance… Can you post a screenshot of the States tool for the entities in that you think should be included, but aren’t?
Since I have type “AA” and type “AAA”, I had to split it a bit
Screenshot 2
and 3
It looks like the issue is that most of your devices are missing the sensor
entities that report the charge percentage. Only the Hue outdoor motion has a sensor
entity, which is what the template is based off of.
As long as they all have a "battery_plus_low` binary sensor enabled, you should be able to adjust the template to:
{{ integration_entities('battery_notes')
| select('match', 'binary_sensor.*battery_plus_low')
| expand
| selectattr('attributes.battery_quantity', 'defined')
| selectattr('attributes.battery_type', 'defined')
| selectattr('attributes.battery_type', 'eq', 'AA')
| map(attribute = 'attributes.battery_quantity')
| sum }}
Note that the Hue motion device is represented in your list twice, once as a sensor
and once as a binary_sensor
, so the expected outcome is 10, not 12.
I have another question:
How can I query the status? I want to display the number of batteries in a second entity where the status battery_plus_low = on
I think the most efficient way would be to add another selection using the is_state
test right after the integration_entities()
function:
{{ integration_entities('battery_notes')
| select('is_state', 'on')
| select('match', 'binary_sensor.*battery_plus_low')
| select('attributes.battery_quantity', 'defined')
| selectattr('attributes.battery_type', 'defined')
| selectattr('attributes.battery_type', 'eq', 'AA')
| map(attribute = 'attributes.battery_quantity')
| sum }}
Thank you so much!!
I tried a few things, but I couldn’t find this solution!!