Problem for concatenate in automation

Hello, I created this automation to monitor the humidity of a few plants. I’m trying to accumulate the message for each plant in a variable. I can’t get my result. Before, I sent four messages, one per plant. My goal is to send only one. Thank you for your advice.

alias: "Surveillance: Humidité des plantes #5"
description: Surveille l'humidité des plantes et envoie des notifications.
triggers:
  - hours: "6"
    minutes: "30"
    seconds: 0
    trigger: time_pattern
    enabled: true
  - trigger: time
    at: "11:15:20"
conditions: []
actions:
  - variables:
      plantes:
        - nom: Plante du salon (1er étage, salon)
          capteur: sensor.plante_1_soil_moisture
          min: 65
          max: 75
        - nom: Cactus de Noël (2e étage, bureau Diane)
          capteur: sensor.plante_2_soil_moisture
          min: 40
          max: 50
        - nom: Cactus de Noël (2e étage, walkin Diane)
          capteur: sensor.plante_3_soil_moisture
          min: 40
          max: 50
        - nom: Alloes (2e étage, walkin Diane)
          capteur: sensor.plante_4_soil_moisture
          min: 30
          max: 50
  - variables:
      message_part: ""
      message_accumule: ""
    enabled: true
  - repeat:
      for_each: "{{ plantes }}"
      sequence:
        - variables:
            humidite: "{{ states(repeat.item.capteur) | float(default=0) | int }}"
            message_part: >
              Humidité de {{ repeat.item.nom }} : 🌱 Normal de {{
              repeat.item.min }}% à {{ repeat.item.max }}%. Actuellement {{
              humidite }}%. {% if humidite < repeat.item.min %} 🔴 Ajouter de
              l'eau. {% elif humidite > repeat.item.max %} 🟡 Trop humide,
              surveiller. {% else %} 🟢 Humidité suffisante.   {% endif %}
            message_accumule: |-
              {{ message_accumule + message_part + '
              ' }}
        - delay:
            hours: 0
            minutes: 0
            seconds: 2
            milliseconds: 0
        - delay:
            hours: 0
            minutes: 0
            seconds: 2
            milliseconds: 0
        - action: logbook.log
          metadata: {}
          data:
            name: PLANTES
            message: "{{ message_accumule + message_part }}"
mode: single

Review the documentation about scope of variables. Because of the local scoping, your approach won’t work; you can’t update the outer-scope message_accumule variable from within the repeat loop.

Probably the easiest way to accomplish what you want is by using an input_text helper.

If you’re comfortable with Jinja, you could also use a Jinja for loop in the initial definition of message_accumule, but only by using a Jinja namespace—the code would be more esoteric.

You need to compute the list of messages within a single script variable using Jinja2.

alias: "Surveillance: Humidité des plantes #5"
description: Surveille l'humidité des plantes et envoie des notifications.
triggers:
  - hours: "6"
    minutes: "30"
    seconds: 0
    trigger: time_pattern
    enabled: true
  - trigger: time
    at: "11:15:20"
conditions: []
actions:
  - variables:
      plantes:
        - nom: Plante du salon (1er étage, salon)
          capteur: sensor.plante_1_soil_moisture
          min: 65
          max: 75
        - nom: Cactus de Noël (2e étage, bureau Diane)
          capteur: sensor.plante_2_soil_moisture
          min: 40
          max: 50
        - nom: Cactus de Noël (2e étage, walkin Diane)
          capteur: sensor.plante_3_soil_moisture
          min: 40
          max: 50
        - nom: Alloes (2e étage, walkin Diane)
          capteur: sensor.plante_4_soil_moisture
          min: 30
          max: 50
      message_accumule: >
        {% set ns = namespace(msg=[]) %}
        {% for x in plantes %}
          {% set humidite = states(x.capteur) | int(0) %}
          {% set guide = "🔴 Ajouter de l'eau." if humidite < x.min else "🟡 Trop humide, surveiller." if humidite > x.max else "🟢 Humidité suffisante." %}
          {% set msg = 'Humidité de {} : 🌱 Normal de {}% à {}%. Actuellement {}%. {}'.format(x.nom, x.min, x.max, humidite, guide) %}
          {% set ns.msg = ns.msg + [msg] %}
        {% endfor %}
        {{ ns.msg }}
  - action: logbook.log
    data:
      name: PLANTES
      message: "{{ message_accumule | join('\n') }}"
mode: single

As mentioned by d921, scoping rules prevent you from redefining a script variable that is defined outside its scope block.

In your example, message_accumule is initially defined outside of the repeat therefore if you define it again within the repeat it’s treated as a completely separate variable (even though it has the same name). In other words, message_accumule inside the repeat is not the same variable as message_accumule outside of the repeat.

Thanks, everything works except that when I try to send a notify to the HA apps, the message is truncated. To your knowledge, is there a maximum length (surely??)

I suspect the truncation happened by your phone’s notification platform rather than by HA, because, as the logbook entry you pasted shows, the code computed the full message. The message only appears truncated when displayed as a notification on your phone.

When you tap the message shown in your screenshot, what happens?

He opens the HA apps

I will do a test with GMAIL.

As a result, it’s the HA app platform that truncates the message. With GMAIL, the message is complete.

It sounds like it’s working correctly.

The truncation of the message is probably due to what d921 explained. For example, if I send this to my phone I see all of it (so HA is not truncating it):

Humidité de Plante du salon (1er étage, salon) : 🌱 Normal de 65% à 75%. Actuellement 72%. 🟢 Humidité suffisante.
Humidité de Cactus de Noël (2e étage, bureau Diane) : 🌱 Normal de 40% à 50%. Actuellement 35%. 🔴 Ajouter de l'eau.
Humidité de Cactus de Noël (2e étage, walkin Diane) : 🌱 Normal de 40% à 50%. Actuellement 55%. 🟡 Trop humide, surveiller.
Humidité de Alloes (2e étage, walkin Diane) : 🌱 Normal de 30% à 50%. Actuellement 40%. 🟢 Humidité suffisante.

The test was performed with an old Samsung phone running Android 11.

In my case it’s a Samsung S24 Ultra with Android 14.

Interesting

Looks like you’ll need to shorten the message to avoid truncation on your phone.

alias: "Surveillance: Humidité des plantes #5"
description: Surveille l'humidité des plantes et envoie des notifications.
triggers:
  - hours: "6"
    minutes: "30"
    seconds: 0
    trigger: time_pattern
    enabled: true
  - trigger: time
    at: "11:15:20"
conditions: []
actions:
  - variables:
      plantes:
        - nom: Plante du salon (1er étage, salon)
          capteur: sensor.plante_1_soil_moisture
          min: 65
          max: 75
        - nom: Cactus de Noël (2e étage, bureau Diane)
          capteur: sensor.plante_2_soil_moisture
          min: 40
          max: 50
        - nom: Cactus de Noël (2e étage, walkin Diane)
          capteur: sensor.plante_3_soil_moisture
          min: 40
          max: 50
        - nom: Alloes (2e étage, walkin Diane)
          capteur: sensor.plante_4_soil_moisture
          min: 30
          max: 50
      message_accumule: >
        {% set ns = namespace(msg=[]) %}
        {% for x in plantes %}
          {% set humidite = x.h %}
          {% set guide = "🔴 Ajouter de l'eau." if humidite < x.min else "🟡 Trop humide, surveiller." if humidite > x.max else "🟢 Humidité suffisante." %}
          {% set msg = '{}: {}% ({}% à {}%). {}'.format(x.nom, humidite, x.min, x.max, guide) %}
          {% set ns.msg = ns.msg + [msg] %}
        {% endfor %}
        {{ ns.msg }}
  - action: logbook.log
    data:
      name: PLANTES
      message: "{{ message_accumule | join('\n') }}"
mode: single

The output of this version looks like this:

Plante du salon (1er étage, salon): 72% (65% à 75%). 🟢 Humidité suffisante.
Cactus de Noël (2e étage, bureau Diane): 35% (40% à 50%). 🔴 Ajouter de l'eau.
Cactus de Noël (2e étage, walkin Diane): 55% (40% à 50%). 🟡 Trop humide, surveiller.
Alloes (2e étage, walkin Diane): 40% (30% à 50%). 🟢 Humidité suffisante.

I sent the same message to the apps on my Samsung X910 tablet, Android 14. Surprise, the message is not truncated.

Interesting, thank you

1 Like

Since the message is shorter, it appears in full on the Samsung S24 Ultra with Android 14.

Glad to hear the concatenation is working and the shortened version is displayed in full on your phone.

Please consider marking my post above with the Solution tag.

It will automatically place a check-mark next to the topic’s title which indicates to others that the topic has been solved. This helps users find answers to similar questions.