This passes the value via a template and is therefore subjected to Home Assistant’s native typing.
phases_selected: "{{ trigger.to_state.state }}"
In other words, the template’s result is automatically assigned a type; the result appears to be numeric so its type is automatically assigned to be a number.
In contrast, this passes the value as a literal string.
the result will still be passed on to the script as number.
I tried putting additional single quotes around:
phases_selected: '"{{ trigger.to_state.state }}"'
This will be passed as string with additional quotes, which is actually correct in a sense, but again not what I am trying to achieve.
phases_selected: '"3"'
Can I somehow enforce that a string which is containing a numeric value is passed from the template as a string to the script? Or do I need to handle the conversion within the script?
Because native typing evaluates the template’s result and, based on the result’s appearance, sets its type. Therefore it doesn’t matter if you add a string filter; if the final result’s appearance is numeric then its type will be a number (int or float).
That would be the simplest way.
Also, why do you need to handle a numeric value as a string?
EDIT
You can also try to pass the service call’s data in JSON format.
Thanks. Passing the json looks good to me. I still don’t think though that HA should do it’s own type evaluation of templates. If I say it’s a string, it’s because I want it to be
I had been using the same script with different inputs. Initially I used a switch that directly sent “1” and “3” as parameters, then I changed to an input_select and went for the template.
I am using HA for a couple of months now, my background is mostly C#. I still struggle with the way HA treats string, int and boolean types. Sometimes a number is a number, sometimes it’s a string. Sometimes off is the same as “off”, sometimes it’s not.
In previous versions (long ago) it didn’t. The result of every template was a string and only a string. That was far more limiting than the way it works now.
I can understand why someone would feel that way if they don’t understand how Home Assistant’s native typing works.
It works according to the simple rule: “If it looks like a duck, it’s a duck.”
In your example, the template’s result looks like a number, so it’s a number.