Template to show (adjusted selection of) file name as title/message

HI,

please help me making a template to display a selected piece of string in the title / message of an automation notification…

this is the automation action:

action:
  - service: notify.ios_iphone
    data_template:
      title: "Activity Speech"
      message: "Please listen" (change this into) {{ template for filename bit}}
      data:
        push:
          sound: >
            {% if is_state ('input_select.activity','Opstart') %} US-EN-Morgan-Freeman-Welcome-Home.wav
            {% elif is_state ('input_select.activity', 'Opstaan') %} US-EN-Morgan-Freeman-Good-Morning.wav
            {% elif is_state ('input_select.activity','Aan de slag') %} US-EN-Morgan-Freeman-Turning-On-The-Lights.wav
            {% elif is_state ('input_select.activity', 'Home theater') %} US-EN-Morgan-Freeman-Starting-Movie-Mode.wav
            {% elif is_state ('input_select.activity','Gym') %} US-EN-Morgan-Freeman-Motion-In-Game-Room.wav
            {% elif is_state ('input_select.activity', 'Selamat makan') %} US-EN-Morgan-Freeman-Motion-In-Kitchen.wav
            {% elif is_state ('input_select.activity', 'Uit huis') %} US-EN-Morgan-Freeman-Vacate-The-Premises.wav
            {% elif is_state ('input_select.activity', 'Naar bed') %} US-EN-Morgan-Freeman-Good-Night.wav
            {% endif %}

If activity ‘Aan de slag’ would be selected, I would want ‘Turning on the lights’ to appear in the message. So somehow it would need to:

  • select the correct file, (already done by this automation, working fine)
  • filter out the ‘US-EN-Morgan-Freeman-’ and ‘.wav’ bits,
  • change ‘Turning-On-The-Lights’ into Turning on the lights…

Could this be done? Need some of your better templating skills here…

Cheers,
Marius

off top of head I think you have to put the Full path in

something like

\local\Sound\US-EN-Morgan-Freeman-Welcome-Home.wav

local being the www folder
Sound being a subfolder under the www

im new at this to remember seeing somewhere the full path been used

no, thats not the issue, the files are built in the app. and already working i the above action.

next step is to have a template use that string part and transform it into the message …
thx

turning to you @anon43302295 as great templatist , would you know a solution for me, as i am almost there…:

{{states.input_select.speech_sound.state}} gives US-EN-Morgan-Freeman-Welcome-Home.wav

how can i use a template to have a message display Welcome Home.

all .wav files have the same naming convention so the template should cut of ‘US-En-Morgan-Freeman-’ form the beginning and ‘.wav’ at the end.

{{states.input_select.speech_sound.state[21:-4]}}
renders Welcome-Home, which is already fine.

next it should adjust Welcome-Home to Welcome Home

Can this be done? Please have a look?
thx,
Marius

Add |replace('-' , ' ') in to the end of the template?

yes… thank you!
{{states.input_select.speech_sound.state[21:-4]|replace('-' , ' ')}} gives Welcome Home
that seemed too simple, sorry…

now change the Home into home…

for the automation in my first post above i need a related template but i cant figure out what the name of the time is I need:

{% if is_state ('input_select.activity','Opstart') %} US-EN-Morgan-Freeman-Welcome-Home.wav

what template would i need to select the wav file? the data_template for the sound.

I’ve simply tried to enter the full template in the message field :slight_smile:

data_template:
          title: "Activity {{states.input_select.activity.state}} speech"
          message: 
                {% if is_state ('input_select.activity','Opstart') %} US-EN-Morgan-Freeman-Welcome-Home.wav
                {% elif is_state ('input_select.activity', 'Opstaan') %} US-EN-Morgan-Freeman-Good-Morning.wav
                {% elif is_state ('input_select.activity','Aan de slag') %} 'US-EN-Morgan-Freeman-Coworker-Is-Arriving.wav
                {% elif is_state ('input_select.activity', 'Home theater') %} US-EN-Morgan-Freeman-Starting-Movie-Mode.wav
                {% elif is_state ('input_select.activity','Gym') %} US-EN-Morgan-Freeman-Motion-In-Game-Room.wav
                {% elif is_state ('input_select.activity', 'Selamat makan') %} US-EN-Morgan-Freeman-Motion-In-Kitchen.wav
                {% elif is_state ('input_select.activity', 'Uit huis') %} US-EN-Morgan-Freeman-Vacate-The-Premises.wav
                {% elif is_state ('input_select.activity', 'Naar bed') %} US-EN-Morgan-Freeman-Good-Night.wav
                {% endif %}  

which yields:

27

data_template:
          title: "Activity {{states.input_select.activity.state}} speech"
          message: 
                {% if is_state ('input_select.activity','Opstart') %} {{'US-EN-Morgan-Freeman-Welcome-Home.wav'[21:-4]|replace('-' , ' ')}}
                {% elif is_state ('input_select.activity', 'Opstaan') %} {{'US-EN-Morgan-Freeman-Good-Morning.wav'[21:-4]|replace('-' , ' ')}}
                {% elif is_state ('input_select.activity','Aan de slag') %} {{'US-EN-Morgan-Freeman-Coworker-Is-Arriving.wav'[21:-4]|replace('-' , ' ')}}
                {% elif is_state ('input_select.activity', 'Home theater') %} {{'US-EN-Morgan-Freeman-Starting-Movie-Mode.wav'[21:-4]|replace('-' , ' ')}}
                {% elif is_state ('input_select.activity','Gym') %} {{'US-EN-Morgan-Freeman-Motion-In-Game-Room.wav'[21:-4]|replace('-' , ' ')}}
                {% elif is_state ('input_select.activity', 'Selamat makan') %} {{'US-EN-Morgan-Freeman-Motion-In-Kitchen.wav'[21:-4]|replace('-' , ' ')}}
                {% elif is_state ('input_select.activity', 'Uit huis') %} {{'US-EN-Morgan-Freeman-Vacate-The-Premises.wav'[21:-4]|replace('-' , ' ')}}
                {% elif is_state ('input_select.activity', 'Naar bed') %} {{'US-EN-Morgan-Freeman-Good-Night.wav'[21:-4]|replace('-' , ' ')}}
                {% endif %}

yields the desired result…

but now I could have just created a new template with the text edited as desired … seems this should be possible in a more intelligent way…
Cheers,
Marius