My script gradually changes the light over an amount of time. I want to stop the loop when either the repeat.index has reached a value, or when the context_id has changed.
I tried using a helper boolean, but that didn’t work as the loop triggered the boolean every time. So what I did is I created another helper text_input that stores the last used context_id when a light state is changed. I’m using that in this script to check if, while this loop is running, a change is made that was not the automation itself. If the change has an unique context_id, then the script should stop running and stay on the light settings that were manually set.
How do I do this??
EDIT: I tested using an input_boolean and it doesn’t toggle when the script is running.
Your condition logic doesn’t make sense. Change the or to and and change the and to or. Then invert the last pair of conditions (remove not from the first, and make the second ==
I haven’t looked over any code in detail but this immediately jumped out to me. I don’t think this condition does what you think it does, unless it should only be true for the very first iteration. repeat.index does not start from zero but from one, so this is line is effectively the same condition as repeat.index == 1 or repeat.first. If you want it to run at least twice I’d go with repeat.index <= 2.
Let me try to explain it in human words, as I’m confusing myself.
I want the loop keep changing the lights gradually, but stop repeating when either the repeat.index is less than or equal to the intervals, OR when the context_id is not the same as the one from last change.
But this last one only checks after the second loop, because starting the automation would probably generate a new context_id.
But what exactly is this comparison meant to achieve? The comparison in your code does not make much sense to me.
For a given run of this automation, trigger.to_state.context.id will never change. It only changes the next time the automation is triggered.
What value is stored inside input_text.last_context_id? The code you have posted never stores a value there. Unless you have a different automation running simultaneously which may update that value, its value will also never change.
Thus trigger.to_state.context.id!=states('input_text.last_context_id') will more than likely either always be true, or always be false.
I have another automation that stores the context_id, but sometimes changing the lights generates multiple instances with the same context_id (because I change a light group consisting of multiple lights)
I’m trying to get all parts of my automations to work before I put them all together.
This is to make sure the automation stops when the context_id changes, because:
My manual override input_boolean helper doesn’t toggle when the script is running, so the script has no way of knowing the lights were manually overwritten
I found out that when a time trigger in an automation triggers the light change, no parent_id is generated.
→ Therefore, I cannot reliably say “if state change generates no parent_id, it’s done by a person”.
I cleaned some things up, let me know if this works for you. It calculates your data ahead of time so you don’t need to rely on conditions during a repeat.
EDIT: If it doesn’t end at the desired targets, we just need to alter {% for i in range(intervals) %} to be + or - 1 depending if we over shoot or under shoot the target.