Automation, condition template, notify (no trigger.to_state.name)

Hi everybody,

I am trying to send a notification the evening before different garbage bins will be picked up at our house. Can you please help me achieve this?

What I did so far

  • install ics_calender via HACS
  • add my garbage collection .ics calendar to ics_calendar
  • create the calendar entities by
calendar:
- platform: ics
  calendars:
      - name: "AWSH"
        url: "https://www.awsh.de/api_v2/collection_dates/1/ort/83/strasse/590/hausnummern/0/abfallarten/R02-B02-D02-P04/kalender.ics"
        includeAllDay: true

That will create these entities:

Those are sensor.abholtermine_event_<n>, where n is 0 - 4; so I got five upcoming events. Unfortunately (for me), these events all start at 00:00:00, which means that the eta state attribute will also always change at midnight. So triggering it then will do me no good!

Problem

I thought I could use platform: time in my automation (below) to “fix” this; however, while I can do this in theory, it will not allow me to use trigger.to_state.name (or anything related to trigger.), so I cannot notify us which bin is being collected.

Those are different garbage bins in one file, so in this case, sensor.abholtermine_event_0 is for regular trash, sensor.abholtermine_event_2 is for compost / garden trash, and so on. However, once on of those events has passed, sensor.abholtermine_event_2 might be for something other than compost, so I cannot use the _<n> as an indicator which event is for which kind of garbage collection.

Automation

automation:
  - alias: "[Notify] AWSH Abholtermine"
    trigger:
      - platform: time
        at: "17:00:00"
    condition:
      condition: template
      value_template: >
        {% set awsh = ['sensor.abholtermine_event_0', 'sensor.abholtermine_event_1', 'sensor.abholtermine_event_2', 'sensor.abholtermine_event_3', 'sensor.abholtermine_event_4'] %}
        {% for each in awsh %}
          {{ state_attr(each, 'eta') < 3 }}
        {% endfor %}
    action:
     - service: notify.me
       data_template:
         message: >
           Trash Pickup (this is where I'd need to use trigger.to_state.name or similar)

Potential solutions (all of which I don’t 100% agree with)

Idea 1

  • create template_sensors for each of those events
  • when either of their eta values change to < 3, trigger the automation
    => that would allow me to use trigger.to_state.name
  • action would first start a delay for x hours (for example, 17:00:00 if I want the notification at 5pm the next day), then trigger the notification

Downside: if I have to restart Home Assistant in the meantime, I will not be notified as the delay from the automation would not “survive” this restart.

idea 2

pretty much the same as above, but use a timer instead of the delay; I am not sure whether or not a timer will “survive” the restart. Also, I don’t know if it is even possible to pass on the trigger.to_state.name to the timer, or rather to that automation that will notify me once the timer runs out.

idea 3

Just after typing all this, I realized that I could manually download the .ics file, then splitting it into one file per type of garbage pickup; then I could push those separate files to my local webserver and have Home Assistant subscribe to one file per type of pickup. While this would allow me to always know which entity would trigger which type (so that I could include the type in the Telegram notification), it seems a hassle!

I am currently doing something similar already, where I download the file, change the notification time, then push it to my Nextcloud calendar and notify us via the calendar app. It is automated (curl the calendar file, awk the reminder time, scp it to the local webserver), but it is a bit hacky and if I have to resort to something equally hacky to use Home Assistant instead of the calendar app, I might as well just stick with the current solution.

Final question

Is there something essential that I have missed and that would allow me to do what I need to without so much hassle on the side?

Thank you for your ideas :slight_smile:

I just did the thing where I use one calendar per event. This is my automation now

automation:
  - alias: "[Notify] AWSH Restabfall"
    trigger:
      - platform: time
        at: "17:00:00"
    condition:
      condition: template
      value_template: >
        {{ state_attr('sensor.restabfall_event_0', 'eta' < 2) }}
    action:
     - service: notify.me
       data_template:
         message: "Restabfall wird morgen abgeholt."

I still need to add callbacks for reminding later and dismissing the reminder, but this works now.

Still wondering if there is a way to use all those sensors in one automation, or if I should copy/paste/edit this one? Since the trigger is still the time, I cannot use trigger.to_state.name to use a data_template for the telegram message.

I guess I could do something like

(...)
  action: notify.all_of_us
  data_template:
    message: >
      {% if state_attr('sensor.restabfall_event_0', 'eta' < 2) %}
       Restabfall wird morgen abgeholt.
      {% elseif state_attr('sensor.bioabfall_event_0', 'eta' < 2) %}
       Bioabfall wird morgen abgeholt.
      {% elseif state_attr('sensor.papiertonne_event_0', 'eta' <2) %}
       Papiertonne wird morgen abgeholt.
      # (etc.)
      {% endif %}

Then I wouldn’t need the condition part at all, right? Or would it just send an empty message at “17:00:00” of there was no pickip with eta < 2?

I mean, both copying/pasting the automation and changing the sensor and message values, and using the data_template should work… at least it seems like they do while I am doing these tests…

But I have asked about templates here before, and usually people have wayyyy more interesting solutions to these things than I come up with, and I’d like to always go with the “best practice” solution :slight_smile:

This should work.

automation:
  - alias: "[Notify] AWSH Abholtermine"
    trigger:
      - platform: time
        at: "17:00:00"
    condition:
      condition: template
      value_template: >
        {% set awsh = [states.sensor.abholtermine_event_0, states.sensor.abholtermine_event_1, states.sensor.abholtermine_event_2, states.sensor.abholtermine_event_3, states.sensor.abholtermine_event_4] %}
        {{ awsh | selectattr('attributes.eta','<', 3) | list | length >= 1 }}
    action:
     - service: notify.me
       data_template:
         message: >
           {% set awsh = [states.sensor.abholtermine_event_0, states.sensor.abholtermine_event_1, states.sensor.abholtermine_event_2, states.sensor.abholtermine_event_3, states.sensor.abholtermine_event_4] %}
           {% set items = awsh | selectattr('attributes.eta','<', 3) | map(attribute='name') | list | join(', ') %}
           Trash Pickup {{ items }}
1 Like

Thank you @petro

I just manually triggered this, and received Trash Pickup Abholtermineevent0, Abholtermineevent1; however, I should get Trash Pickup Restabfall 40L-240L(2-wöchentlich), Wertstoff/LVP(2-wöchentlich) (these two events both have an eta of 1).

Is this because I manually triggered this, or what could be the reason? Here is the content of sensor.abholtermine_event_0

name: Restabfall 40L-240L(2-wöchentlich)
location: Lauenburg, Alle StraĂźen
start: 2019-12-28T00:00:00+00:00
end: 2019-12-28T00:00:00+00:00
eta: 1
friendly_name: Abholtermine_event_0
icon: mdi:calendar

manual trigger shouldn’t make a difference. What’s the state of all 5 etas?

I set the trigger to 16 instead of 17, so I just got the same message a couple of minutes ago with the same result. These are the states

  • 1
  • 1
  • 8
  • 14
  • 14

Ok, so it’s just not outputting the friendly name.

Change to this:

automation:
  - alias: "[Notify] AWSH Abholtermine"
    trigger:
      - platform: time
        at: "17:00:00"
    condition:
      condition: template
      value_template: >
        {% set awsh = [states.sensor.abholtermine_event_0, states.sensor.abholtermine_event_1, states.sensor.abholtermine_event_2, states.sensor.abholtermine_event_3, states.sensor.abholtermine_event_4] %}
        {{ awsh | selectattr('attributes.eta','<', 3) | list | length >= 1 }}
    action:
     - service: notify.me
       data_template:
         message: >
           {% set awsh = [states.sensor.abholtermine_event_0, states.sensor.abholtermine_event_1, states.sensor.abholtermine_event_2, states.sensor.abholtermine_event_3, states.sensor.abholtermine_event_4] %}
           {% set items = awsh | selectattr('attributes.eta','<', 3) | map(attribute='attributes.name') | list | join(', ') %}
           Trash Pickup {{ items }}
1 Like

Awesome, thank you so much. This works perfectly.

1 Like