Good morning
I would like to create several automations in 1 to avoid having too many for the same thing
-I would like when my switch is turned on it turns on the light
-I would like that when my switch turns off it turns off the light
-I would like that when my light is turned on it turns on the switch
-I would like that when my light turns off it turns off the switch
I managed to do it with 4 automations which takes up space
Sincerely
Hi @Stevoidex
This question comes up fairly often so, you may want to search for other answers. However, in general, you need to:
- Create all of the triggers (switch on or off, light on or off) in a new automation
- For each trigger, use the 3-dot menu to add a Trigger id - a basic description like âswitch onâ or âswitch offâ
- In the actions section create a Choose block
- Add Options to the Choose block - 1 for each trigger. Set the condition for each option to âTriggered byâ and select one of your Trigger ids.
- Then add any actions you want to the Actions section of the Option.
I can appreciate the desire for elegance, but there really is no great advantage in combining automations (particularly now that they can be labelled and annotated), and it can make maintenance much more difficult. The space they take up is trivial.
If your automations have common code, that can be put in a single script to be called by all of them.
This is the answer.
Just search YouTube for videos on trigger IDs and the choose action. Smart home junkie has a good one
triggers:
- trigger: state
entity_id: light.your_light_here
not_to:
- unknown
- unavailable
id: switch.your_switch_here
- trigger: state
entity_id: switch.your_switch_here
not_to:
- unknown
- unavailable
id: light.your_light_here
actions:
- action: "homeassistant.turn_{{trigger.to_state.state}}"
target:
entity_id: "{{ trigger.id }}"
- delay: 1
max_exceeded: silent
EDIT: corrections made with the assistance of Taras.
Or search this forum firstâŚ
This. Iâm a big fan of simple automations. They are easier to write, easier to maintain, easier to trace and hence more likely to work properly. The only reason to combine them is if you need sequencing across multiple functions using automation queueing or restarting.
Pete,
I am just the opposite. I like my automations containerized. My mind likes to go one place to fix a function.
For instance instead of having four automations for my bedroom lights (e.g., Bedroom - Spouse Light on, Bedroom - Spouse Light off, Bedroom - My Light on, Bedroom - My Light off), I have a single automation that is named Bedroom - Lights. Granted, when I first started HA (I am still quite the newbie), I did have four separate automations. I deliberately combined them into one when I learned about the CHOOSE option. Of course as I learn more about HA and grow, I may end of one day going back and splitting my automations back out again. That is what makes HA so great. You can âCHOOSEâ (see what I did there?) whatever style of organization and development that fits your personality. Heck, one day I may even learn YAML. LOL!
Aaaaaargh!
Did you see the picture I posted, and lol why the face
Compare it to the elegance of Tomâs automation here and youâll see why people are pulling faces.
Not only did it ignore the simplicity of a triggerId, your ChatGPT automation will try to call light.turn_unavailable
since it doesnât know those can be possible âbadâ states.
It also suggested the use of mode: restart
which will cause an endless loop of lights turning on the switch, and vice versa. The only good thing about it is that itâs a nice way of killing off your entire HA instance at the flip of a light switch.
@shadowfist sorry, idk i tell everyone ai can do it for you because it does it for me well. but i dont feed it a simple request i feed it some code. a bit more information than a desired outcome. I fix the bugs if any and i keep moving forward
Edit: its ideas work better with nodered, youll run into less errors if you ask it for node red automations. Ai does have a limitation, it tends to hallucinate because it runs out of context
Your suggestion would have caused the OP to crash their instance if they followed your advice.
Thereâs a very specific reason why itâs against the rules to post untested AI code, and you just proved it.
If I were in your shoes, Iâd edit that post and leave a comment stating no one should use that automation. At the very least itâll work in your favour if mods are trying to decide whether to ban you or not.
youâre the boss, thank you itâs the best and no more bugs
1,000% my philosophy, espcially for the maintenance part. Once you have something finally set up the way you want with one monolithic automation, you have locked yourself in such that you may discover you want something to work in a different way, but you are locked into this one huge thing that might be a nightmare to restructured/rewrite.
My philosophy is to have each automation (with some exceptions*) do one thing and have them properly named so it is very simple to change. Also I have run into situations where different automations all need to have the same piece of logic in many situations - in that case I created a generic script that does the logic that is repeated - and call it from automations with objects passed as parameters. I can supply an example if you like.
*Examples of my only monolithic ones -
- On HA startup - helpers/timers/switches etc. - adjust them to what makes sense for the current state.
- When the last person has left the home (if not in guest mode) - turn off all lights, change hvac to âoffâ etc.
- When the weather sensor detects actual rain falling - one big automation to check all windows and send me a text message âplease close window âXâ as it just started rainingâ etc.