Calling a script with parameter 'states' of entity get not worked

Hello, I’m trying to implement a script to be used in multiple automations.
This script has a parameter to write this value out via modbus:

nibe_heizkurve_verschiebung_setzen:
  alias: Nibe Heizkurve Verschiebung Setzen
  description: Nibe Heizkurve Verschiebung Setzen über Modbus Write Register
  icon: mdi:thermometer-lines
  fields:
    value_a:
      name: Neuer_Wert_Heizkurve_Verschiebung
      description: Wert auf den die Heizkurve Verschiebung gesetzt wird
      required: true
      example: '-1'
  sequence:
  - variables:
      value_b: '{{ value_a }}'
  - action: modbus.write_register
    metadata: {}
    data:
      hub: nibe_modbus_hub
      address: 30
      slave: 1
      value: '{% if int(value_b) < 0 %}{{ 65536 + int(value_b) }}{% else %}{{ int(value_b)}}{%
        endif %}'
  mode: single

Now calling the script with a fix number works fine:

  - action: script.nibe_heizkurve_verschiebung_setzen
    metadata: {}
    data:
      value_a: -1

BUT I want to give a state() of an entity as parameter:

  - action: script.nibe_heizkurve_verschiebung_setzen
    metadata: {}
    data:
      value_a: states('input_number.nibe_heizkurve_verschiebung_manual')

But now I always get error:
" …template error: int got invalid input ‘states(‘input_number.nibe_heizkurve_verschiebung_manual’)’ when rendering…"
The ‘parameter’ get not rendered to his value, instead it always was taken as string … :frowning:
also trying value in “” or ‘’ does not help …
→ ANY IDEAS ?
Thanks an best wishes, Clemens.

I think you need to add a float to your entity state to turn it into a number.

Hi Jack, thanks for Reply.
also “float” would not help: then I get same error with float:
Template error: float got invalid input ‘states(‘input_number.nibe_heizkurve_verschiebung_manual’)’ when rendering …
Problem is that the script generell use the parameter as string and are not willing to render it as {{ parameter }} … also not into the variable value_b, even into the variable value_a … ??

AND: if I put the same code of the script direct into a action it works fine … ??? The problem is the “not rendering” of the given parameter …!

The value probably can’t be templated.
Transfer the sensor name as text instead a d then read it in your script.

Hallo Wally,
Yes,this would be a solution:
I can change the script with:

      value_b: '{{ states(value_a) }}'

and then also change the call to:

  - action: script.nibe_heizkurve_verschiebung_setzen
    data:
      value_a: input_number.nibe_heizkurve_verschiebung_manual

→ now its always a string that would be transferred:
THIS WORKED.
BUT: now the script if not so universal, because the parameter now is an entity-id !
If I want to input a number, for example from a calculation I cannot use the same script. …
Maybe I will take this nevertheless as work around: using then the script without parameter (so use always the same entity) and writing the value calculated before to this entity.

Maybe some other find out, how to give a state of an entity to a script as parameter [I cannot believe, that thhis is not possible!] :grinning:.
Thanks, Clemens.

HA have a lot of those bumps in places and other places do not have the same limitations.
I think it has something to do with when the code was made.
The newer the code the higher the chance of support for templates.

You should be able to run tests on if the input is a number or not and then act on, but I agree the template solution would be better and I suggest you make a WTH post about it.
It is exactly what the WTH section is about.

1 Like

Hi Wally, thanks again.
Yes you’re right - seems so.
I put a request there according your recommendation. :slight_smile:
Thanks again, best wishes, Clemens.

! was SOVED by Tom with that solution: THANKS to Tom !

  - action: script.nibe_heizkurve_verschiebung_setzen
    metadata: {}
    data:
      value_a: "{{ states('input_number.nibe_heizkurve_verschiebung_manual') }}"

see thread:

Sweet! :slight_smile:
The WTH section do get a bit more attention these days. :roll_eyes: