Since sensors/actuators sometimes do not respond to commands, I want to ensure that a script is fully and reliably executed on all sensors/actuators by means of a while-loop.
The following is an example for a single thermostat:
- repeat:
while:
- condition: not
conditions:
- condition: state
entity_id: climate.room_climate_badezimmer
attribute: temperature
state: 18
The sequence to be executed with this is the following:
Now in the while loop, I need to replace a static variable value (entitiy_id: climate.room_climate_badezimmer) with a variable one like so: climate.room_climate_[wohnung[repeat.index - 1]]
while:
- condition: not
conditions:
- condition: state
entity_id: climate.room_climate_&"{{ wohnung[repeat.index - 1] }}" # Syntax knowingly wrong
attribute: temperature
state: 18
So my first question is: How do I concatenate variable value strings correctly for this use case?
And my second question is: How do I deal with repeat count in case of nested repeats? Auxiliary variable?
And the 3rd question: Is the complete code below correct and can my low programming experience be streamlined out of it?
The whole code should look something like this:
alias: Lüften 5 Minuten
variables:
wohnung:
- bad
- arbeitszimmer
- wohnzimmer
- schlafzimmer
sequence:
- repeat: # loop 1
count: "{{ wohnung | count }}"
sequence:
- repeat: # nested loop 2
while:
- condition: NOT
conditions:
- condition: state
entity_id: climate.room_climate_[wohnung[repeat.index - 1]] # but the repeat.index must be from the superordinate repeat loop 1 !!!
attribute: temperature
state: 18
- condition: state
entity_id: climate.room_climate_[wohnung[repeat.index - 1]] # but the repeat.index must be from the superordinate repeat loop !!!
attribute: hvac_mode
state: heat
- sequence:
- service: climate.set_temperature
data:
temperature: 18
hvac_mode: heat
target:
area_id: climate.room_climate_[wohnung[repeat.index - 1]] # but the repeat.index must be from the superordinate repeat loop !!!
I know that at least the last part of my complete code is wrong - as mentioned above, I need suggestions as to how to concatenate correctly the variable names and how to deal with nested loops and their respective repeat.counts.
OK this is great - this is even more towards what I want - thank you so much, Taras!
However, I want to stay with the variable array as per my original snippet (wohnung[] = ..., ...) because I intend to use it further down in the overall script - how can I do that? Again, with a repeat loop going through wohnung[], right?
Or could I use something like repeat: for_each: wohnung[]?
Enhancement. Added wait_template to the repeat until loop in order for the service call to take effect before evaluating the until condition. It waits a maximum of 3 seconds; more efficient than a fixed delay.
!!! Perfect - I had a hard time finding info on how this whole {{}} business works (is it like js?). Also, hard time to find good examples about template_value, generally variables and arrays.
The commented part results in only the first item in wohnung[] to be actuated. It seems there’s something wrong in the condition check of the while loop.
The trace is also available for download as a file containing all execution steps (and more) in JSON format.
Even without the trace, what exactly did you mean when you said it “doesn’t work”? I can see from the screenshot that the script stopped execution at some point. Where did the script stop?
FYI: I added an enhancement to my example posted above. The inner loop waits up to 3 seconds for the service call’s changes to take effect before attempting another iteration.
So this is my current script. It doesn’t seem to work if I - for test reasons - shorten the top level delay (roughly in the middle of the script) too much. It appears that maybe the Bosch Controller responds with a sensor state that in fact the sensor itself doesn’t take over. Could that be?
For future reference, you don’t mark your own post with the Solution tag if it duplicates a solution given to you by someone else.
Your posted is based entirely on the proposal presented here which suggested to replace your outer repeat count with a repeat for_each and have it iterate a variable (wohnung) containing a list of names.