Any way to do multiple "for" triggers?

I have a number of automations I want to have re-trigger a second time. Is there any way to have multiple “for” trigger lines?

For example (psudocode) I have this setup, I would like to not just trigger at 12 hours but maybe like a couple times over at 12, 24, 72 hours in case I was busy and missed it…but not over and over on an interval because if I’m not home I don’t want excessive notifications?

This particular automation is nearly 100 entities when you get done with the several different ways they could trigger (some state=on, some state=off, a couple different numeric thresholds) so “just duplicate it with multiple for lines” is a rather poor solution, and I really want to use the entity name as the thing I get sent so I don’t have to code 100 separate messages.

automation:
  - alias: 'Telegram Check Sensor Alert'
    trigger:
      # Low Battery Alerts
      # Binary Sensor Alerts (on = alarm)
      - entity_id:
          - binary_sensor.outside_front_sensor_battery_low
          - binary_sensor.outside_rear_sensor_battery_low
          - binary_sensor.attic_sensor_battery_low
          # and many, many more.....
        platform: state
        from: 'off'
        to: 'on'
        # Reduce flapping
        for:
          hours: 12
      # Low Battery Alerts
      - platform: numeric_state
        entity_id:
          - sensor.front_door_battery_level
          - sensor.back_door_battery_level
          - sensor.garage_side_door_battery_level
          # and many, many more.....
        below: 33
        # Reduce flapping
        for:
          hours: 12
    action:
      service: notify.telegram_me
      data_template:
        message: |
          Warning - Check Sensor:
          {{ trigger.to_state.attributes.friendly_name }}

Each trigger can only be for one reason. Just repeat the trigger for each for: you want.

Yeah, I was just really hoping to avoid that. Its not bad if its a few lines but when you have 4-6 different triggers with a dozen or more different entities in them and now have to keep track of ALL those (and if you replace/change one, updating all of them) gets to be kinda overwhelming.

Seemed like there should be a better way…

Hi Matt Miller,

So your problem appears that you do not want to list all the entities multiple times?

If that is the case, I would test if you could put the list of entities into a list under
trigger_variables:
once, then refer to the list variable under
- entity_id:
I am not certain that it would work but I think it would, and is worth trying.

2 Likes

Oh interesting idea! I have not seen that before but it does sound like it could do exactly what I want.

Thanks!

1 Like

This also might be of interest to you: Low battery notification blueprint. With that blueprint there is no need to list each battery entity.

Interesting…I don’t quite understand how that selects what things it will notify on and how to pick which type or threshold?

I have (due to different vendor implementations) some things that fail quickly around 30%, others still work for days after hitting 0%, and some that just have a binary sensor. The binary sensor ones, some have “Battery OK” binary sensor, others have “Battery LOW” binary sensor so I have to account for if on or off is “low”.

I only took a handful of the things in my automation to keep the post short but I also have triggers for other stuff like sensor age (stopped updating), propane gauge capacity (in both pounds and estimated days remaining), water pressure loss, etc. Basically one automation that sends me “you need to check ”

It looks like handles level and binary battery entities. For the entities that report a level you can set the threshold to alert at, but it doesn’t appear to handle per-device thresholds.

{% for state in states.sensor | selectattr('attributes.device_class', '==', 'battery') %}
      {% if 0 <= state.state | int(-1) < threshold | int and not state.entity_id in exclude.entity_id %}
        {% set result.sensors = result.sensors + [state.name ~ ' (' ~ state.state ~ ' %)'] %}
      {% endif %}
    {% endfor %}
    {% for state in states.binary_sensor | selectattr('attributes.device_class', '==', 'battery') | selectattr('state', '==', 'on') %}
      {% if not state.entity_id in exclude.entity_id %}
        {% set result.sensors = result.sensors + [state.name] %}
      {% endif %}
    {% endfor %}

As you need reporting for other devices in the same automation it wont be suitable anyway