How to store a state in a variable


I want to build an automation to switch on and off a light on motion but only if it is not on. (my wife hates light going down while she is in the room) That’s why I need to variable to keep that state.


  1. I build an automation that only switches on motion if the state of the light is off and writes the information “true” in a variable so it knows that it was switched on automatically.
  2. A second automation switches off at end of motion, but only if this variable is “true”, not false. If the light was switched on manually, it does nothing because the variable is “false”.

It seems to be an easy one, but I do not get the simple part writing the variable in the action of #1 and reading it in the trigger of#2 done. How does the syntax look like. Can I do this with a helper?

Yes, with an Input Boolean using input_boolean.turn_on and input_boolean.turn_off service calls.


You may wish to browse the Blueprints Exchange section because there may already be a blueprint for this application.

Read it in the condition of #2.

And as always with HA, there are many ways to achieve what you want. :slight_smile:

My suggestion would be to use trigger variables. In automations you can get as an id, what triggered the automation (manual switch vs. motion sensor). Using this in the action block to decide, if the light was turned on manually, should give you even more possibilities. Like switching off the light after some time, if it was turned on manually, but still is on after 3 hours. :slight_smile:

Great point. A safety switch-off after a few hours is a must do, even if there is an alarm on the kNX side coming up after a while. I’ll try it this way now.

Thanks a lot. I was looking for a document like that one. It is looking quite doable, and I will give it a try now.

Thank you all for your rapid help.
I started with the beginner solution setting up a boolean helper variable and I’m surprised that I could do it all the way down in the GUI. Wow, I did not expect that.
It’s running now with two automations, one switching the light on by motion and setting the variable to true, one to switch off if the light if true and set it back to false after that. I#ll me much more advanced in a few weeks from now and try to implement Patrick’s idea then. Thanks again. Great help all of you!