How to send notifications for DWD weather warnings

Nice tip, I’ll try to change it - did you already tried it this way?

No, not really - but I am only clearing the notifications if there is no warning left (if the DWD-integration does not clear the alerts first which causes the else to be executed)

Yes, tried already and it is working great.

Actually I did a lot of enhancements and it is looking quite complex and dynamic.

I came to idea to use timeout for clearing notification to reduce sending push notifications and save on a daily limit. Timeout is not yet tested, waiting for a bad weather. :slight_smile: , also html color in message for notification is not yet tested.

alias: "Notify: Weather warning"
description: >-
  Supporting multiple trigger entities. Helper input_number used.
trigger:
  - platform: state
    entity_id:
      - sensor.warnwetter_stadt_tralala_current_warning_level
      - sensor.warnwetter_stadt_tralala_advance_warning_level
condition:
  - condition: template
    value_template: "{{ trigger.to_state.state != 'unavailable' }}"
action:
  - variables:
      change: 0
  - if:
      - condition: template
        alias: Anzahl Warnungen prüfen
        value_template: "{{ trigger.to_state.attributes.warning_count|trim |int >= 1}}"
    then:
      - repeat:
          count: "{{ trigger.to_state.attributes.warning_count|trim |int }}"
          sequence:
            - if:
                - condition: template
                  alias: Warnung änderungen prüfen
                  value_template: >-
                    {{ trigger.from_state.attributes["warning_" ~ repeat.index]
                    != trigger.to_state.attributes["warning_" ~ repeat.index] }}
              then:
                - variables:
                    change: 1
      - if:
          - condition: and
            conditions:
              - condition: template
                alias: Warnungen Anzahl änderung prüfen
                value_template: >-
                  {{ trigger.to_state.attributes["warning_count"] ==
                  trigger.from_state.attributes["warning_count"] }}
              - condition: template
                value_template: "{{ change == 0 }}"
              - condition: template
                value_template: >-
                  {{ states('input_number.' ~ trigger.entity_id | replace('.',
                  '_') ~ '_notification_sent') | float == 1.0 }}
        then:
          - stop: No changes in warnings.
      - service: notify.mobile_app_alan_s22
        data:
          message: |-
            {% set count = state_attr(trigger.entity_id, 'warning_count') %}
            {% for i in range(count) %}
              {%- set description = state_attr(trigger.entity_id, 'warning_' ~ loop.index ~ '_description') %}
              {%- set time_start = (state_attr(trigger.entity_id, 'warning_' ~ loop.index ~ '_start')|as_timestamp(now()) )|timestamp_custom('%d.%m.%Y, %R', true, 'N/A') %}
              {%- set time_end = (state_attr(trigger.entity_id, 'warning_' ~ loop.index ~ '_end')|as_timestamp(now()) )|timestamp_custom('%d.%m.%Y, %R', true, 'N/A') %}
              {%- set weekday_start = (state_attr(trigger.entity_id, 'warning_' ~ loop.index ~ '_start')|as_timestamp(now().timestamp()) )|timestamp_custom('%w', true, now().weekday() )|int %}
              {%- set weekday_end = (state_attr(trigger.entity_id, 'warning_' ~ loop.index ~ '_end')|as_timestamp(now().timestamp()) )|timestamp_custom('%w', true, now().weekday() )|int %}
              {%- set headline = state_attr(trigger.entity_id, 'warning_' ~ loop.index ~ '_headline') %}
              {%- set level = state_attr(trigger.entity_id, 'warning_' ~ loop.index ~ '_level') %}
              {%- set color = state_attr(trigger.entity_id, 'warning_' ~ loop.index ~ '_color') %}
              <span style="color: {{ color }}">{{ headline|trim }} - Level {{ level }}</span>
              {{ ['Montag','Dienstag','Mittwoch','Donnerstag','Freitag','Samstag','Sonntag'][weekday_start-1] ~ ', ' ~ time_start ~ ' - ' ~ ['Montag','Dienstag','Mittwoch','Donnerstag','Freitag','Samstag','Sonntag'][weekday_end-1] ~ ', ' ~ time_end }} {{ description|trim }}
            {% endfor %}
          title: |-
            priority: high
            sticky: "true"
            persistent: true
            timeout: |-
              {%- set count = state_attr(trigger.entity_id, 'warning_count') %}
              {%- set max_time_end = 0 %}
              {%- set ns = namespace (max_time_end = 0) %} 
              {% for i in range(count) -%}
                {%- set time_end = state_attr(trigger.entity_id, 'warning_' ~ loop.index ~ '_end')|as_timestamp() %}
                {% if ns.max_time_end < time_end -%}
                  {%- set ns.max_time_end = time_end %}
                {%- endif %}
              {% endfor %}
              {{ (ns.max_time_end - as_timestamp(utcnow())) | int }}
      - service: input_number.set_value
        data:
          value: 1
        target:
          entity_id: >-
            {{ 'input_number.' ~ trigger.entity_id | replace('.', '_') ~
            '_notification_sent' }}
    else:
      - if:
          - condition: template
            value_template: >-
              {{ trigger.to_state.attributes.warning_count !=
              trigger.from_state.attributes.warning_count }}
        then:
          - service: notify.mobile_app_alan_s22
            data:
              message: clear_notification
              data:
                tag: weather_warnings_{{ trigger.entity_id }}
          - service: input_number.set_value
            data:
              value: 0
            target:
              entity_id: >-
                {{ 'input_number.' ~ trigger.entity_id | replace('.', '_') ~
                '_notification_sent' }}
mode: queued
max: 10

The only thing that I needed is input number for every warning sensor, 2 in my case.

# Sensor helpers
sensor_warnwetter_stadt_tralala_current_warning_level_notification_sent:
  name: Warnwetter Stadt Tralala current warning level notification sent
  icon: mdi:weather-lightning-rainy
  initial: 0
  min: 0
  max: 1
sensor_warnwetter_stadt_tralala_advance_warning_level_notification_sent:
  name: Warnwetter Stadt Tralala advance warning level notification sent
  icon: mdi:weather-lightning-rainy
  initial: 0
  min: 0
  max: 1

Not sure if all things are supported on iPhone but on Android it looks nice.

3 Likes

Did some testing with some random city, timeout was not working correct, fixed it and updated my previous post.

For html and coloring message it didn’t worked, not sure why. In history of sent notifications under companion in setting, colors are applied but on actual notification on my android s22 not. Very strange, maybe hex colors are not supported only names like, red, yellow like in example in the documentation: Introduction | Home Assistant Companion Docs

On the other side, colors for notification icon are working fine though.

This is from trace, also looks fine:

service_data:
    message: >-
      <span style="color: #fb8c00">Amtliche WARNUNG vor SCHWEREN STURMBÖEN -
      Level 2</span>
        Samstag, 07.01.2023, 18:00 - Sonntag, 08.01.2023, 10:00 Es treten oberhalb 1000 m schwere Sturmböen mit Geschwindigkeiten zwischen 85 km/h (24 m/s, 47 kn, Bft 9) und 100 km/h (28 m/s, 55 kn, Bft 10) aus südwestlicher Richtung auf.
    title: 'Stadt Wernigerode: Wetterwarnung (1)'

Companion App - Notification history:

Perhaps you can help me, im trying to set up a sensor to proof if there is a dwd-weather warning. In the end i want to proof if there is a warning 33,34,36 or 53 and so on, excluding warning 22,24,82 etc…

The following code doesnt work because of the loop writes for each warning data into the sensor:

      - unique_id: "wetter_dwd_warnung_regen"
        name: "wetter_dwd_warnung_regen"
        state: >
          {% set current_count = state_attr("sensor.dwd_weather_warnings_current_warning_level", "warning_count") %}
          {% set advance_count = state_attr("sensor.dwd_weather_warnings_advance_warning_level", "warning_count") %}
          {% if ((current_count == 0) and (advance_count == 0)) %}
          aus
          {% else %}
          {% for i in range(current_count) %}
          {{ state_attr("sensor.dwd_weather_warnings_current_warning_level", "warning_" ~ loop.index ~ "_description") }}
          {% endfor %}
          {% endif %}

To avoid the loop, i tried to set a variable, but this also dont works:

      - unique_id: "wetter_dwd_warnung_regen"
        name: "wetter_dwd_warnung_regen"
        state: >
          {% set current_count = state_attr("sensor.dwd_weather_warnings_current_warning_level", "warning_count") %}
          {% set advance_count = state_attr("sensor.dwd_weather_warnings_advance_warning_level", "warning_count") %}
          {% if ((current_count == 0) and (advance_count == 0)) %}
          aus
          {% else %}
          {% for i in range(current_count) %}
          {% if state_attr("sensor.dwd_weather_warnings_current_warning_level", "warning_" ~ loop.index ~ "_type") == "64" or "51" | float %}
          {% set warnung = "aktiv" %}
          {% else %}
          {% endif %}
          {% endfor %}
          {{ warnung }}
          {% endif %}

Thank you for your help!

Have you tried testing your code in HA Developer Tools?

This is the code that should work, at least syntax is not complaining any errors.

          {% set current_count = state_attr("sensor.state_attr("sensor.dwd_weather_warnings_current_warning_level", "warning_count") %}
          {% set advance_count = state_attr("sensor.state_attr("sensor.dwd_weather_warnings_advanced_warning_level", "warning_count") %}
          {% if ((current_count == 0) and (advance_count == 0)) %}
            aus
          {% else %}
            {% for i in range(current_count) %}
              {{ state_attr("sensor.state_attr("sensor.dwd_weather_warnings_current_warning_level", "warning_" ~ loop.index ~ "_description") }}
            {% endfor %}
          {% endif %}

Hello, no that didnt work either.
I tried another way which seems to work a little but im not finished yet. What do you think?:

{% set current_count = state_attr("sensor.dwd_weather_warnings_current_warning_level", "warning_count") %}
{% set advance_count = state_attr("sensor.dwd_weather_warnings_advance_warning_level", "warning_count") %}
{% set warnung1 = 0 %}
{% set warnung2 = 0 %}
{% set warnung3 = 0 %}
{% set warnung4 = 0 %}
{% if ((current_count == 0) and (advance_count == 0)) %}
{% set warnung1 = aus %}
{% elif (current_count > 0)  %}
{% set warnung1 = state_attr("sensor.dwd_weather_warnings_current_warning_level", "warning_" ~ 1 ~ "_type") %}
{{ warnung1 }}
{{ "eins" }}
{% endif %}
{% if (current_count > 1) %}
{% set warnung2 = state_attr("sensor.dwd_weather_warnings_current_warning_level", "warning_" ~ 2 ~ "_type") %}
{{ warnung2 }}
{{ "zwei" }}
{% endif %}
{% if (current_count > 2) %}
{% set warnung3 = state_attr("sensor.dwd_weather_warnings_current_warning_level", "warning_" ~ 3 ~ "_type") %}
{{ warnung3 }}
{% elif (current_count > 3) %}
{% set warnung4 = state_attr("sensor.dwd_weather_warnings_current_warning_level", "warning_" ~ 4 ~ "_type") %}
{{ warnung4 }}
{% else %}
{% endif %}
{% set warnungallg = (warnung1, warnung2, warnung3) %}
{{ warnungallg }}
{% if 52 or 12 or 51 in warnungallg %}
{{ "Alarm die Teppiche fliegen weg!" }}
{% else %}
{% endif %}

Today there is warning 38 and 247.
Unfortunately my code doesnt work as it should be:

      - unique_id: "wetter_dwd_warnung_markise"
        name: "wetter_dwd_warnung_markise"
        state: >
          {% set current_count = state_attr("sensor.dwd_weather_warnings_current_warning_level", "warning_count") %}
          {% set advance_count = state_attr("sensor.dwd_weather_warnings_advance_warning_level", "warning_count") %}
          {% set warnung1 = 0 %}
          {% set warnung2 = 0 %}
          {% set warnung3 = 0 %}
          {% set warnung4 = 0 %}
          {% set warnung5 = 0 %}
          {% if (current_count == 0) %}
          Keine Warnung.
          {% else %}
          {% if (current_count > 0) %}
          {% set warnung1 = state_attr("sensor.dwd_weather_warnings_current_warning_level", "warning_" ~ 1 ~ "_type") %}
          {% else %}
          {% endif %}
          {% if (current_count > 1) %}
          {% set warnung2 = state_attr("sensor.dwd_weather_warnings_current_warning_level", "warning_" ~ 2 ~ "_type") %}
          {% else %}
          {% endif %}
          {% if (current_count > 2) %}
          {% set warnung3 = state_attr("sensor.dwd_weather_warnings_current_warning_level", "warning_" ~ 3 ~ "_type") %}
          {% else %}
          {% endif %}
          {% if (current_count > 3) %}
          {% set warnung4 = state_attr("sensor.dwd_weather_warnings_current_warning_level", "warning_" ~ 4 ~ "_type") %}
          {% else %}
          {% endif %}
          {% if (current_count > 4) %}
          {% set warnung5 = state_attr("sensor.dwd_weather_warnings_current_warning_level", "warning_" ~ 5 ~ "_type") %}
          {% else %}
          {% endif %}
          {% set warnungallg = (warnung1, warnung2, warnung3, warnung4, warnung5) %}
          {% if "31" or "33" or "34" or "36" or "39" or "40" or "41" or "42" or "44" or "45" or "46" or "48" or "49" or "51" or "52" or "53" or "54" or "55" or "56" or "61" or "62" or "63" or "64" or "65" or "66" or "90" or "91" or "92" or "93" or "95" or "96" == warnung1 or warnung2 or warnung3 or warnung4 or warnung5 %}
          {{ warnungallg }}
          {% else %}
          Unwichtig
          {% endif %}
          {% endif %}

Even there is no warning-type 38 or 247 selected, the wetter_dwd_warnung_markise sensor shows following warning:

It should be “Unwichtig”. I think the error is somewhere in the line:
{% if "31" or "33" or "34" or "36" or "39" or "40" or "41" or "42" or "44" or "45" or "46" or "48" or "49" or "51" or "52" or "53" or "54" or "55" or "56" or "61" or "62" or "63" or "64" or "65" or "66" or "90" or "91" or "92" or "93" or "95" or "96" == warnung1 or warnung2 or warnung3 or warnung4 or warnung5 %}

Can s.o. pls help. Thank you!

Now i think i got it. I needed to check if array of string exists in another array

      - unique_id: "wetter_dwd_warnung_markise"
        name: "wetter_dwd_warnung_markise"
        state: >
          {% set current_count = state_attr("sensor.dwd_weather_warnings_current_warning_level", "warning_count") %}
          {% set advance_count = state_attr("sensor.dwd_weather_warnings_advance_warning_level", "warning_count") %}
          {% set warnung1 = 0 %}
          {% set warnung2 = 0 %}
          {% set warnung3 = 0 %}
          {% set warnung4 = 0 %}
          {% set warnung5 = 0 %}
          {% if (current_count == 0) %}
          Keine Warnung.
          {% else %}
          {% if (current_count > 0) %}
          {% set warnung1 = state_attr("sensor.dwd_weather_warnings_current_warning_level", "warning_" ~ 1 ~ "_type") %}
          {% else %}
          {% endif %}
          {% if (current_count > 1) %}
          {% set warnung2 = state_attr("sensor.dwd_weather_warnings_current_warning_level", "warning_" ~ 2 ~ "_type") %}
          {% else %}
          {% endif %}
          {% if (current_count > 2) %}
          {% set warnung3 = state_attr("sensor.dwd_weather_warnings_current_warning_level", "warning_" ~ 3 ~ "_type") %}
          {% else %}
          {% endif %}
          {% if (current_count > 3) %}
          {% set warnung4 = state_attr("sensor.dwd_weather_warnings_current_warning_level", "warning_" ~ 4 ~ "_type") %}
          {% else %}
          {% endif %}
          {% if (current_count > 4) %}
          {% set warnung5 = state_attr("sensor.dwd_weather_warnings_current_warning_level", "warning_" ~ 5 ~ "_type") %}
          {% else %}
          {% endif %}
          {% set warnungallg = (warnung1, warnung2, warnung3, warnung4, warnung5) %}
          {% set a = [ 31, 33, 34, 36, 38, 40, 41, 42, 44, 45, 46, 48, 49, 51, 52, 53, 54, 55, 56, 61, 62, 63, 64, 65, 66, 90, 91, 92, 93, 95, 96 ] %}
          {% set result = namespace(value=0) %}
          {% for item in a %}
            {% if item in warnungallg %} 
            {% set result.value = result.value + 1 %} 
            {% endif %}
          {% endfor %}
          {{ result.value }}
          {% endif %}

Hi @alanmilinovic,

first of all, thank you for your code. After a few small adjustments, the notification also works for iOS devices.
A similar question was already asked here, but with completely different code. I would like to use your code variant but also hide some warnings, e.g. when it gets hot or cold.
Can you possibly help me with this?

Thanks in advance
Greetings from Germany
Tom

Hi @alanmilinovic,

one more thing I noticed.
Every 15 minutes I get a push message on the iPhone with one and the same warning. When I look at the entity it is also updated every 15 minutes, but the value does not change.
As I understood your code, exactly then NO push should be sent… Do you have any idea what it could be?

Thanks a lot
Tom

Do you have input_number configured that is preventing from sending notification over and over again if there are no changes.

Hi @alanmilinovic,

thanks for you quick reply :slight_smile:
Can you give me a quick tip on how/where to do this? I am unfortunately still super fresh in such things…

Thanks a lot
Tom

`alias: Benachrichtigung / Wetterwarnungen
description: Supporting multiple trigger entities. Helper input_number used.
trigger:
  - platform: state
    entity_id:
      - sensor.dwd_weather_warnings_805382076_current_warning_level
      - sensor.dwd_weather_warnings_805382076_advance_warning_level
condition:
  - condition: template
    value_template: "{{ trigger.to_state.state != 'unavailable' }}"
action:
  - variables:
      change: 0
  - if:
      - condition: template
        alias: Anzahl Warnungen prüfen
        value_template: "{{ trigger.to_state.attributes.warning_count|trim |int >= 1}}"
    then:
      - repeat:
          count: "{{ trigger.to_state.attributes.warning_count|trim |int }}"
          sequence:
            - if:
                - condition: template
                  alias: Warnung änderungen prüfen
                  value_template: >-
                    {{ trigger.from_state.attributes["warning_" ~ repeat.index]
                    != trigger.to_state.attributes["warning_" ~ repeat.index] }}
              then:
                - variables:
                    change: 1
      - if:
          - condition: and
            conditions:
              - condition: template
                alias: Warnungen Anzahl änderung prüfen
                value_template: >-
                  {{ trigger.to_state.attributes["warning_count"] ==
                  trigger.from_state.attributes["warning_count"] }}
              - condition: template
                value_template: "{{ change == 0 }}"
              - condition: template
                value_template: >-
                  {{ states('input_number.' ~ trigger.entity_id | replace('.',
                  '_') ~ '_notification_sent') | float == 1.0 }}
        then:
          - stop: No changes in warnings.
      - service: notify.benachrichtigungen_alle
        data:
          title: WETTERWARNUNG
          message: |-
            {% set count = state_attr(trigger.entity_id, 'warning_count') %}
            {% for i in range(count) %}
              {%- set description = state_attr(trigger.entity_id, 'warning_' ~ loop.index ~ '_description') %}
              {%- set time_start = (state_attr(trigger.entity_id, 'warning_' ~ loop.index ~ '_start')|as_timestamp(now()) )|timestamp_custom('%d.%m.%Y, %R', true, 'N/A') %}
              {%- set time_end = (state_attr(trigger.entity_id, 'warning_' ~ loop.index ~ '_end')|as_timestamp(now()) )|timestamp_custom('%d.%m.%Y, %R', true, 'N/A') %}
              {%- set weekday_start = (state_attr(trigger.entity_id, 'warning_' ~ loop.index ~ '_start')|as_timestamp(now().timestamp()) )|timestamp_custom('%w', true, now().weekday() )|int %}
              {%- set weekday_end = (state_attr(trigger.entity_id, 'warning_' ~ loop.index ~ '_end')|as_timestamp(now().timestamp()) )|timestamp_custom('%w', true, now().weekday() )|int %}
              {%- set headline = state_attr(trigger.entity_id, 'warning_' ~ loop.index ~ '_headline') %}
              {%- set level = state_attr(trigger.entity_id, 'warning_' ~ loop.index ~ '_level') %}
              {%- set color = state_attr(trigger.entity_id, 'warning_' ~ loop.index ~ '_color') %}
              {{ ['Montag','Dienstag','Mittwoch','Donnerstag','Freitag','Samstag','Sonntag'][weekday_start-1] ~ ', ' ~ time_start ~ ' - ' ~ ['Montag','Dienstag','Mittwoch','Donnerstag','Freitag','Samstag','Sonntag'][weekday_end-1] ~ ', ' ~ time_end }} {{ description|trim }}
            {% endfor %}
            timeout: |-
              {%- set count = state_attr(trigger.entity_id, 'warning_count') %}
              {%- set max_time_end = 0 %}
              {%- set ns = namespace (max_time_end = 0) %} 
              {% for i in range(count) -%}
                {%- set time_end = state_attr(trigger.entity_id, 'warning_' ~ loop.index ~ '_end')|as_timestamp() %}
                {% if ns.max_time_end < time_end -%}
                  {%- set ns.max_time_end = time_end %}
                {%- endif %}
              {% endfor %}
              {{ (ns.max_time_end - as_timestamp(utcnow())) | int }}
      - service: input_number.set_value
        data:
          value: 1
        target:
          entity_id: >-
            {{ 'input_number.' ~ trigger.entity_id | replace('.', '_') ~
            '_notification_sent' }}
    else:
      - if:
          - condition: template
            value_template: >-
              {{ trigger.to_state.attributes.warning_count !=
              trigger.from_state.attributes.warning_count }}
        then:
          - service: notify.benachrichtigungen_alle
            data:
              message: clear_notification
              data:
                tag: weather_warnings_{{ trigger.entity_id }}
          - service: input_number.set_value
            data:
              value: 0
            target:
              entity_id: >-
                {{ 'input_number.' ~ trigger.entity_id | replace('.', '_') ~
                '_notification_sent' }}
mode: queued
max: 10
`

Not sure what you would like achieve, need more details.

I have added “my” modified code to my post.
Where or in what can I change the input number so that I don’t get the same notification over and over again? Maybe you can show me the code line.

You need to have this in: /config/input_numbers.yaml

# Sensor helpers
sensor_dwd_weather_warnings_805382076_current_warning_level_current_warning_level_notification_sent:
  name: Warnwetter Stadt 805382076 current warning level notification sent
  icon: mdi:weather-lightning-rainy
  initial: 0
  min: 0
  max: 2
sensor_dwd_weather_warnings_805382076_advance_warning_level_advance_warning_level_notification_sent:
  name: Warnwetter Stadt 805382076 advance warning level notification sent
  icon: mdi:weather-lightning-rainy
  initial: 0
  min: 0
  max: 2

Okay, thank you! The file did not exist yet, but I have created it now.
Then I observe now whether the message appear again.

Thanks a lot

You must also have this input_number: !include input_numbers.yaml in /config/configuration.yaml.

1 Like

Thank you very much! And sorry for these beginner questions. But so little by little I understand the construct more and more :slight_smile:

…unfortunately rejoiced too soon. Every 15 minutes comes the push message.
Code is unchanged as above, additional yaml file created and configuration.yaml added.

And Home Assistant restarted?