Script to send sms no longer working

I am in the process of transferring my HA configuration to a new server and cleaning things up along the way. I had a script that would send an SMS using the Joaoapps Join component. The script should send the information from a sensor I create called sensor.shopping_list. I know the Joaoapps component is working correctly because I can send a simple text, but it fails if I try to send Template Data. The following script worked in my previous HA install but now fails.

send_shopping_list_via_sms:
  alias: Text Shopping List
  sequence:
  - service: joaoapps_join.myphone_send_sms
    data_template:
      number: 8675309
      message: 'Shopping List:
                                     
        {{ states(''sensor.shopping_list'') }}'

The following errors appear in the log:

  • can only concatenate str (not “int”) to str
  • can only concatenate str (not “dict”) to str
  • can only concatenate str (not “NoneType”) to str
  • can only concatenate str (not “int”) to str

previous HA instance was: 0.106.6
New instance is: 0.107.0

that’s because you messed the message bit up. try this

send_shopping_list_via_sms:
  alias: Text Shopping List
  sequence:
  - service: joaoapps_join.myphone_send_sms
    data_template:
      number: 8675309
      message: >
        Shopping List:{{ states(''sensor.shopping_list'') }}

Thanks, but this does not pass the HA server config check.

Invalid config for [script]: invalid template (TemplateSyntaxError: expected token ‘,’, got ‘sensor’) for dictionary value @ data[‘script’][‘send_shopping_list_via_sms’][‘sequence’][0][‘data_template’][‘message’]. Got “‘Shopping List:{{ states(’‘sensor.shopping_list’‘) }}’\n”. (See /config/configuration.yaml, line 15).

      message: 'Shopping List:
                                     
        {{ states(''sensor.shopping_list'') }}'

https://yaml-multiline.info/

This should be fine. Quotes can be used for multi line strings. I would recommend using a block scalar, though it isn’t needed (it looks better and does some extra formatting for you). That website shows you all the different once you can do. In your case, I’d use the block literal and strip newlines at the end (|-). This will preserve all newlines you put in (so the one between Shopping List and the actual list, and remove any empty lines at the end in case sensor.shopping_list has them.

send_shopping_list_via_sms:
  alias: Text Shopping List
  sequence:
  - service: joaoapps_join.myphone_send_sms
    data_template:
      number: "8675309"
      message: |-
        Shopping List:
              
        {{ states('sensor.shopping_list') }}

But this doesn’t answer your question. Looking at the documentation, in their example, number is passes with quotes. I’d try adding quotes around the phone number maybe? It’s complaining that it can’t append a number to a string. The sensor.shopping_list is a string for sure, so my guess is the phone number is the ‘int’ it’s complaining about.

Awesome! This worked perfectly.

I believe it’s something to do with the fancy quotes around sensor.shopping_list

But, what if i throw a little wrench into this…

passing in the following does not work:

number: "!secret phone_number"

or

number: !secret phone_number

It doesn’t error, but it appears it attempts to send a text to an improper phone number.

shouldn’t work as it’s undocumented

well… create a test automation that creates a persistent notification and use it there to see what’s going on with your number

The 2nd one should work fine. Assuming you have quotes around your phone_number in secrets.yaml.

Thanks. I didn’t have the phone number quoted in secrets.yaml.

I’m using ClickSend with HA which works perfectly: https://www.home-assistant.io/integrations/clicksend/
Might be easier to switch to this if you get stuck.

I believe it’s something to do with the fancy quotes around sensor.shopping_list

That one bugged me forever! The dang gui does single quotes for everything.

But I later learned that a double single quote is perfectly valid and evaluates to a single quote in yaml.

In double quoted strings if you need to include a literal double quote in your string you can escape it by prefixing it with a backslash \ (which you can in turn escape by itself). In single quoted strings the single quote character can be escaped by prefixing it with another single quote, basically doubling it. Backslashes in single quoted strings do not need to be escaped.

https://docs.octoprint.org/en/master/configuration/yaml.html#scalars

So a double single quote is essentialy escaping the quote for you. It keeps the parsing simpler as they don’t need to keep track of double/single quotes and can just put single quotes around everything. Further, the \ doesn’t need to be escaped in single quote strings.

It does make some ugly output though with all those double single quotes!

1 Like