How to best solve this HA version comparison in template

knowing I am comparing strings, and not numbers, please let me ask how to make this formally correct, though my current template seems to always give the desired outcome:

      ha_update_available:
        friendly_name: 'Ha update available'
        value_template: >
          {% set latest = states('sensor.ha_available_version') %}
          {% set local = states('sensor.ha_local_version') %}
          {{latest != 'unavailable' and local != 'unavailable' and 'b' not in latest and
            latest > local}}

output of both latest and local is in the form of 0.109.5 and 0.109.6 (latest is incorrect at the moment, but that is irrelevant for this issue. well, see below, it is the reason for the whole exercise…)

see what happens in template editor.
no matter which comparison I throw at the bottom template, it always seems correct.
still, I know I shouldn’t do it like this.

please have a look.
reason I ask is because of the fact the latest is incorrect at the moment, and before, the template was simply checking both sensors not being equal. != With a correct state , the would of course suffice. If the available version would not be equal to the installed version, that would indicate an update.

right now, with the available version being 1 down from the installed version, this results in a false red flag :wink:
need to be more exact.

1 Like

I’m guessing it’s an order of operations thing. What happens if you throw some parentheses at it,

{{ (latest != 'unavailable') and (local != 'unavailable') and ('b' not in latest) and (latest > local) }}

no, my template works alright, its like doing this:

{{latest > local}}

which are 2 strings. we shouldn’t use > operator on strings.
though in this specific case it might just work…since we know the strings are always in the format of 0.109.5

Couldn’t you just remove the dots and convert it to an int for comparing?

ha, like

{{latest.replace('.','')|int}}
{{local.replace('.','')|int}}

yea probably could. its killing the leading 0 though. guess that doesnt hurt. thanks for the idea!

1 Like

Yes like this

You could also split the version into major, minor and patch and compare each , but this would be a bit overkill, but you would have the ability to e.g. only inform if there’s a new minor release or hqve different messages based on whether it’s a patch or minor release