Can you template from an array of attributes?

Is it possible to template on arrays of attributes? (I think I have the terminology right :wink: )

I know I can do this:

{% set sensor_id= '1' %}
{% set my_value = state_attr('sensor.my_sensor_' ~ sensor_id, 'my_attribute') %}

But what if the attributes are an array? As far as I am aware the only way to extract these is to use the form:

{% set my_value = states.sensor.my_sensor_1.attributes.array[0].my_attribute %}

What I’d like to be able to do is this (which obviously doesn’t work and neither do the many other forms I’ve tried)

{% set my_value = states.sensor.my_sensor_{{ sensor_id }}.attributes.array[0].my_attribute %}

Is there a way to do this?
Thanks

{% set my_value = state_attr('sensor.my_sensor_' ~ sensor_id, 'array')[0].my_attribute %}
2 Likes

Thank you!

Now… Is there a way to see if that attribute is defined?

This works, in that if it is defined it returns True…

{% set id1 = states('input_text.id1') %}

{% if state_attr('sensor.my_sensor_' ~ id1, 'array')[0].my_attribute is defined %}
  True
{% else %}
  False
{% endif %}

But if it is not defined it returns an error

Error rendering template: UndefinedError: None has no element 0

e.g.

{% set id1 = 'x') %}

{% if state_attr('sensor.my_sensor_' ~ id1, 'array')[0].my_attribute is defined %}
  True
{% else %}
  False
{% endif %}

I’ve tried a few other formats but I’m just guessing now…

{% if state_attr('sensor.my_sensor_' ~ id1, 'array') is not none %}
   ...
1 Like

Shoot, beat me to it. I’ll post my pretty picture anyway …

2 Likes

Thanks to both of you…

BTW, in the Jinja expression “xyz is none”, “none” is a test, not a value, and it’s case sensitive. I.e., it’s not a Python expression, so “xyz is None” will not work, so don’t be tempted to change it. :wink:

Sorry to resurrect this but using this in a slightly different way is giving me problems.
I’d be grateful for help to determine if the array obs has any elements.

To be clear, what I am ultimately trying to do is create sensors templated on certain elements e.g. brightness (as highlighted)

Here is the Templates page showing two arrays, one populated and one not.
Thanks as always for any help/pointers.

And just to try to clarify what I am trying to do, this was how I originally had the (incorrect) sensors and I have tried various other permutations of defined and none using both forms of ‘states’ syntax.

{% if states.sensor.smartweather_1.attributes.obs[0].brightness is defined %}
  {{ states.sensor.smartweather_1.attributes.obs[0].brightness }}
{% else %}
  unknown
{% endif %}
{% set obs = state_attr('sensor.smartweather_1', 'obs') %}
{% if obs is not none and obs|length > 0 %}
  {{ objs[0].brightness }}
{% else %}
  unknown
{% endif %}

EDIT: Or better yet:

{% set obs = state_attr('sensor.smartweather_1', 'obs') %}
{% if obs is sequence and obs|length > 0 and obs[0].brightness is defined %}
  {{ objs[0].brightness }}
{% else %}
  unknown
{% endif %}
3 Likes

Works well for whatever I throw at it. :+1:

Perfect, thank you.
Now to investigate what sequence actually does :wink:

Here you go:

https://jinja.palletsprojects.com/en/2.11.x/templates/#sequence

sequence ( value )
Return true if the variable is a sequence. Sequences are variables that are iterable.

1 Like