Keep a template result as a string, even if it could be converted to a number

As title, I would like to know how to the template syntax to keep the template result as string even if it looks like a number. For example if I enter in Developer Tools the template

{{ "+393331234567" }}

the result is 393331234567 and the type of result is number. How to change this template so that result is +393331234567 and the type a string?

The need to keep as string some template result, referencing states and script variables, come for some scripts and automation, where dropping the plus sign at the beginning of the string cause failures in subsequent service calls. An example (not the only one) is

      action:
      - service: notify.sms
        data_template:
          target: "{{ states('input_text.phone_number') }}"
          message: "{{ '1234FOFF' }}"

I can think to some workaround but any is optimal.

That is not what I get.
I get the string +393331234567.

What happens if you {{ "" ~ "+393331234567" }}?

393331234567 and still number

Try this in the Developer tools. You’ll see it’s doing “the right thing” for you:

{% set str_test="+12345" %}
{{ str_test }}

I think you need to get it into a variable first.

No way, still number without + sign.

Append |string

Edit: Ok dont work

My findings:



:thinking:

Not relavent

Also a number

It’s a string

There is a bug, maybe

Edit:

I give up

      action:
      - service: notify.sms
        data_template:
          target: "{{ states('input_text.phone_number')|string }}"
          message: "{{ '1234FOFF' }}"

Check if + is missing

1 Like

got this to work

{% set x = ('+393331'| string) |string  %}
{% set y = ('234567'|string) | string %}
{{ (x) }}
{{ (y)}}
{% set z = x ~ y %}
{{ z }}

this also works

It seems you have to do a second operation for it to work.

This works properly and returns a string

{% set y = '+393331234567'  %}
{{ y }}
{% set z = y %}
{{ z }}

This does not

{% set y = '+393331234567'  %}
{{ y }}

3331234567 (without +39) is a valid input, the default country code (+39) will be used by subsequent gateway service
but if +39 or whatever intl code is included the plus should be keept to make gateway service understand there is already a country code
the logic seems too complex to replicate in a template in tens of place in automations and scripts

there a new lines embedded in result, that’s why the result is a string and + signs are kept

the “working” templates include newlines, that’s why the result is a string

do you get a string with leading + from my template?

I’ve tried multiple different ways to make this work in the template editor, but I can also only get the result of number. I think this needs to be raised as a bug, if you explicitly cast to a string, it should not be getting converted to a number.

1 Like

Then why doesn’t this work?

{% set w = "+393331234567\n" %}
{{ (w) }}

This returns a number???

It’s even worse than that - try this:

{% set w  = "+3912345678901234567890"|string %}
{{ w }}

3.9123456789012346e+21
??

Even specifically casting to a string, the value is still automagically converted to a number, and now you can’t even get access to the original value.