UK MET Office Severe Weather Warnings

Ok a new weather alert was announced this morning, however my automation failed to send me a notification for some reason. The trace shows the following:

and the conditions part of my automation,

alias: Weather Alerts
description: ''
trigger:
  - platform: state
    entity_id: sensor.weather_alerts_devon
condition:
  - condition: template
    value_template: '{{ trigger.to_state.state not in [''unknown'', ''unavailable'',''0''] }}'
  - condition: template
    value_template: '{{ trigger.from_state.state not in [''unknown'', ''unavailable'',''0''] }}'
action:

I took the conditions from the automation code provided earlier in the thread, and when I was manually running the automation a couple of days ago, it was working fine - the status of my alert sensor would have been ‘1’ at the time.

Looking at the second condition - surely having ‘0’ as a state NOT from is incorrect, as normally the sensor state will be ‘0’ when no alerts are present . Should this ‘0’ be removed from the ‘trigger.from_state.state not in’ condition?

If you look at the state value in dev tools for sensor.weather_alerts_devon what value can you see there?

It’s currently showing as a state of ‘1’ which I would expect, and before the alert was generated this morning, the state was showing as ‘0’

Oh sorry, didn’t look at your conditions close enough. You were right that the problem is with the 0 in the second condition. What you currently have means that you will get the notification only when initial value will be different than unknown, unavailable and zero. So in your case the value changed from 0 to 1 so this condition failed. You have to remove 0 from the second condition and should be fine

TBH, I don’t even have second condition in my code as I think it doesn’t really make sense. As long as the value will change TO anything than unkown, unavailable or 0 then can change FROM any value. So change from unkown to 1 in my case will trigger the notification but in yours will not

Ah yes that makes perfect sense thinking about it. I think the conditions in post #76 came from @jchh automation code originally, so perhaps their alert state is not normally 0. Anyway, all sorted now, thanks again :slight_smile:

1 Like

Yes, it is normally 0.

edit: Sorry, I haven’t been following the most recent posts. Please ask anything - if I can help, I will.

Although this automation appears to be running OK, I’ve noticed a couple of things. If I reload my yaml in Home Assistant (to make certain changes in my config update for example) then the push message notification from this automation gets triggered, and has a title, but no content. I believe this is because the state of the trigger sensor changes to ‘blank’ temporarily while the yaml is reloaded. I’ve added in a ’ ’ into the check for the NOT states to resolve this, so

condition:
  - condition: template
    value_template: "{{ trigger.to_state.state not in ['', 'unknown', 'unavailable','0'] }}"

I do also get a warning when the automation triggers, even though it sends the notification through fine:

Template variable warning: 'str object' has no attribute 'link' when rendering '{{ warning.link }}'

How would I resolve this, as it seems to be an issue with the new warning variable setup?

This is weird. I have checked my logs and don’t see any warnings for that. Can you post your current automation script?

Oddly, since clearing the error, and running the automation manually, the error hasn’t appeared again. I might have to wait until the automation gets triggered automatically, but I would have thought it would generate the error every time, even when manually running the automation. Unless it happened when the automation actually triggered by the genuine alert that happened today, and that happened to be immediately when/after I reloaded my yaml in HA, so just a coincidence?! Here’s my automation:

alias: Weather Alerts
description: ""
trigger:
  - platform: state
    entity_id: sensor.weather_alerts_devon
condition:
  - condition: template
    value_template: "{{ trigger.to_state.state not in ['', 'unknown', 'unavailable','0'] }}"
action:
  - service: notify.notify
    data:
      title: ⛅ Met Office
      message: "{{ message }}"
      data:
        actions:
          - action: URI
            title: View Warning
            uri: "{{ warning.link }}"
variables:
  warning: |
    {%- for item in state_attr('sensor.weather_alerts','entries') %}
      {%- for type in [
        ('extreme heat'),('fog'),('ice'),('lightning'),('rain'),('rain, wind'),('snow'),('snow, ice'),('thunderstorm'),('thunderstorms'),('wind')]
        if type == item.summary | regex_findall_index('.*warning of (.*) affecting.*', ignorecase=True) %}
          {% set w = item %}
          {{ w }}
      {%- endfor %}
    {%- endfor %}
  message: |
    {%- for item in state_attr('sensor.weather_alerts','entries') %}
      {%- for type in [
        ('extreme heat'),('fog'),('ice'),('lightning'),('rain'),('rain, wind'),('snow'),('snow, ice'),('thunderstorm'),('thunderstorms'),('wind')]
        if type == item.summary | regex_findall_index('.*warning of (.*) affecting.*', ignorecase=True) %}
        {% set summary = warning.summary | regex_findall_index('(.*) affecting South West England: (.*) valid from (.*) to (.*)', ignorecase=True) %}
            {% set time_from = as_timestamp(strptime(summary[2], "%H%M %a %d %b")) | timestamp_custom("%H:%M") %}
            {% set time_to = as_timestamp(strptime(summary[3], "%H%M %a %d %b")) | timestamp_custom("%H:%M") %}
            {% set date_from = summary[2][5:8] +'-'+ summary[2][9:11] +'-'+ summary[2][12:] %}
            {% set date_to = summary[3][5:8] +'-'+ summary[3][9:11] +'-'+ summary[3][12:] %}
            {% set link = warning.link %}
            <b>⚠️ {{ summary[0] }}</b>
            {{ warning.summary.split(':')[0] | title }}
            {%- if date_from == date_to %}
              {{ date_from }}
            {%- else %}
              {{ date_from }} ➜ {{ date_to }}
            {%- endif %}
            {{ time_from }} ➜ {{ time_to }}
          {%- endfor %}
        {%- endfor %}

I think that may be caused by variables which seems to be set too late. Try to move variables section below trigger like below:

alias: Weather Alerts
description: ""
trigger:
  - platform: state
    entity_id: sensor.weather_alerts_devon
variables:
  warning: |
    {%- for item in state_attr('sensor.weather_alerts','entries') %}
      {%- for type in [
        ('extreme heat'),('fog'),('ice'),('lightning'),('rain'),('rain, wind'),('snow'),('snow, ice'),('thunderstorm'),('thunderstorms'),('wind')]
        if type == item.summary | regex_findall_index('.*warning of (.*) affecting.*', ignorecase=True) %}
          {% set w = item %}
          {{ w }}
      {%- endfor %}
    {%- endfor %}
  message: |
    {%- for item in state_attr('sensor.weather_alerts','entries') %}
      {%- for type in [
        ('extreme heat'),('fog'),('ice'),('lightning'),('rain'),('rain, wind'),('snow'),('snow, ice'),('thunderstorm'),('thunderstorms'),('wind')]
        if type == item.summary | regex_findall_index('.*warning of (.*) affecting.*', ignorecase=True) %}
        {% set summary = warning.summary | regex_findall_index('(.*) affecting South West England: (.*) valid from (.*) to (.*)', ignorecase=True) %}
            {% set time_from = as_timestamp(strptime(summary[2], "%H%M %a %d %b")) | timestamp_custom("%H:%M") %}
            {% set time_to = as_timestamp(strptime(summary[3], "%H%M %a %d %b")) | timestamp_custom("%H:%M") %}
            {% set date_from = summary[2][5:8] +'-'+ summary[2][9:11] +'-'+ summary[2][12:] %}
            {% set date_to = summary[3][5:8] +'-'+ summary[3][9:11] +'-'+ summary[3][12:] %}
            {% set link = warning.link %}
            <b>⚠️ {{ summary[0] }}</b>
            {{ warning.summary.split(':')[0] | title }}
            {%- if date_from == date_to %}
              {{ date_from }}
            {%- else %}
              {{ date_from }} ➜ {{ date_to }}
            {%- endif %}
            {{ time_from }} ➜ {{ time_to }}
          {%- endfor %}
        {%- endfor %}
condition:
  - condition: template
    value_template: "{{ trigger.to_state.state not in ['', 'unknown', 'unavailable','0'] }}"
action:
  - service: notify.notify
    data:
      title: ⛅ Met Office
      message: "{{ message }}"
      data:
        actions:
          - action: URI
            title: View Warning
            uri: "{{ warning.link }}"

Ah Ok thanks, I’ll give that a try. I was under the impression that the variables would be access in the order required by the automation, regardless of the order they actually appear in the yaml. I’ll confirm once I’ve tested this.

No problem, that should also solve the problem with missing content in the notification after HA restart.

Really sorry to keep posting questions here, but I do appreciate the help! I noticed there are 3 weather alerts showing on my dashboard today, however I didn’t get a push notification. Upon checking the logs, I can see 2 errors relating to this automation.
I’m wondering if this is due to having multiple warnings for my region at the same time? The errors are:

Logger: homeassistant.helpers.template
Source: helpers/template.py:2345
First occurred: 21 February 2024 at 12:22:39 (3 occurrences)
Last logged: 09:24:00

Template variable warning: 'str object' has no attribute 'summary' when rendering '{%- for item in state_attr('sensor.weather_alerts','entries') %} {%- for type in [ ('extreme heat'),('fog'),('ice'),('lightning'),('rain'),('rain, wind'),('snow'),('snow, ice'),('thunderstorm'),('thunderstorms'),('wind')] if type == item.summary | regex_findall_index('.*warning of (.*) affecting.*', ignorecase=True) %} {% set summary = warning.summary | regex_findall_index('(.*) affecting South West England: (.*) valid from (.*) to (.*)', ignorecase=True) %} {% set time_from = as_timestamp(strptime(summary[2], "%H%M %a %d %b")) | timestamp_custom("%H:%M") %} {% set time_to = as_timestamp(strptime(summary[3], "%H%M %a %d %b")) | timestamp_custom("%H:%M") %} {% set date_from = summary[2][5:8] +'-'+ summary[2][9:11] +'-'+ summary[2][12:] %} {% set date_to = summary[3][5:8] +'-'+ summary[3][9:11] +'-'+ summary[3][12:] %} {% set link = warning.link %} <b>⚠️ {{ summary[0] }}</b> {{ warning.summary.split(':')[0] | title }} {%- if date_from == date_to %} {{ date_from }} {%- else %} {{ date_from }} ➜ {{ date_to }} {%- endif %} {{ time_from }} ➜ {{ time_to }} {%- endfor %} {%- endfor %}'

and

Logger: homeassistant.components.automation.weather_alerts
Source: components/automation/__init__.py:615
Integration: Automation (documentation, issues)
First occurred: 21 February 2024 at 12:22:39 (3 occurrences)
Last logged: 09:24:00

Error rendering variables: IndexError: list index out of range

The first error is the similar to the error I was getting, before moving the variables section up underneath the trigger section in the automation order as suggested, but now it’s saying that the warning variable has no attribute ‘summary’, whereas before it was saying there was no attribute ‘link’, so there is something not quite right with the ‘warning’ variable still. My current automation:

alias: Weather Alerts
description: ""
trigger:
  - platform: state
    entity_id: sensor.weather_alerts_devon
variables:
  warning: |
    {%- for item in state_attr('sensor.weather_alerts','entries') %}
      {%- for type in [
        ('extreme heat'),('fog'),('ice'),('lightning'),('rain'),('rain, wind'),('snow'),('snow, ice'),('thunderstorm'),('thunderstorms'),('wind')]
        if type == item.summary | regex_findall_index('.*warning of (.*) affecting.*', ignorecase=True) %}
          {% set w = item %}
          {{ w }}
      {%- endfor %}
    {%- endfor %}
  message: |
    {%- for item in state_attr('sensor.weather_alerts','entries') %}
      {%- for type in [
        ('extreme heat'),('fog'),('ice'),('lightning'),('rain'),('rain, wind'),('snow'),('snow, ice'),('thunderstorm'),('thunderstorms'),('wind')]
        if type == item.summary | regex_findall_index('.*warning of (.*) affecting.*', ignorecase=True) %}
        {% set summary = warning.summary | regex_findall_index('(.*) affecting South West England: (.*) valid from (.*) to (.*)', ignorecase=True) %}
            {% set time_from = as_timestamp(strptime(summary[2], "%H%M %a %d %b")) | timestamp_custom("%H:%M") %}
            {% set time_to = as_timestamp(strptime(summary[3], "%H%M %a %d %b")) | timestamp_custom("%H:%M") %}
            {% set date_from = summary[2][5:8] +'-'+ summary[2][9:11] +'-'+ summary[2][12:] %}
            {% set date_to = summary[3][5:8] +'-'+ summary[3][9:11] +'-'+ summary[3][12:] %}
            {% set link = warning.link %}
            <b>⚠️ {{ summary[0] }}</b>
            {{ warning.summary.split(':')[0] | title }}
            {%- if date_from == date_to %}
              {{ date_from }}
            {%- else %}
              {{ date_from }} ➜ {{ date_to }}
            {%- endif %}
            {{ time_from }} ➜ {{ time_to }}
          {%- endfor %}
        {%- endfor %}
condition:
  - condition: template
    value_template: "{{ trigger.to_state.state not in ['', 'unknown', 'unavailable','0'] }}"
action:
  - service: notify.notify
    data:
      title: ⛅ Met Office
      message: "{{ message }}"
      data:
        actions:
          - action: URI
            title: View Warning
            uri: "{{ warning.link }}"

These same two errors generate when I run the automation manually now, and no push notification is generated. For reference, the current ‘state’ of my sensor.weather_alerts is ‘3’ and it has the following attributes:

entries: 
- title: Yellow warning of rain affecting South West England
  title_detail:
    type: text/plain
    language: null
    base: https://www.metoffice.gov.uk/public/data/PWSCache/WarningsRSS/Region/sw
    value: Yellow warning of rain affecting South West England
  links:
    - rel: alternate
      type: text/html
      href: >-
        https://www.metoffice.gov.uk/weather/warnings-and-advice/uk-warnings#?date=2024-02-22&id=166e6f71-9368-488f-9a12-888d240b9674&referrer=rss
    - length: '15285'
      type: image/png
      href: >-
        https://data.consumer-digital.api.metoffice.gov.uk/v1/warnings/rss/image/yellow-rain.png
      rel: enclosure
  link: >-
    https://www.metoffice.gov.uk/weather/warnings-and-advice/uk-warnings#?date=2024-02-22&id=166e6f71-9368-488f-9a12-888d240b9674&referrer=rss
  summary: >-
    Yellow warning of rain affecting South West England: Bath and North East
    Somerset, Bournemouth Christchurch and Poole, Bristol, Dorset,
    Gloucestershire, North Somerset, Somerset, South Gloucestershire, Swindon,
    Wiltshire valid from 0500 Thu 22 Feb to 1800 Thu 22 Feb
  summary_detail:
    type: text/html
    language: null
    base: https://www.metoffice.gov.uk/public/data/PWSCache/WarningsRSS/Region/sw
    value: >-
      Yellow warning of rain affecting South West England: Bath and North East
      Somerset, Bournemouth Christchurch and Poole, Bristol, Dorset,
      Gloucestershire, North Somerset, Somerset, South Gloucestershire, Swindon,
      Wiltshire valid from 0500 Thu 22 Feb to 1800 Thu 22 Feb
  id: >-
    https://www.metoffice.gov.uk/weather/warnings-and-advice/uk-warnings#?date=2024-02-22&id=166e6f71-9368-488f-9a12-888d240b9674&referrer=rss&region=South
    West England
  guidislink: false
- title: Yellow warning of wind affecting South West England
  title_detail:
    type: text/plain
    language: null
    base: https://www.metoffice.gov.uk/public/data/PWSCache/WarningsRSS/Region/sw
    value: Yellow warning of wind affecting South West England
  links:
    - rel: alternate
      type: text/html
      href: >-
        https://www.metoffice.gov.uk/weather/warnings-and-advice/uk-warnings#?date=2024-02-22&id=603a4c83-9114-4648-8840-7e78b26978ca&referrer=rss
    - length: '15776'
      type: image/png
      href: >-
        https://data.consumer-digital.api.metoffice.gov.uk/v1/warnings/rss/image/yellow-wind.png
      rel: enclosure
  link: >-
    https://www.metoffice.gov.uk/weather/warnings-and-advice/uk-warnings#?date=2024-02-22&id=603a4c83-9114-4648-8840-7e78b26978ca&referrer=rss
  summary: >-
    Yellow warning of wind affecting South West England: Bournemouth
    Christchurch and Poole, Dorset, Wiltshire valid from 0800 Thu 22 Feb to 1800
    Thu 22 Feb
  summary_detail:
    type: text/html
    language: null
    base: https://www.metoffice.gov.uk/public/data/PWSCache/WarningsRSS/Region/sw
    value: >-
      Yellow warning of wind affecting South West England: Bournemouth
      Christchurch and Poole, Dorset, Wiltshire valid from 0800 Thu 22 Feb to
      1800 Thu 22 Feb
  id: >-
    https://www.metoffice.gov.uk/weather/warnings-and-advice/uk-warnings#?date=2024-02-22&id=603a4c83-9114-4648-8840-7e78b26978ca&referrer=rss&region=South
    West England
  guidislink: false
- title: Yellow warning of rain affecting South West England
  title_detail:
    type: text/plain
    language: null
    base: https://www.metoffice.gov.uk/public/data/PWSCache/WarningsRSS/Region/sw
    value: Yellow warning of rain affecting South West England
  links:
    - rel: alternate
      type: text/html
      href: >-
        https://www.metoffice.gov.uk/weather/warnings-and-advice/uk-warnings#?date=2024-02-22&id=fbd10cc1-d33f-4cca-90c7-379ed4172ca4&referrer=rss
    - length: '15285'
      type: image/png
      href: >-
        https://data.consumer-digital.api.metoffice.gov.uk/v1/warnings/rss/image/yellow-rain.png
      rel: enclosure
  link: >-
    https://www.metoffice.gov.uk/weather/warnings-and-advice/uk-warnings#?date=2024-02-22&id=fbd10cc1-d33f-4cca-90c7-379ed4172ca4&referrer=rss
  summary: >-
    Yellow warning of rain affecting South West England: Bournemouth
    Christchurch and Poole, Cornwall, Devon, Dorset, Gloucestershire, North
    Somerset, Plymouth, Somerset, South Gloucestershire, Torbay valid from 0300
    Thu 22 Feb to 1400 Thu 22 Feb
  summary_detail:
    type: text/html
    language: null
    base: https://www.metoffice.gov.uk/public/data/PWSCache/WarningsRSS/Region/sw
    value: >-
      Yellow warning of rain affecting South West England: Bournemouth
      Christchurch and Poole, Cornwall, Devon, Dorset, Gloucestershire, North
      Somerset, Plymouth, Somerset, South Gloucestershire, Torbay valid from
      0300 Thu 22 Feb to 1400 Thu 22 Feb
  id: >-
    https://www.metoffice.gov.uk/weather/warnings-and-advice/uk-warnings#?date=2024-02-22&id=fbd10cc1-d33f-4cca-90c7-379ed4172ca4&referrer=rss&region=South
    West England
  guidislink: false

icon: mdi:rss
friendly_name: Weather Alerts

I can see 2 places in the message that are wrong here.
First one is line

{% set summary = warning.summary | regex_findall_index('(.*) affecting South West England: (.*) valid from (.*) to (.*)', ignorecase=True) %}

In the above code you need to change warning.summary to item.summary

Second line would be:

{{ warning.summary.split(':')[0] | title }}

And the same, change warning.summary to item.summary

Second error not sure yet as it is not pointing to any specific line in the automation code. It may be caused by the first error so please update the code and try again.

Ok thanks. I’ve updated those 2 places, and just re-run the automation, and I just get the one error, which is the one I was getting before moving the variables up underneath the trigger:

Logger: homeassistant.helpers.template
Source: helpers/template.py:2345
First occurred: 16:02:54 (1 occurrences)
Last logged: 16:02:54

Template variable warning: 'str object' has no attribute 'link' when rendering '{{ warning.link }}'

current automation:

alias: Weather Alerts
description: ""
trigger:
  - platform: state
    entity_id: sensor.weather_alerts_devon
condition:
  - condition: template
    value_template: "{{ trigger.to_state.state not in ['', 'unknown', 'unavailable','0'] }}"
action:
  - service: notify.notify
    data:
      title: ⛅ Met Office
      message: "{{ message }}"
      data:
        actions:
          - action: URI
            title: View Warning
            uri: "{{ warning.link }}"
variables:
  warning: |
    {%- for item in state_attr('sensor.weather_alerts','entries') %}
      {%- for type in [
        ('extreme heat'),('fog'),('ice'),('lightning'),('rain'),('rain, wind'),('snow'),('snow, ice'),('thunderstorm'),('thunderstorms'),('wind')]
        if type == item.summary | regex_findall_index('.*warning of (.*) affecting.*', ignorecase=True) %}
          {% set w = item %}
          {{ w }}
      {%- endfor %}
    {%- endfor %}
  message: |
    {%- for item in state_attr('sensor.weather_alerts','entries') %}
      {%- for type in [
        ('extreme heat'),('fog'),('ice'),('lightning'),('rain'),('rain, wind'),('snow'),('snow, ice'),('thunderstorm'),('thunderstorms'),('wind')]
        if type == item.summary | regex_findall_index('.*warning of (.*) affecting.*', ignorecase=True) %}
        {% set summary = item.summary | regex_findall_index('(.*) affecting South West England: (.*) valid from (.*) to (.*)', ignorecase=True) %}
            {% set time_from = as_timestamp(strptime(summary[2], "%H%M %a %d %b")) | timestamp_custom("%H:%M") %}
            {% set time_to = as_timestamp(strptime(summary[3], "%H%M %a %d %b")) | timestamp_custom("%H:%M") %}
            {% set date_from = summary[2][5:8] +'-'+ summary[2][9:11] +'-'+ summary[2][12:] %}
            {% set date_to = summary[3][5:8] +'-'+ summary[3][9:11] +'-'+ summary[3][12:] %}
            {% set link = warning.link %}
            <b>⚠️ {{ summary[0] }}</b>
            {{ item.summary.split(':')[0] | title }}
            {%- if date_from == date_to %}
              {{ date_from }}
            {%- else %}
              {{ date_from }} ➜ {{ date_to }}
            {%- endif %}
            {{ time_from }} ➜ {{ time_to }}
          {%- endfor %}
        {%- endfor %}

According to the logs, the error is in the line 2345 of your template.py

helpers/template.py:2345

If it is pointing by any chance to this line:

uri: "{{ warning.link }}"

then definitelly you need to move the variables section above the action one.

I’m not sure why HA keeps moving the variables section back to the bottom, after I move it up underneath the trigger and save the automation. Next time I go in to the automation yaml, the variables have moved to the bottom again!? This is how I’m saving it:

Yeah I was a bit surprised tbh seeing python file instaed of yaml. But even that it shouldn’t move any code by itself. Anyway yaml file is a good way to go. You can then specify the packages location in the HA configuration.yaml like this:

homeassistant:
  packages: !include_dir_named packages/

And keep separate files for each integration in the packages directory. I.e. I have a yaml file called met_office_allerts.yaml which contains similar structure from this post above: UK MET Office Severe Weather Warnings - #51 by jchh

This way you can nicely separate all your integrations and make them easier to maintain.

Sorry I probably wasn’t very clear. I meant that the order of the yaml code within the weather alerts automation itself changes, after I save the updated automation. So I move the ‘variables’ section underneath the ‘trigger’ and ‘condition’ sections, so that the ‘action’ section is the final part of the code, then save. If I exit that part of the GUI, and go back into automations, click on my weather alter automation and go to ‘edit in yaml’, the order of the sections has reverted, and the ‘variables’ section is back at the bottom. This makes me think that HA doesn’t care about the ‘order’ that the yaml within the automation is written, just as long as the syntax is correct.

If that’s the case, then it looks like the template is trying to access attributes of ‘warning’ before they have been properly assigned - namely the link attribute.
Changing the order of the automation yaml isn’t achieving this, so I’ll have to see if I can find a way of only accessing ‘warning.link’ AFTER ‘warning’ has been assigned a value. Possibly…