Error with greater than

I got an error greater then not supported…

Actie script/x kon niet worden uitgevoerd. TypeError: ‘>’ not supported between instances of ‘str’ and ‘int’

How can i make numbers from strings in variables?

alias: start_netspeed
variables:
  berichtlogtype: debug
  berichtid: "{{ state_attr(this.entity_id, 'friendly_name') }} (script)"
  keuze: "{{ states('sensor.speedtest_download') }}"
  norm: 600 
  mode: "{{ iif(keuze > norm, 'ok', 'nok') }}"
  berichttitel: "{{ state_attr(this.entity_id, 'friendly_name')}} {{mode}}"
sequence:
  - data:
      replace_attributes: false
      attributes:
        "{{berichtid}}": 1 start {{keuze}}
    target:
      entity_id: sensor.melding
    enabled: true
    action: variable.update_sensor
  - data: {}
    target:
      entity_id:
        - sensor.speedtest_download
    action: homeassistant.update_entity
  - choose:
      - conditions:
          - condition: template
            value_template: "{{ mode == 'nok' }}"
        sequence:
          - data:
              entity_id: media_player.chromecast_huiskamer_nest
              message: hallo, Netwerk is traag, {{keuze}}
              cache: true
              language: nl
            enabled: false
            action: tts.google_translate_say
          - data:
              message: hallo, Netwerk is traag, {{keuze}}
              title: Netwerk
            action: notify.persistent_notification
  - data:
      variables:
        bericht_titel: "{{ berichttitel}}"
        bericht_prg: "{{berichtid}}"
        bericht_id: "{{ states['sensor.melding'].attributes[berichtid] }}"
        bericht_logtype: "{{berichtlogtype}}"
        bericht_inhoud:
          keuze: "{{keuze}}"
          mode: "{{mode}}"
    target:
      entity_id: script.default_bericht
    enabled: true
    action: script.turn_on
mode: single

One of keuze or norm is integer, while the other is string, so you cannot compare them. Lets change this to:

{{ iif(keuze|int > norm|int, 'ok', 'nok') }}

Obviously one of the filter can be removed, as one value is already integer, but it will not harm.

1 Like

Why do you write every variable as “”{{ …}}"?
Can’t you write them as {{…}}?

If it is a single line value (not multiline string, with > or |) then starting with { has a special meaning. It starts a dictionary. That is not what you want here. So you need to make it a string first, before you can start the template. That is why it needs the quotes.

Resume

I need this?

keuze: “{{ states(‘sensor.speedtest_download’) }}”
norm: 600
mode: “{{ iif(keuze|int > norm|int, ‘ok’, ‘nok’) }}”

PS: i cant leave the quotes away for line mode, then the editor becomes red

The script dows work but sometime i get this:

2024-10-08 14:30:46.107 ERROR (MainThread) [homeassistant.components.script.x] start_netspeed: Error rendering variables: ValueError: Template error: int got invalid input ‘unavailable’ when rendering template ‘{{ iif(keuze|int > norm|int, ‘ok’, ‘nok’) }}’ but no default was specified

Yes, if put in the respective places. But either norm or keuze is currently undefined. As norm looks fine, is states(‘sensor.speedtest_download’) returning the right value in the developer tools template tester?

Yes, thanks, I’ve recently noticed that the automation-syntax has changed.

You can write ‘{{ iif(keuze|int(0) > norm|int(0), ‘ok’, ‘nok’) }} to make the default 0.

Also, just found info about “iif”:
Syntax: iif(condition, if_true, if_false, if_none), looks like the if_none-part is missing in your code, I think that might be the reason for the ERROR.

The defaults will hide the fact that the value you need isn’t there, so you should definitely look why it is unavailable and if that is really the case before you start protecting against unavailbility.

hi all,

thanks for the replies!

I solved the errors with the next code…

keuze: “{{ states(‘sensor.speedtest_download’) | int | int(default=‘0’) }}”
norm: 600
mode: “{{ iif(keuze | int > norm, ‘ok’, ‘nok’) }}”

Problem is (thanks edwin_d) that the speedtest integration isnt always stable, so the sensor value is not there…
In that case there isnt a value for the sensor.
I do a reload for this integration and that solves the other problem…

I doubt it is solved. Here you convert to int twice, but only the second time you have a default. Once is enough, but it must have a default. Also, your default is a string, which is weird if you wanted an int This should be enough:

keuze: "{{ states('sensor.speedtest_download') | int(0) }}"

This also needs a default.

mode: "{{ iif(keuze | int(0) > norm, 'ok', 'nok') }}"

Ironically this casts to int again, so you could have done away with the first one too.

i changed the code with your code and renamed the old lines.
but i cant save the code because of an error…

Message malformed: invalid template (TemplateSyntaxError: unexpected char ‘‘’ at 11) for dictionary value @ data[‘variables’][‘keuze’]

alias: start_netspeed
variables:
oudkeuze: “{{ states(‘sensor.speedtest_download’) | int | int(default=‘0’) }}”
keuze: “{{ states(‘sensor.speedtest_download’) | int(0) }}”
norm: 600
oudmode: “{{ iif(keuze | int > norm, ‘ok’, ‘nok’) }}”
mode: “{{ iif(keuze | int(0) > norm, ‘ok’, ‘nok’) }}”
sequence:

Oh, sorrt. It seems like IOS did it’s nasty trick again to change normal quotes into beautiful, but non working curly quotes. You need the straight, real quotes for it to work. I have it set to off in the keyboard settings, so I do not know how and why it was activated anyway. I edited them back (click on the pen above to see the subtle, but deadly difference).

The “oud” lines will still give you errors though.

@pvklink If you don’t format your code properly with the </> button, you’re going to keep finding errors with quotes even without iOS trying to to us all up.

Oh, excuses…

Here the full script and yes i can save it now…

alias: start_netspeed
variables:
  berichtlogtype: debug
  berichtid: "{{ state_attr(this.entity_id, 'friendly_name') }} (script)"
  keuze: "{{ states('sensor.speedtest_download') | int(0) }}"
  norm: 600
  mode: "{{ iif(keuze | int > norm, 'ok', 'nok') }}"
  berichttitel: "{{ state_attr(this.entity_id, 'friendly_name')}} {{mode}}"
sequence:
  - data:
      replace_attributes: false
      attributes:
        "{{berichtid}}": 1 start {{keuze}}
    target:
      entity_id: sensor.melding
    enabled: true
    action: variable.update_sensor
  - data: {}
    target:
      entity_id:
        - sensor.speedtest_download
    action: homeassistant.update_entity
  - choose:
      - conditions:
          - condition: template
            value_template: "{{ mode == 'nok' }}"
        sequence:
          - data:
              entity_id: media_player.chromecast_huiskamer_nest
              message: hallo, Netwerk is traag, {{keuze}}
              cache: true
              language: nl
            enabled: false
            action: tts.google_translate_say
          - data:
              message: hallo, Netwerk is traag, {{keuze}}
              title: Netwerk
            action: notify.persistent_notification
  - data:
      variables:
        bericht_titel: "{{ berichttitel}}"
        bericht_prg: "{{berichtid}}"
        bericht_id: "{{ states['sensor.melding'].attributes[berichtid] }}"
        bericht_logtype: "{{berichtlogtype}}"
        bericht_inhoud:
          keuze: "{{keuze}}"
          mode: "{{mode}}"
    target:
      entity_id: script.default_bericht
    enabled: true
    action: script.turn_on
mode: single
1 Like