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.
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 %}