Can i not do this? states_attr in tts

I was trying to do :

service: tts.google_translate_say
data:
  entity_id: media_player.first_bathroom_tv
  message: >-
    {{ [ "Person is currently" + states('sensor.person_status'), "Person B is currently" + states_attr('sensor.personb_status','attribute') ] |random }}

This would alternate between the first and second message where the second should then refer the attribute in person sensor.

But i’m getting :

Template variable error: 'states_attr' is undefined when rendering

state_attr.

Ah nice one. Thank you very much @Troon.

1 Like

Strangely i now see in my logs:

Error rendering data template: TypeError: can only concatenate str (not "NoneType") to str

I wonder, cannot i concatenate attribute with a regular string or have i made another typo…

EDIT: I see what i think the problem is. The sensor i’m reading is the Life360 status of someone and the address attribute. I now see currently the address attribute is null (whereas it wasn’t before).

I suspect that’s what’s causing the Nonetype error.

service: tts.google_translate_say
data:
  entity_id: media_player.first_bathroom_tv
  message: >-
    {{ [ 'Person is currently ' ~ states('sensor.person_status'), 'Person B is currently ' ~ state_attr('sensor.personb_status','attribute') | default('unknown', true) ] | random }}

BTW, the function is named state_attr not states_attr.

1 Like

Yes i changed that upon Troon’s suggestion.

Thanks @123
Could you elaborate on what the ~ does instead of the + ? Also, the addition of default(‘unknown’, true) i suppose sets the default status of the attribute (sensor as well?) to unknown avoiding the problem of the Nulltype concatenation.

And does this unknown default apply to all sensors/attributes now or just that one.

1 Like

“+”

Adds two objects together. Usually the objects are numbers, but if both are strings or lists, you can concatenate them this way. This, however, is not the preferred way to concatenate strings! For string concatenation, have a look-see at the ~ operator. {{ 1 + 1 }} is 2.

“~”

Converts all operands into strings and concatenates them.

{{ "Hello " ~ name ~ "!" }} would return (assuming name is set to 'John') Hello John!.

Taken from the official docs for Jinja

https://jinja.palletsprojects.com/en/2.11.x/templates/#math

EDIT:
And here’s the doc for the “default” filter:

https://jinja.palletsprojects.com/en/2.11.x/templates/#default

1 Like

Love it, thank you.

Just the one you are specifying in the state_attr function.

As for the use of + or ~ that has no bearing on whether your template works or not. It’s simply a good practice to use ~ to specify concatenation because the values are always handled as strings. In contrast, the + operator can perform arithmetic addition or concatenation depending on value’s type. Sometimes that can lead to ambiguity and unexpected results, such as when you want to concatenate (not add) two numeric strings.

1 Like

Thanks and glad you got it solved! :slight_smile: But I was just linking to the documentation from Jinja, that @123 already suggested (by posting the use of it). :slight_smile:

Just for clarification (and for future use), please be so kind and change the solution tag from my post to the one from @123 two posts above mine. :slight_smile: If other users in the future are looking for the solution, that is the post that should be shown, thank you very much! :+1: :slight_smile:

1 Like

Great, many thanks. Learned something new.

Thanks @paddy0174

1 Like