idro
(Fabio)
December 16, 2024, 12:58pm
1
Hi there,
if I use the following yaml template code, it works perfectly:
{{ expand('cover.tapparelle_casa')
| selectattr('attributes.current_position', 'eq', '100')
| list | count }}
Otherwise, if I use this:
{{ expand('cover.tapparelle_casa')
| selectattr('attributes.current_position', '<', '100')
| list | count }}
I get the error message:
UndefinedError: 'homeassistant.util.read_only_dict.ReadOnlyDict object' has no attribute 'current_position'
PS: tapparelle_casa stands for home_covers
idro
(Fabio)
December 19, 2024, 2:07pm
2
Iām using the templates into developer tools ā templates, to get the current position attributes from covers.
If I put into template this simple code:
{{ expand('cover.tapparelle_casa')
}}
I get the following output:
[<template TemplateState(<state cover.tapparella_2=unavailable; restored=True, device_class=shutter, friendly_name=Tapparella cameretta 2, supported_features=15 @ 2024-12-18T23:39:58.579010+01:00>)>, <template TemplateState(<state cover.tapparella_cameretta_1=open; current_position=26, device_class=shutter, friendly_name=Tapparella cameretta 1, supported_features=15 @ 2024-12-18T23:38:57.083252+01:00>)>, <template TemplateState(<state cover.tapparella_della_camera=open; current_position=100, device_class=shutter, friendly_name=Tapparella della camera, supported_features=15 @ 2024-12-19T08:17:39.360471+01:00>)>]
If I try to catch the current_position values in this way:
{{ expand('cover.tapparelle_casa')
|map(attribute='attributes.current_position')
| list
}}
I get the following:
[Undefined, 26, 100]
How could I do some compares with numbers I get?
For example if I want all the covers with current_position > 0 ??
What are you actually trying to do? You can use selection and rejection filters with comparison tests to winnow the values in the list to just the ones you are interested in.
Filters:
select()
selectattr()
reject()
rejectattr()
Tests
To get the entity IDs of all covers in the group with current_position
values that are less than 0:
{{ expand('cover.tapparelle_casa')
| selectattr('attributes.current_position', 'defined')
| selectattr('attributes.current_position', '<' , 0)
| map(attribute='entity_id')
| list
}}
1 Like
idro
(Fabio)
December 20, 2024, 2:26pm
4
Didgeridrew:
{{ expand('cover.tapparelle_casa')
| selectattr('attributes.current_position', 'defined')
| selectattr('attributes.current_position', '<' , 0)
| map(attribute='entity_id')
| list
}}
First of all thanks for the reply. I committed an error: obviously I need to select the current position > 0 and not < 0.
Now I can work on this code to get other data I need. Thank you.