Custom messages for automations

Hey guys!

Recently I’m been reading some content about how can I customize and randomize the messages sent to my telegram account by automations.

Actually with help of these links, I found a way to put my ideas to work:

But I still have doubts about how re-use parts of these messages, like greetings and friendly names.

This is an example of automation working:

- id: '1234567890123'
  alias: Custom messaging test
  trigger:
  - at: '19:00'
    platform: time
  condition: []
  action:
  - data:
      message: >
        ⚠️{% if now().strftime("%H")|int < 12 %}Good morning {% elif now().strftime("%H")|int < 18 %}Good afternoon  {% else %}Good evening {% endif %} {{ [ "Mr.","Sir","Boss" ] | random }} ! Can you read me?   
    service: notify.telegram

I needed to put the message inline to actually work, but for better reading:

message: >
        ⚠️ 
        #greetings
        {% if now().strftime("%H")|int < 12 %}
        Good morning 
        {% elif now().strftime("%H")|int < 18 %}
        Good afternoon 
        {% else %}
        Good evening
        {% endif %} 
        #friendly names
        {{ [ "Mr.","Sir","Boss" ] | random }}
        ! Can you read me?

My doubt here is, can I break these greetings and friendly names in files, so I could just call them when necessary on automations?

You can put it into a script file and then call the script from the automation’s action.

BTW, here’s another way to write the template:

        {% set t = now().strftime("%H")|int %}
        {% if t < 12 %} Good morning 
        {% elif t < 18 %} Good afternoon 
        {% else %} Good evening
        {% endif %} 
        {{ [ "Mr.","Sir","Boss" ] | random }}! Can you read me?
1 Like

Hia - and thanks for using my script.

Short answer: No
Longer answer: It is sadly not possible to include multiple script-files, not include a script file in another !included script-file, so like I do here:

- alias: 'JEEVES: Morning Briefing'
  trigger:
    platform: time
    at: '07:00:00'
  condition:
    condition: state
    entity_id: 'binary_sensor.workday_sensor'
    state: 'on'
  action:
    - service: notify.jeeves
      data_template:
        message: !include response_texts/jeeves_morning_briefing.yaml
        target: !secret ole_phone_number

and in the script:
image
it’ll include the text fron the /jeeves_morning_briefing.yaml file, but if I were to use another !include, say in said file, it just responds back the line word for word, as a made up response here:
image
So, unless I’m missing something, it means nested speech scripts are out of the question, yet.

I’m still hoping to get an answer on exactly this in my post you read and linked.
Sadly, when I Google “home-assistant notify parse nested”, I show up as the top answer so far.

Wow much better code, thanks!

Hey! Thank you! Your post helped me a lot!

And yeah, I ended stuck on the same situation…
How are you working around this limitation? A file for each message, with the full sentence? Anything new/different?

Maybe we should transform this on a ticket on Github…

No problem!

Yup, I’m doing a full sentence added file - but then again I don’t have more than a few/10 different notification messages currently, so it’s not a big bother - yet.

I’d love a Github ticket, but I think it’s a complication with the yaml language because it compiles the messages before running the script, resulting in the problematic code.

Can you show me some example and how you call for the file on automations?

This allows you to call for values like entity names from triggers?

Currently, this doesn’t allow me to change variables based on the triggered entities.
I imagine a template would be able to do just that, although I haven’t toyed with those enough to be able to confidently set up one.

I can however show you more automations, such as my sunset notifications:

File: automation/jeeves_sunset_notification.yaml

#Jeeves Sunset notification
- alias: 'JEEVES: Sunset Notification'
  trigger:
    - platform: sun
      event: sunset
  action:
    - service: notify.jeeves
      data:
        message: !include response_texts/jeeves_evening_greeting.yaml
        target: !secret ole_phone_number

File: automation/response_texts/jeeves_evening_greeting.yaml

      >
      {{ [
      "Sundown is upon us, Sir.",
      "The sun has set. Good Evening, Master Wayne.",
      "It is evening now, Sir."
      ] | random }}

Notice the pointy bracket ( > )beginning in the file.
This allows for a multi-line included file.

This is a quite simple automation with one function: When the state of the sun.sun sensor triggers the sunset event state, the automation calls the notify-action I’ve set up called Jeeves.
Jeeves then informs me on my phone with the message, a pre-configured yaml-script with three different outputs.

 "Sundown is upon us, Sir.",
 "The sun has set. Good Evening, Master Wayne.", 
or
 "It is evening now, Sir."

Simple, in truth.

Is this “include” of the yaml with messages also working for tts messages, since i cant get that to work

Bart

Yes it is :slight_smile: You can totally template your TTS messages