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.
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.
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!.
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.
Thanks and glad you got it solved! But I was just linking to the documentation from Jinja, that @123 already suggested (by posting the use of it).
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. If other users in the future are looking for the solution, that is the post that should be shown, thank you very much!