Loop through sensors to build message

Hi all,

I have 4 sensors that I would like to run through every day for garbage collection.
They all have an attribute that states number of days until next collection.

How would I be able to loop through them, check which ones are due the next day and than create a message for a notification that is structured like natural language?
I would like the message to be along the lines of “Garbage collection tomorrow: Recycling, cardboard and greenery”.


Hi, I don’t understand much about your setup but you could use an automation that gets triggered at a certain time every day or (depending on what sensors you have) when motion is first detected in the morning. This could contain multiple choose conditions to check the attributes you refer to and trigger a suitable notifcation for each as required.

Not sure if you are familiar to templating, but this can easily be achieved with few lines of code.

You might b able to build something using the bits and pieces showcased in this snippet.

{{ (as_timestamp(now()) - as_timestamp([
          states.person.d] |
            sort(reverse=true, attribute='last_changed') |
            selectattr('state','eq','home') |
            map(attribute='last_changed') |
            list | first)) | int / 60 < 5}}

This one checks which person was coming home last and seems to have all the pieces you would need.

And … there is a plugin for garbage collection which I use and itsit is rather complete. Have a look if you can accomplish what you have planned with the plugin already.

bruxy70/Garbage-Collection: :wastebasket: Custom Home Assistant sensor for scheduling garbage collection (or other regularly re-occurring events - weekly on given days, semi-weekly or monthly) (github.com)

The probem is not starting the automation. The issue is creating a message in natural language.

If it loops through the 4 sensors, it should identify which ones are relevant and then create the message with the “,” and final “and” correctly.

For instance if there are 4 sensors: rubbish, recycling, paper and garden waste.
And tomorrow it’s only rubbish, paper and garden waste, than it should loop through the 4 sensors to check but in the end produce a message like “Tomorrow collection of rubbish, paper and garden waste”, rather than “Tomorrow collection of rubbish, , paper, garden waste” (as an example).

{% set persons = expand('group.arriving') | map(attribute='name') | join(', ') | regex_replace(',([^,]*)$',' und\\1') %}

This should help.