Template rendering oddly

Can anyone tell me why this template is rendering as true here? in what world is 2021.9.7 >2021.10.0b0?

{{states('sensor.current_ha_stable') > states('sensor.hass_installed_version')}}
{{states('sensor.current_ha_stable')}}
{{states('sensor.hass_installed_version')}}

How does it process this as a number? If it just sees it as a decimal place 2021.9 is greater than 2021.1

Hi David!

It’s doing a string comparison, character by character. It comes to the 6th character, and compares 1 to 9, and sees that 9 is greater than 1.

Maybe this is why it’s always worked before…

Now you are in the 10th month it’s not working as expected.

Leave it with me and I will come up with a template for you :slight_smile:

1 Like
{% set current_ha_stable = "2021.9.7" %}
{% set hass_installed_version = "2021.10.0b0" %}
{% set current_ha_stable_year, current_ha_stable_month, current_ha_stable_patch = current_ha_stable.split(".") %}
{% set hass_installed_version_year, hass_installed_version_month, hass_installed_version_patch = hass_installed_version.split(".") %}

{% if current_ha_stable_year != hass_installed_version_year %}
    {{ current_ha_stable_year|int > hass_installed_version_year|int }}
{% elif current_ha_stable_month != hass_installed_version_month %}
    {{ current_ha_stable_month|int > hass_installed_version_month|int }}
{% else %}
    {{ current_ha_stable_patch > hass_installed_version_patch }}
{% endif %}

1 Like

You don’t need all that. Just split, map to int and compare the lists.

{% set current = "2021.9.7" %}
{% set next = "2021.10.0b0" %}
{{ next.split('.') | map('int') | list >  current.split('.') | map('int') | list }}

Edit, you’ll probably want to replace b with a period to catch beta updates too

2 Likes

This?

binary_sensor:
  - platform: template
    sensors:
      hass_update_available:
        value_template: >-
           {%- if states('sensor.current_ha_stable') != 'unavailable' -%}
           {% set next = states('sensor.current_ha_stable') %}
           {% set current = states('sensor.hass_installed_version') | replace('b','.') %}
           {{ next.split('.') | map('int') | list >  current.split('.') | map('int') | list }}
           {%- endif %}
1 Like