< comparison from string to float not working

I’m getting in my logs:

Error handling message: In 'template' condition: ValueError: Template error: float got invalid input '2022.11.1' 

when rendering template '{{ (state_attr('update.home_assistant_core_update','installed_version') | float()
 < state_attr('update.home_assistant_core_update','latest_version') | float()) }}' but no default was 
specified (unknown_error) from

With below value template. The condition itself tests fine in the GUI. I tried casting it to a float or int but i can’t seem to get it to work. If i cast it in developer tools to an | int(0) or a | float(0) it will just say unknown value.

description: ""
  - platform: state
      - update.home_assistant_core_update
  - platform: state
      - update.home_assistant_operating_system_update
  - platform: state
      - update.home_assistant_supervisor_update
  - platform: state
      - sensor.hacs
    from: "0"
    to: "1"
  - condition: or
      - condition: template
        value_template: >-
      - condition: template
        value_template: >-
      - condition: template
        value_template: >-
          {{ state_attr('update.home_assistant_core_update','installed_version')
          < state_attr('update.home_assistant_core_update','latest_version') }}
  - service: script.announcement_voice
      alexa: let_them_sleep
      tts_type: announce
      message: There is a Home Assistant or HACS update available
mode: single

Umm - it says it right there… 2022.11.1 is not a valid float. 2022.11 would be, 11.1 would be - but 2022.11.1 has 2 decimal points.

Did you get that error with the original automation? Because I cannot see the code

{{ (state_attr('update.home_assistant_core_update','installed_version') | float()
 < state_attr('update.home_assistant_core_update','latest_version') | float()) }}

in your automation.

I think i must have use | float() at the time.

Test with the original code. |float() makes no sense as long as you haven’t set a default value.

1 Like

I tried with float(0) as well.

Basically i have 2 strings from strattr, with a value that i need to use < or > operators on, but can’t because it doesn’t convert into a float or a int. Maybe i need to remove the dots and use round().

Good point. So do i round ? Or remove the periods? What would be the best to convert the string to a number so i can compare in size?

{{ state_attr('update.home_assistant_core_update','installed_version') < state_attr('update.home_assistant_core_update','latest_version') }}

I’m using this code, too, but I,ce never got an error.

Yeah i think it should work (and it does in the GUI) as well as in the developer tools, but in the HA logs it show up as an error.

Which error is thrown when using the original code?

Yes - it should work fine without converting to float. The string values will compare as expected.

you could try this

{{ state_attr("update.home_assistant_core_update","installed_version")|replace('.','')|int
< state_attr("update.home_assistant_core_update","latest_version")|replace('.','')|int }}

Thanks - i’ll try that. Yeah the code works fine, just don’t like getting the error in my logs.

I can’t find it now anymore, but it was saying < is not supported from NoneType to NoneType.

Not sure why it didn’t post that in the first place. But that was the whole start of me trying to convert it to ‘a’ type.

NoneType means that HA couldn’t find a valid entity.

Hmm maybe it happens after a restart and the entities are not available yet? Seems strange but…