String concatenation in automation

Hi, I’m new in YAML and HA programming.
I have automation which notifies if next day is garbage collection day.
5 different types of garbage are being collected, sometimes 2-3 types same day.
Action section of automation:

action:
  - if:
      - condition: template
        value_template: >-
          {{ state_attr('sensor.glass_and_metal','days_to_pickup') | string == '1' }}
    then:
      - service: notify.mobile_app_phone
        data:
          message: "Garbage collection tomorrow: glass and metal."
          title: Home Assistant - garbage collection
  - if:
      - condition: template
        value_template: >-
          {{ state_attr('sensor.garbage_foodwaste','days_to_pickup') | string == '1' }}
    then:
      - service: notify.mobile_app_phone
        data:
          message: "Garbage collection tomorrow: food waste."
          title: Home Assistant - garbage collection
  - if:
      - condition: template
        value_template: >-
          {{ state_attr('sensor.garbage_paper','days_to_pickup') | string == '1' }}
    then:
      - service: notify.mobile_app_phone
        data:
          message: "Garbage collection tomorrow: paper."
          title: Home Assistant - garbage collection
  - if:
      - condition: template
        value_template: >-
          {{ state_attr('sensor.garbage_plastic','days_to_pickup') | string == '1' }}
    then:
      - service: notify.mobile_app_phone
        data:
          message: "Garbage collection tomorrow: plastic."
          title: Home Assistant - garbage collection
  - if:
      - condition: template
        value_template: >-
          {{ state_attr('sensor.garbage_mixed','days_to_pickup') | string == '1' }}
    then:
      - service: notify.mobile_app_phone
        data:
          message: "Garbage collection tomorrow: mixed."
          title: Home Assistant - garbage collection

Currently I’m getting notification for each type of garbage collection separately. My goal is to have just one notification with all types to be collected next day separated by new line.
I know I need to use variable and concatenate string for every if / then condition but how to do that then?

I have an idea for how to achieve your goal but it relies on using each garbage sensor’s friendly_name.

What are the friendly_names of the 5 sensors? Are they like “glass and metal”, “food waste”, “paper”, “plastic”, and “mixed”? Or are they significantly different?


EDIT

This is what I had in mind:

action:
  - variables:
      sensors:
        - sensor.glass_and_metal
        - sensor.garbage_foodwaste
        - sensor.garbage_paper
        - sensor.garbage_plastic
        - sensor.garbage_mixed
      waste: >
        {{ expand(sensors)
          | selectattr('attributes.days_to_pickup', 'eq', 1)
          | map(attribute='name') | list }}
  - condition: template 
    value_template: "{{ waste | count > 0 }}"
  - service: notify.mobile_app_phone
    data:
      message: "Garbage collection tomorrow: {{ waste | join(', ') }}."
      title: Home Assistant - garbage collection

My sensors have friendly names.

After small modifications (cosmetic) your code does exactly what I need to achieve, thanks!

1 Like