Hey, I’ve got a script that is outputting a message "{{ tonights_dinner.events[0].summary }}"
. Since I’m trying to get Alexa Media Player to speak that message, though, it has problems if it contains an ampersand. So whenever I have the string " & "
I want to replace it with " and "
. Which seems like it should be "{{ tonights_dinner.events[0].summary | replace(' & ', ' and ') }}"
but something in the yaml really doesn’t like the ampersand, and I can’t figure out how to escape it. It’s working just fine if it doesn’t have an ampersand.
Try it like this:
message: >
{{ tonights_dinner.events[0].summary | replace(' "&" ', ' and ') }}
Does it matter that this is inside a script? I’m getting a syntax error on the second line of your code: “missed comma between flow collection entries (710:38)” where line 710 is {{ tonights_dinner.events[0].summary | replace(' "&" ', ' and ') }}
Maybe it will help if I include more of my code. Here’s the whole script:
dinner_calendar:
mode: restart
sequence:
- service: alexa_media.update_last_called
- delay: 00:00:01
- service: calendar.list_events
data:
start_date_time: "{{ today_at().strftime('%Y-%m-%d %H:%M') }}"
duration:
hours: 24
target:
entity_id: calendar.dinner
response_variable: tonights_dinner
- if:
- alias: "there is nothing scheduled"
condition: template
value_template: "{{ tonights_dinner.events|count == 0 }}"
then:
- service: notify.alexa_media
data_template:
message: "Sorry, tonight's dinner is not in the calendar."
title: "Dinner"
target: '{{ states.sensor.last_alexa.state }}'
data:
type: tts
else:
- service: notify.alexa_media
data_template:
message: "{{ tonights_dinner.events[0].summary }}"
title: "Dinner"
target: '{{ states.sensor.last_alexa.state }}'
data:
type: tts
It’s probably more code than you need, but I’m trying to replace that last message
line.
Okay, with your replacement, the code looks like this:
dinner_calendar:
mode: restart
sequence:
- service: alexa_media.update_last_called
- delay: 00:00:01
- service: calendar.list_events
data:
start_date_time: "{{ today_at().strftime('%Y-%m-%d %H:%M') }}"
duration:
hours: 24
target:
entity_id: calendar.dinner
response_variable: tonights_dinner
- if:
- alias: "there is nothing scheduled"
condition: template
value_template: "{{ tonights_dinner.events|count == 0 }}"
then:
- service: notify.alexa_media
data_template:
message: "Sorry, tonight's dinner is not in the calendar."
title: "Dinner"
target: '{{ states.sensor.last_alexa.state }}'
data:
type: tts
else:
- service: notify.alexa_media
data_template:
message: >
{{ tonights_dinner.events[0].summary|replace('&', 'and' }}
title: "Dinner"
target: '{{ states.sensor.last_alexa.state }}'
data:
type: tts
While this appears to validate properly, when I restart my configuration, I get the following error in the log file:
Script with object id ‘dinner_calendar’ could not be validated and has been disabled: template value should be a string for dictionary value @ data[‘sequence’][3][‘else’][0][‘data_template’]. Got {‘message’: “{{ tonights_dinner.events[0].summary|replace(’&’, ‘and’ }}\n”, ‘title’: ‘Dinner’, ‘target’: ‘{{ states.sensor.last_alexa.state }}’, ‘data’: {‘type’: ‘tts’}}
Thanks for helping. Most of my knowledge is in JavaScript and PHP, and Jinja is different enough that it’s like swimming upstream.
Dang it, I was missing a closing parenth. I see it now. Thanks, it works!