Trying to randomize tts announcements

Dear Community, I am trying to randomize certain tts announcements and I created the following action in the automation:

service: tts.amazon_polly_say
data:
entity_id: media_player.tv_room_speaker
message: >-
{{[ 'Good morning . temperature outside is '{{states(‘sensor.openweathermap_current_temperature’)| round(0) }}‘degree’,
'Hello there . currently it is ‘{{states(‘sensor.openweathermap_condition’) }}’,
]|random }}
cache: false

But I get this error:
Message malformed: template value should be a string for dictionary value @ data[‘action’][2][‘data’]

The sensors itself work well but not when trying to randomize them. I’m sure I’m missing a quote somewhere or something like this. Read through a couple of posts here but can’t figure it out.

Please format your post correctly.

my bad here we go, hope that’s formatted correctly now

service: tts.amazon_polly_say
data:
  entity_id: media_player.tv_room_speaker
  message: >-
    {{[ 'Good morning . temperature outside is '{{states('sensor.openweathermap_current_temperature')| round(0) }}'degree',
    'Hello there . currently it is '{{states('sensor.openweathermap_condition') }}',
    ]|random }}    
  cache: false

Try this:

message: >
   {{ [ 'Good morning. Temperature outside is ' ~ states('sensor.openweathermap_current_temperature')| round(0) ~ ' degrees.', 
         'Hello there. Currently it is ' ~ states('sensor.openweathermap_condition') ]|random }}

Without digging too deep into your code. You are aware of the “Template” section in the developing tools? It gives quite good error messages. If the jinja2 code works there … you are fine.

So amazing how quick I get help here. I’m not really aware of the template section in the developing tools but will give it certainly a try, thanks Ralf.
I tried the new code but still getting an error:
Message malformed: template value should be a string for dictionary value @ data[‘action’][2][‘data’]

service: tts.amazon_polly_say
data:
  entity_id: media_player.tv_room_speaker
  message: >
    {{ [ 'Good morning. Temperature outside is' {{states('sensor.openweathermap_current_temperature')| round(0) }} 'degree', 
         'Hello there. Currently it is' {{states('sensor.openweathermap_condition') }} ]|random }}
  cache: false
enabled: true

I did a sneaky edit of my post. Try what I changed it to.

I’m fairly sure it’s because you double mustache.

Thanks Tom, that works now, thanks so much. I would have never figured it out myself

message: >
   {{ [ "Good morning. Temperature outside is " ~ states('sensor.openweathermap_current_temperature')| round(0) ~ " degree", 
         "Hello there. Currently it is " ~ states('sensor.openweathermap_condition')  ]|random }}
1 Like