Why is it that when using an input_number in a template it is read as a string but when writing to it with input_number.set_value, it needs to be a number?
e.g.
Has to be cast as an int - because it is read as a string:
Everything works that way, not just input numbers. Just check your sensors, theyâll behave the same way. So if you have any logic that requires them to be numbers, you should be casting them.
Maybe you are confusing attributes with the state of an object?
Attributes return the correct type, states always return strings.
So on an input number the min attribute will return a float:
Strings multplied by 2 will result in double string
{% for input_number in states.input_number %}
{{ input_number.entity_id }} state multiplied by 2
object without cast: {{ input_number.attributes.min * 2 }}
method without cast: {{ state_attr(input_number.entity_id,'min') * 2 }}
{% endfor %}
All âmainâ states in home assistant everywhere are strings. Iâm not sure why this was done, but that is the case. My guess is that it was done because Jinja only returns strings.
But itâs âmainâ state can be any of the following numbers, 0, 25, 50, 75 or 100. And if I want to set that state in an action I have to supply a number, not a string.
But as you can see in my example above the âmainâ state of an input_number is not a string.
Iâm not trying to be argumentative by the way. Just trying to understand, and I appreciate your patience in helping with my lack of understanding because it is things like this that keep tripping me up when configuring HA.
Thereâs really nothing to understand here other than all âmainâ states that you grab in home assistant are strings.
The inputs themselves are driven by yaml. When you use âvalue: 100â in the input, while it may look like a number to you, itâs actually a string when pulled into yaml. Now this is all conjecture, but homeassistant or the yaml library in python most likely has a method to detect which kind of python type the object is. When you place quotes on it, it most likely says âHey this is a string, not a number because it has quotesâ. So leaving out the quotes in the yaml is most likely casting it as an integer.
I understand itâs illogical. But what your seeing is a nuance of the yaml python library (which tries to make yaml easier by strongly typing your inputs, I.E. converting inputs to the correct type). See documentation:
And then you are also seeing what was a decision made by the hass team: All âmainâ states are strings.
I do not know why this was done and I could make guesses. I would wager that because templates (jinja) only returns strings, and user interfaces only display strings, that the logical method to store states is as a string.