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.
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.
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.
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.