How to properly use state attributes?

I’ve been toying around in the developper tag and trying to use templates to “extract” a state attribute but I guess I’m reading the help wrong, as the only output I get is either None or an error.

I would like to use the “period” as a condition in a few automations, but so far, no dice.

So far this has yielded the entire state, which is not what I want:

{{state_attr('sensor.defi_hilo','next_events')}}

I’m probably missing something quite simple am I not?

Any pointers appreciated.

try:

{{state_attr('sensor.defi_hilo','next_events')[0].period}}

the [x] is the index value of the list of items in the “next_events” attribute. since lists in jinja are 0 indexed then the first item is index value 0 → [0].

then you need to give it the key of the value you want in the list of attributes.

so in this case it’s index 0 of the list and the key you want is ‘name’.

to take it to the next level if you wanted the second item in the list of “phases”:

{{state_attr('sensor.defi_hilo','next_events')[0].phases.preheat_end}}
1 Like