Templates usage for Dynamic Strings in Notification Messages

Hello,
As requesting I am creating a new topic for my question.
I currently use a few Google TTS related automations which are sending notification messages (long dynamic string with few sentences including some inline jinja code), f.e. weather_forecast/daily_plan in local language.
Automations have multiple copies because of different triggers, conditions and//or output speaker device.
But the notifications parameter “message” is the same.
It’s a very long string with some jinja code to include selected sensors state.

I tried to use the new Template feature to remove duplicity of the notification message parameter between similar automations (use and maintain 1 copy of “message” parameter used by multiple automation).

Moved the “message” string to a file /config/custom_templates/tts_message_weather_forecast.jinja and
tried to replace the notification “message” parameter in automations with {% include 'tts_message_weather_forecast.jinja' %} but receiving an error “Template not found”.

Is it possible to use the new HA 2023.4 templating feature somehow for definition of 1 dynamic message string used for sending notification messages via multiple automations?

Notification parameter Message content saved as tts_message_weather_forecast.jinja file follows:

    Mám pre Vás informácie o počasí. Vonku je práve    
    {{states.sensor.openweathermap_weather.state}} a    
    {{states.sensor.openweathermap_temperature.state|round}} °, pocitová
    teplota    
    {{states.sensor.openweathermap_feels_like_temperature.state|round}} °,    
    vlhkosť vzduchu {{states.sensor.openweathermap_humidity.state|round}} %. 
    {%     set condition =
    (states.sensor.openweathermap_forecast_condition.state) %}      {% set
    conditions = {'clear-night': 'jasná noc', 'cloudy': 'zamračené',    
    'exceptional': 'výnimočne', 'few clouds': 'slabá oblačnosť', 'fog':    
    'hmlisto', 'hail': 'krupobitie', 'lightning': 'búrky s bleskami',    
    'lightning-rainy': 'búrky s dažďom', 'partlycloudy': 'čiastočne
    zamračené',     'pouring': 'silný lejak', 'rainy': 'dážď', 'snowy': 'padanie
    snehu',     'snowy-rainy': 'dážď so snehom', 'sunny': 'slnečno', 'windy':
    'veterno',     'windy-variant': 'premenlivý vietor'} %} Predpoveď na dnešný
    deň je {{     conditions[condition] }}, najvyššia teplota  dosiahne    
    {{states.sensor.openweathermap_forecast_temperature.state|round}} ° {%
    if     states.sensor.openweathermap_forecast_temperature_low.state ==
    "unknown" %}     . {% else %} a najnižšia klesne na    
    {{states.sensor.openweathermap_forecast_temperature_low.state|round}} °.
    {%     endif %}  {% if    
    states.sensor.openweathermap_forecast_precipitation_probability.state|int
    >     0 %} S pravdepodobnosťou {{    
    states.sensor.openweathermap_forecast_precipitation_probability.state }}
    %     by mohlo spadnúť {{    
    states.sensor.openweathermap_forecast_precipitation.state }} milimetrov    
    zrážok {% endif %} {% if    
    states.sensor.openweathermap_precipitation_kind.state == "None" %} . {%
    else     %}  {% set precipitation =    
    (states.sensor.openweathermap_precipitation_kind.state) %} {% set    
    precipitations = {'Rain': 'dažďa', 'Snow': 'snehu', 'Snow-rain': 'dažďa
    so     snehom'} %} vo forme {{ precipitations[precipitation] }}. {% endif %}
    {% if     states.sensor.openweathermap_forecast_wind_speed.state|int > 5 %}
    Vietor by     mohol dosiahnuť rýchlosť až {{    
    states.sensor.openweathermap_forecast_wind_speed.state|int*3.6 }} km/h.
    {%     endif %}       {% set ts_forecast =
    (as_timestamp(states.sensor.openweathermap_forecast_time.state)) | int
    %}       {% set ts_now = (as_timestamp(now())) | int %}       {% set
    duration = ts_forecast - ts_now  %}       {% set seconds = duration % 60
    %}       {% set minutes = (duration / 60)|int % 60 %}       {% set hours =
    (duration / 3600)|int %}       Platnosť predpovede {% if duration < 0 %} {%
    set duration = -1 * duration %} skončila pred časom {% else %} skončí za čas
    {% endif %} {{ duration | timestamp_custom("%H:%M:%S", 0) }}

Thanks, let’s see if we can solve this.

First, a few things to make this easier to work with.

Reduce your problem to the smallest but complete amount or code that shows the problem. This is called a minimal example. This ensures nobody gets confused by things that don’t matter.

In this case, shorten your template file as much as possible: Just have one very short message and perhaps one parameter.

Also.post one automation where you are using this.

From there, one can build it up.

OK, I have created a minimal version for testing.
It’s using sun.sun sensor but any other could be used.

Template file “test_message.jinja” content:

    This is a template dynamic message. 
    Sun is currently {{states.sun.sun.state}} .

Testing automation “TEST - dynamic message”

alias: TEST - Dynamic message
description: ""
trigger: []
condition: []
action:
  - service: notify.persistent_notification
    data:
      title: Local message
      message: >-
        This is a local dynamic message.  Sun is currently
        {{states.sun.sun.state}} .
  - service: notify.persistent_notification
    data:
      title: Template message
      message: "{% include 'test_message.jinja' %}"
mode: single

Automations shoudl create 2 messages, 1 local and 1 template.
The second message is failing with error “template not found”.

OK, it seems to work after HA full restart :slight_smile:
Jinja file was not found if only refreshed via “Developer Tools” / “Template Entities”…

Ok, good. That template entities button only reloads template YAML, not Jinja. If you want to (re)load during runtime, you need to navigate to services under the dev tools and call the homeassistant.reload_custom templates service.

1 Like