Hi there,
probably for a more savvy user the answer is obvious, but I’m going nuts here right now. I am trying to extract certain values from a attribute of a sensor using split. This method used to work fine for me a while ago in a different template but I can’t seem to get it working again.
The sensor attribute is:
Counts:
- geometries:
- id: 7
name: Eingang
type: LINE
id: 12
info: XLT_LINE_IN_OUT_COUNT
name: Rein-Raus
records:
- counts:
- id: 23
name: fw
value: 0
- id: 24
name: bw
value: 0
from: 1695027840000
samples: 1
samples_expected: 1
to: 1695027900000
I want to extract the “value” of the counts with the ID 23. using the following template:
{{ state_attr('sensor.personenzahler_einlass_2', 'Counts').split ('value:')[1] }}
It gives me the error message
‘list object’ has no attribute ‘split’
I would very much appreciate some guidance since I am quite new to more complex templating…
Thanks in Advance!
Try
{{ state_attr('sensor.personenzahler_einlass_2', 'records')[0][0].value }}
Rofo
(Ro)
September 18, 2023, 9:24am
3
state_attr returns a list, not a string. So you can either
Find the element in the list (which is a string), such as:
{{ state_attr('sensor.personenzahler_einlass_2', 'Counts')[0].split ('value:')[1] }}
[0] above is just a guess, I’m not sure what number you’ll need in there.
Or cast the whole list to a string
{{ state_attr('sensor.personenzahler_einlass_2', 'Counts')|string.split ('value:')[1] }}
Ok that makes sense thanks!
Unfortunately both of your suggestions don’t seem to work for me.
With the first option I get the error
‘dict object’ has no attribute ‘split’
And with the second (which seems the most sensible option for me) I get the following error:
TemplateSyntaxError: expected token ‘end of print statement’, got ‘[’
Thanks for the suggestion, unfortunately this gives me the error:
UndefinedError: None has no element 0
Could you maybe explain what [0][0]
stands for?
[0] refers to element of the list. I misread your example as containing a list in a list so it refers to element [0] of first list and then element [0] of that list.
I don’t have experience with count sensor outputs so If you post the contents of Attributes field for that sensor in Developer Tools > States it would be easier to decipher.
you might be able to do:
{{ state_attr('sensor.personenzahler_einlass_2', 'Counts')[0].value }}
Sorry I’m a complete moron that needs more coffee…
{{ state_attr('sensor.personenzahler_einlass_2', 'Counts')[0].records[0].counts[0].value }}
Oh great, thanks! That work’s like a charm
Rofo
(Ro)
September 18, 2023, 10:11am
8
I missed out some brackets, should have been:
{{ (state_attr('sensor.personenzahler_einlass_2', 'Counts')|string).split ('value:')[1] }}
But the indexing method is cleaner.