this evening I sat around 4 hours on creating the following code.
I wanted to build a presence simulation that plays several scenes once after another.
When the input_boolean for presence simulation (input_boolean.anwesenheitssimulation) is “activated”, the automation shall start (-> anwesenheitssimulation is the german word for presence simulation)
When input_boolean.anwesenheitssimulation is “deactivated”, a final scene shall be activated ending the process
This automation is started when the bool is activated:
It felt a bit tricky to build the automation in a way, that it is “intercepted” when the value of the input_boolean changes, so hopefully this doesn’t look too bad.
This is my first Home Assistant YAML, so I would really like to hear your thoughts if this code is idiomatic from your point of view.
Hi, I think this is a leftover from you wanting to put it in one automation before? because only the first trigger does something, so if you remove the second trigger, you also no longer need the choose.
I have something similar, but with some differences.
I have a vacation setting, which means the simulation runs. I do not use the simulation boolean as the trigger, but sunset/sunrise or time. I put the vacation in the conditions, it will not work if vacation is off.
then, to make it less predictable, I use some random delay:
No, it isn’t. It is there intentionally, after running into the following issue.
Explaination:
The automation shall (obviously) start when input_boolean.anwesenheitssimulation is set to true.
The automation needs (obviously) to stop when input_boolean.anwesenheitssimulation is set to false
Issue I ran into without the statement: When input_boolean.anwesenheitssimulation was set to true and some time later to false, the automation AnwesenheitssimulationAN (AN == ON) ran through until the end of the while loop
So, I set mode: restart to restart the whole automation when the bool in question is set to false.
Because there is no condition set, nothing than a restart of the automation happens, as intended.
This was one of the questions I asked myself: Does this make sense or is there any other mechanic in HA to stop an automation working in an idiomatic way.
Ah, now I get it. This is rather convoluted, and in a year you’ll probably don’t understand what you did there. The solutions is rather simple. Put the loop in a separate script. use script.turn_on to start it and script.turn_off to stop it. You can use one automation to turn it on and off:
I would trigger the automation to restore the lights to normal based on the state of the script turning off. The reason being that the script is aborted during a delay, so you’d want to wait until it has actually stopped.
Last but not least, have you considered using scene.create to capture the state of the lights before absence, and restoring that when you stop? It kind of depends how long you intend the boolean to be on, if you start it dat daytime and stop at night, it might not be what you want.