How to access attributes of a sensor in Jinja2


For an automation I need to iterate on all attributes of a sensor. I’m stuck because I don’t find how to access the attribute list for this specific sensor.

In the template page i’ve tried the following

This works but this is not the sensor i’m interested in :
{{ states.sensor.backup_state.attributes }}

This generate the error “TemplateSyntaxError: expected token ‘end of print statement’, got ‘_weather_alert’” :
{{ states.sensor.94_weather_alert.attributes }}

My guess is that’s because the sensor id starts with numbers but I can’t figure out a way to solve this.

Can you please help me ? :slight_smile:

1 Like

Thank you ! It seems I still have some documentation to read :confused:

This worked :
{{ states.sensor['94_weather_alert'].attributes }}

The expand function might also be an option for your use:

{{ expand("sensor.94_weather_alert") }}

1 Like