Closed mcshosho closed 3 years ago
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.
Another approach is to use the new repeat
construct in HA scripts
. Take a look here.
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) }}
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.