I try to write a generic script to pass a value to a device by modbus.write_register.
The parameter should be the value that are written to the device.
I now can call the script with a fix value: this is working!
This is a bit above my pay grade, but if the parameter is a number don’t you need to add float or int when you use states etc. Which is what you’re asking for.
states('sensor.my_number_entity') | float(0)
or
states('sensor.my_number_entity') | int
Hi Tom, THANKS, This works! Before testing again, I was quite sure that I also try this before …
… maybe I have another writing error in this test I do before … (?).…
In order to use a state as a number, integer, or boolean, you need to specify that for use inside the template.
I.e. "{{ states(...) | float(0) + 1 }}"
If you’re just returning the state from a template, you do not need | float. because you aren’t perform math with that inside the template. After a template renders a value, it will always be a string. However Home assistant does a fancy little thing after the template resolves and before it puts the value in a field/variable. It takes the response and attempts to assign a type to it. So when the value goes into the field/variable, it’s properly typed.
e.g. if sensor.abc has a state of "4". The template "{{ states('sensor.abc') }}" will resolve as "4" (a string), but then it’s placed into the field or variable as 4 because HA identified it as a number.
This allows you to do things like:
field: "{{ states('sensor.abc') }}"
field2: "{{ field + 1 }}"