Sensor if there is a current DWD warning

The German DWD provides weather event data, for which there is already an integration in HA. However, the warnings have timestamps and to get the information, if the warning applies for this very moment, you need to analyze the data a bit. I have created a template, which exactly does that. Feedback is welcome :slight_smile:

{% set dwd_warning = namespace(found=false)%}
{% if state_attr('sensor.dwd_weather_warnings_current_warning_level', 'warning_count') > 0 %}
{% for i in range(1, state_attr('sensor.dwd_weather_warnings_current_warning_level', 'warning_count')+1) %}
{% set start = as_timestamp(state_attr('sensor.dwd_weather_warnings_current_warning_level', 'warning_'~i~'_start'))%}
{% set end = as_timestamp(state_attr('sensor.dwd_weather_warnings_current_warning_level', 'warning_'~i~'_end'))%}
{% if start < as_timestamp(now()) < end %}
{% set dwd_warning.found = true %}
{%endif%}
{%endfor%}
{%endif%}
{{dwd_warning.found}}

I use something similar in order to count the active weather warnings:

  wetterwarnungen:
	friendly_name: "Wetterwarnungen"      
	icon_template: mdi:weather-partly-lightning
	value_template: >-
	  {% set ns = namespace(found=0) %}          
	  {% set current_count = state_attr("sensor.wetterwarnungen_current_warning_level", "warning_count") %}
	  {% set advance_count = state_attr("sensor.wetterwarnungen_advance_warning_level", "warning_count") %}

	  {% if current_count > 0 or advance_count > 0 %}         
		{% for i in range(current_count) %}
		  {% set startTime = as_timestamp(state_attr("sensor.wetterwarnungen_current_warning_level", "warning_" ~ loop.index ~ "_start")) %}
		  {% set endTime = as_timestamp(state_attr("sensor.wetterwarnungen_current_warning_level", "warning_" ~ loop.index ~ "_end")) %}
		
		  {% if startTime < as_timestamp(now()) < endTime %}
			{% set ns.found = ns.found + 1 -%}
		  {%endif%}
		{% endfor %}

		{% for i in range(advance_count) %}
		  {% set startTime = as_timestamp(state_attr("sensor.wetterwarnungen_advance_warning_level", "warning_" ~ loop.index ~ "_start")) %}
		  {% set endTime = as_timestamp(state_attr("sensor.wetterwarnungen_advance_warning_level", "warning_" ~ loop.index ~ "_end")) %}
		  
		  {% if startTime < as_timestamp(now()) < endTime %}
			{% set ns.found = ns.found + 1 -%}
		  {%endif%}
		{% endfor %}              
	  {% endif %}                
	  
	  {{ ns.found }}   

Is there a reason you are comparing for “now()” twice?

{% if start < as_timestamp(now()) < as_timestamp(now()) < end %}

Yes, you are right. It does not add anything and may lead to confusion. I adjusted it. Thanks!

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!