Compare (sub)strings in Jinja2

I’ve got a strange issue but am sure it’s because of my lack of experience with Jinja2.
Goal: check if I’m running the current version of NC.

Therefor I have two sensors, sensor.ncver with the current version of nextcloud:production-fpm available on docker hub (currently 23.0.5) and sensor.nextcloud_json.attributes.nextcloud.system.version with the version I’m running (currently 23.0.5.1).

Problem (checked in Developer Tools / Template):
{{states('sensor.ncver') in states('sensor.nextcloud_json.attributes.nextcloud.system.version')}} results in False, which I deem wrong because {{'23.0.5' in '23.0.5.1'}} is True.

Can someone tell me where I went astray?

Use the state_attr() function to get an attribute’s value, not states().

{{states('sensor.ncver') in state_attr('sensor.nextcloud_json', 'nextcloud').system.version }}

If that fails to retrieve the correct value from sensor.nextcloud_json then I’ll need to see an example of the value in the nextcloud attribute.

Thank you Taras, that did the trick.
I tried state_attr('sensor.nextcloud_json','nextcloud.system.version') within the template playground but it errors out with TypeError: argument of type 'NoneType' is not iterable.
Never thought of attaching it after the function, though.
state_attr('sensor.nextcloud_json','nextcloud')['system']['version'] works too, btw.

1 Like

Yes, that’s called “bracket notation” and the other one is “dot notation”. You can use one or the other, even a mix of both. However, use bracket notation if the word begins with a number. Reference: Entity ID begins with a number