I’m completely new to Home Assistant and I’m looking for the most elegant way to solve this.
I have a “TV Mode” automation, it is triggered by an input_boolean.tv_mode. It dims some lights in the living room and hallway and closes one of my covers. The input boolean for TV Mode should be switched off as soon as any of the living room or hallway lights change brightness or are turned off.
I wrote a “TV Mode Stop” automation that turns off the input boolean if those lights change brightness or are turned off. Now obviously whenever I turn on the TV Mode, the lights are dimmed which is then triggering the TV Mode Stop right away as well.
I know I can get around that various ways but all I can think of seems overly complicated. Is there an elegant way to not run an automation if the state change that is triggering it is caused by another automation?
I would take the actions from your tv mode and put them into a script instead. To activate “TV mode” I would use a button on a dashboard that calls the script. The last action in that script would be to turn on the “TV mode” input_boolean.
Essentially your “TV mode” boolean becomes a status indicator only, not a dual purpose status and command.
If you want to be able to turn it off you could set up your script so that it performs different actions depending on if the Boolean is on or off. Then the button would toggle the state.
If you don’t want buttons, you could just create another input Boolean. One would be “tv mode status” and the other would be “tv mode command”
I’m already using a button and that’s the issue. The TV Mode Stop doesn’t perform any action apart from turning off the boolean.
Here’s a typical scenario:
I want to watch a movie so I tap the “TV Mode” button on my dashboard, the lights dim down. After I’ve finished watching I don’t want to remember to manually turn it off every single time. Instead I wanna be able to get up from my couch, turn the lights to 80% and the input boolean should be automatically turned off because I’m obviously done watching. So when I come back to my couch 8h later the state is consistent and I do not have to then double tap to first turn off (because I forgot before) and turn on again to trigger the TV Mode script.
What I suggested would do that. Perhaps I didn’t explain it well enough. My suggestion of moving the actions to the script also eliminates the trigger that you had, which was triggering tv mode when the toggle was turned on. A little more detail:
Have a script called “tv_mode_script” that has all the actions that dim the lights and set the blinds etc.
Have a button on your dash called “start TV mode” that runs the script.
You can stop at this point if you don’t need a status indicator of whether “tv mode” is currently on or off. For example I have a bedtime script that turns off lights and locks doors, but HA doesn’t know or care if anyone changed anything after I run the script. So ask yourself “does this really need to be stateful?”
If you need it to be stateful, create an input_boolean called “tv mode status”
As the very last action in the script from item #3, turn the “tv mode status” input_boolean to “on”. You may also need a 1 or 2 second delay immediately prior to this action.
Create an automation that is triggered by the state changing of any entity that is controlled in your script. The action would be to run the inout_boolean “off”. Make sure this automation mode is set to “restart”.
Now, when you use the button on your dash, the script will be launched and lights will start dimming. This will trigger your automation to turn off the tv mode toggle, but it’s already off anyway. Once the script completes, it will then toggle it “on”. Later on, when you turn up the brightness on your lights, the automation will run and toggle the tv mode switch to “off”.
Note that you should not toggle the switch manually. It is a status indicator, not an actual switch.
If you want the toggle to be read-only, you could create a trigger-based binary template sensor in YAML, and use custom events to change its state.
——-
Option #2
Keep what you already have, and use the context as a condition to halt the automation if the entities were changed by an automation. Detail is here; you’d want to check that {{ trigger.to_state.context.user_id == none and trigger.to_state.context.parent_id != none }}
However if you are in tv mode and you trigger some other automation to change the lights (for example you add a bedtime script to turn off all your lights), the tv mode won’t turn off.
—-
There are still many more options you could pursue.
Got it, thanks for your explanation.
You are using a button and a status indicator - I was using just a button and wanted it to display the status as well. I’ve ended up with this solution partly based on your suggestions but having both status indicator and toggle button in one button. If you have the time please let me know if there’s anything to improve on - I’m still learning.