Templating question in automation using trigger.event

I hate to bump this but I have been looking at it for days and either I am doing something stupid or there is something very wrong with HA (obviously I suspect the former).

Also, tagging @petro, @anon43302295, @Mariusthvdb may not be appreciated but this feels like just the kind of thing you generally solve. But I do apologise and feel free to ignore this if you want to.

(And of course any general improvements or recommendations are appreciated too.)


I have three automations that have identical triggers.

  • one creates a persistant notification,
  • one sets an input_text,
  • the other sets an input_datetime,

All achieve this by extracting from and reformatting trigger.event.data.description.

My first issue is that one of these automations doesn’t actually fire when the trigger occurs. Any suggestions as to why this might be?

all them trigger like this:

trigger:
  - platform: event
    event_type: feedreader


The second issue is that even one of the automations that does fire doesn’t work even though in the template test tool, both work perfectly.

This is the persistant notification automation which always fires and always works.

automation:
  - alias: 'Alert From Weather RSS Feed'
    initial_state: on
    trigger:
      - platform: event
        event_type: feedreader

    action:
      - service: script.notify
        data_template:
          show: True
          message: "{{ trigger.event.data.description }}"
          title: "{{ trigger.event.data.title }}"

This is the automation that never fires and in the next post is the automation that fires but doesn’t work (except in the template tool - it is long so had to be a second post).

In case anyone wants to try these here is an example of trigger.event.data.description

“Yellow Warning of Snow And Ice affecting England (London & South East England) : Buckinghamshire, Milton Keynes, Oxfordshire valid from 0900 Sat 15 Dec to 0900 Sun 16 Dec”

  #====================
  #=== Set expiry time
  #====================
  - alias: Met Office Severe Weather Warning Expiry
    initial_state: on
    trigger:
      - platform: event
        event_type: feedreader

    action:
      - service: input_datetime.set_datetime
        data_template:
          entity_id: input_datetime.met_office_severe_weather_warning_expiry
          data:
            time: >
              {% set warning = trigger.event.data.description %}

              {% set valid_to = warning.split('from ')[1].split(' to')[0] %}
              {% set valid_to_hour = valid_to.split(' ')[0][0:2] %}
              {% set valid_to_minutes = valid_to.split(' ')[0][2:4] %}

              {{ valid_to_hour }}:{{ valid_to_minutes }}

            date: >
              {% set warning = trigger.event.data.description %}

              {% set months_map = {
                            'Jan': '01', 'Feb': '02', 'Mar': '03', 'Apr': '04', 'May': '05', 'Jun': '06', 
                            'Jul': '07', 'Aug': '08', 'Sep': '09', 'Oct': '10', 'Nov': '11', 'Dec': '12' } %}

              {% set valid_to = warning.split('from ')[1].split(' to')[0] %}
              {% set valid_to_date = valid_to.split(' ')[-2] %}
              {% set valid_to_month = months_map[valid_to.split(' ')[-1]] %}
                
              {{ now().year }}-{{ valid_to_month }}-{{ valid_to_date }}

EDIT: I halved the size of this automation with a macro. It still doesn’t work in HA even though it does in the template tool :frowning:

  #============================================================
  #=== Format the weather warning suitable for an announcement
  #============================================================
  - alias: Met Office Severe Weather Warning
    initial_state: on
    trigger:
      - platform: event
        event_type: feedreader

    action:
      - service: input_text.set_value
        data_template:
          entity_id: input_text.met_office_severe_weather_warning
          value: >
            {% set warning = trigger.event.data.description %}

            {#-- Extract warning type --#}
            {% set warning_type = warning.split(' : ')[0].split(' affecting ')[0] %}

            {#-- Extract location --#}
            {% set location = warning.split(' : ')[1] %}
            {% set location = location.split(' valid')[0] %}
            {% set last_comma = location.rfind(',') %}
            {% set location = location[:last_comma] + ' and' + location[last_comma + 1:] %}

            {% set days_map = { 'Sat': 'Saturday', 'Sun': 'Sunday', 'Mon': 'Monday', 'Tue': 'Tuesday',
                                'Wed': 'Wednesday', 'Thu': 'Friday', 'Fri': 'Friday'} %}

            {% set months_map = { 'Jan': 'January', 'Feb': 'February', 'Mar': 'March', 'Apr': 'April',
                                  'May': 'May', 'Jun': 'June', 'Jul': 'July', 'Aug': 'August',
                                  'Sep': 'September', 'Oct': 'October', 'Nov': 'November', 'Dec': 'December'} %}

            {#-- Macro to build the valid from and to phrases --#}
            {% macro valid_from_to(validity_time) %}

              {#-- Extract hour --#}
              {%- set validity_hour = validity_time.split(' ')[0][0:2] | int %}

              {#-- Get period of the day --#}
              {%- if validity_hour | int < 12 %}
                {%- set validity_day_period = "morning" %}
              {%- elif validity_hour | int > 16 %}
                {%- set validity_day_period = "evening" %}
              {%- else %}
                {%- set validity_day_period = "afternoon" %}
              {%- endif %}

              {#-- Convert time to 12 hour format --#}
              {%- if validity_hour | int > 12 %}
                {%- set validity_hour = validity_hour | int - 12 %}
              {%- endif %}

              {#-- Extract minutes --#}              
              {%- set validity_minutes = validity_time.split(' ')[0][2:4] | int %}
              {%- if validity_minutes < 10 %}
                {%- set validity_minutes = "o'clock" %}
              {%- endif %}

              {#-- Extract day, month and date --#}
              {%- set validity_day = days_map[validity_time.split(' ')[1][0:3]] %}
              {%- set validity_month = months_map[validity_time.split(' ')[-1]] %}
              {%- set validity_date = validity_time.split(' ')[-2] %}

              {#-- Get suffix for date --#}
              {%- if validity_date  in (1, 21, 31) %}
                {%- set suffix = 'st ' %}
              {%- elif validity_date   in (2, 22) %}
                {%- set suffix = 'nd ' %}
              {%- elif validity_date   in (3, 23) %}
                {%- set suffix = 'rd ' %}
              {%- else %}
                {%- set suffix = 'th ' %}
              {%- endif %}

              {#-- build the final phrase --#}
              {%- if as_timestamp(now()) | timestamp_custom('%a %d %b') == validity_time[5:] -%}
                {%- set phrase = validity_hour ~ ' ' ~ validity_minutes ~ ' this ' ~ validity_day_period -%}
              {%- elif now().day + 1 == validity_date | int -%}
                {%- set phrase = validity_hour ~ ' ' ~ validity_minutes ~ ' tomorrow ' ~ validity_day_period -%}
              {%- elif now().day - 1 == validity_date | int -%}
                {%- set phrase = validity_hour ~ ' ' ~ validity_minutes ~ ' yesterday ' ~ validity_day_period -%}
              {%- else -%}
                {%- set phrase = validity_hour ~ ' ' ~ validity_minutes ~ ' on ' ~ validity_day ~ ' the ' ~ validity_date ~ suffix ~ 'of ' ~ validity_month -%}
              {%- endif -%}

              {{ phrase }}

            {%- endmacro %}

            {#-- Extract the valid to and valid from time --#}
            {%- set valid_from = warning.split('from ')[1].split(' to')[0] %}
            {%- set valid_to = warning.split('from ')[1].split('to ')[1] %}

            {#-- Get the valid to and valid from phrases --#}
            {%- set to = valid_from_to(valid_to) %}
            {%- set from = valid_from_to(valid_from) -%}

            The Met Office have issued a {{ warning_type }} affecting {{ location }}, valid from {{ from }} to {{ to }}

Indenting seems to be incorrect for initial_state , trigger and action. In your other automations they are aligned with alias.

#====================
#=== Set expiry time
#====================
- alias: Met Office Severe Weather Warning Expiry
    initial_state: on
    trigger:
      - platform: event
        event_type: feedreader

That has somehow happened in the copy and paste.
The indenting is correct in my config and I have edited my original post.

Whats firing event feedreader? Do you have a line from the log that shows the event and it’s data?

feedreader fires when the Met Office issues an RSS feed.
It happened last night and I have restarted since then so can’t show anything from the log but the automation ‘Alert From Weather RSS Feed’ does this which shows you the data.

Thanks for looking.

image

Yeah but without the full dictionary event, not much help can be done. You should turn on INFO and watch the logs for the event. Once you see the event. Post the line that contains the event here.

not yet chiming in, but just to let you know i didn’t receive a notification about your tagging me…

reading up as type this though.

OK I have restarted with INFO and I get four lines in the log. Is this what you are after?

2018-12-17 15:23:14 INFO (MainThread) [homeassistant.setup] Setting up feedreader

2018-12-17 15:23:21 INFO (MainThread) [homeassistant.setup] Setup of domain feedreader took 7.1 seconds.

2018-12-17 15:23:55 INFO (SyncWorker_13) [homeassistant.components.feedreader] Fetching new data from feed http://metoffice.gov.uk/public/data/PWSCache/WarningsRSS/Region/se

2018-12-17 15:23:55 INFO (SyncWorker_13) [homeassistant.components.feedreader] Fetch from feed http://metoffice.gov.uk/public/data/PWSCache/WarningsRSS/Region/se completed

I know my post is long and maybe hard to follow so just to be clear, the contents of:

trigger.event.data.description and trigger.event.data.title

can be reliably inferred from the persistent notification as pictured above and the automation that created it.

Same issue!