xlcnd / meteoalarmeu

A 'custom component' for Home-Assistant for Weather Warnings from meteoalarm.eu

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Loop through multiple alerts

mcshosho opened this issue · comments

It is a question actually: in case that there are more alerts at same time, how can I loop though them and make separate notification foe each alert? I see an attribute with number of alerts, but the rest of the attributes are not list, so I am not sure how can I loop through them and make distinct notifications.

alert_id: 2577399750
country: RO
region: Bucuresti
awareness_type: Extreme low temperature
awareness_level: Yellow
from: '2021-02-11 12:00'
until: '2021-02-15 10:00'
message: 'româna: Temperaturi foarte coborâte. english: Very low temperatures.'
message_id: 3195067993
published: '2021-02-11 12:12'
alert_id_1: 2224738174
country_1: RO
region_1: Bucuresti
awareness_type_1: Wind
awareness_level_1: Yellow
from_1: '2021-02-11 12:00'
until_1: '2021-02-12 18:00'
message_1: >-
  româna: Vântul va avea intensificări, cu viteze de 55...65 km/h și temporar
  peste 70...80 km/h english: Wind gusts will reach 55...65 km/h and temporary
  70...80 km/h.
message_id_1: 2166280796
published_1: '2021-02-11 12:12'
alert_id_2: 34520528
country_2: RO
region_2: Bucuresti
awareness_type_2: Snow/Ice
awareness_level_2: Yellow
from_2: '2021-02-11 12:00'
until_2: '2021-02-12 18:00'
message_2: 'româna: Snowfall english: Ninsori'
message_id_2: 2360044196
published_2: '2021-02-11 12:12'
alerts: 3
attribution: Information provided by meteoalarm.eu
friendly_name: meteoalarmeu_bucuresti
device_class: safety
commented

Hi.

You should check the HA Forum where there are examples for similar things... the basic idea is to have a running variable and transverse the attributes by name (not as a list)! I am sorry not give you a full working example but I am short of time.

commented

Another approach is to use the new repeat construct in HA scripts. Take a look here.

commented

As probably you already discovered, a possible solution is this:

automation:
- alias: Alert me about weather warnings
  trigger:
  - platform: state
    entity_id: binary_sensor.meteoalarmeu
    attribute: message_id
  - platform: homeassistant
    event: start
  condition:
  - condition: state
    entity_id: binary_sensor.meteoalarmeu
    state: 'on'
  action:
  - repeat:
      count: "{{ state_attr('binary_sensor.meteoalarmeu', 'alerts') | int }}"
      sequence:
      - service: persistent_notification.create
        data:
          title: >
            {% set ext = "" if repeat.first else "_" + (repeat.index-1)|string %}
            {{ state_attr('binary_sensor.meteoalarmeu', 'awareness_type' + ext) }} ({{ state_attr('binary_sensor.meteoalarmeu', 'awareness_level' + ext) }})
          message: >
            {% set ext = "" if repeat.first else "_" + (repeat.index-1)|string %}
            {{ state_attr('binary_sensor.meteoalarmeu', 'message' + ext) }}

            Effective from **{{ state_attr('binary_sensor.meteoalarmeu', 'from' + ext) }}** until **{{ state_attr('binary_sensor.meteoalarmeu', 'until' + ext) }}**
          notification_id: >
            {% set ext = "" if repeat.first else "_" + (repeat.index-1)|string %}
            meteoalarm-{{ state_attr('binary_sensor.meteoalarmeu', 'alert_id' + ext) }}