I have a scenario that I’m bouncing out to the family that I just want some opinions. I have a complex automation scenario that I need which I accomplished by creating six (yes, 6) different automations. Everything works great, but there has to be a better way, right?
Here’s my scenario described:
* switch ON at 7:00pm (independent of thermostat state below)
* from 7pm to 6am: mirror thermostat state:
if COOLING, then switch ON
else, switch off
* If door sensor = open for 20 minutes, switch OFF (any time of day)
* If switch turned ON not between 8pm to 7am then switch OFF
And, for economy of time, here’s the six automations, described:
1. Trigger: Switch ON
Condition: Door Open FOR 20 minutes
Action: Switch OFF
2. Trigger: Time = 8pm
Action: Switch ON
3. Trigger: Time = 7am
Action: Switch OFF
4. Trigger: Thermostat HVAC Action = cooling
Condition: between 8pm and 7am
Action: Switch ON
5. Trigger: Thermostat HVAC Action = idle
Action: Switch OFF
6. Trigger: Door open for 20 minutes
Action: Switch OFF
And for those who want the code for respective automations, see below. And, for the record, not looking for a code “handout” (lol), just some pointers how I can economically build scenarios. Thanks, all!
- id: '1622133713481'
alias: BdrmAC - Off if Door Open
description: ''
trigger:
- platform: state
entity_id: switch.meross_plug_e_mss110_main_channel
to: 'on'
for: 00:20:00
condition:
- condition: state
entity_id: input_boolean.sliding_door
state: 'on'
#Note: 'sliding_door' = ON means door is open
action:
- service: switch.turn_off
target:
entity_id: switch.meross_plug_e_mss110_main_channel
mode: single
- id: '1622134747922'
alias: BdrmAC - On at 8pm
description: ''
trigger:
- platform: time
at: '20:00:00'
condition: []
action:
- service: switch.turn_on
target:
entity_id: switch.meross_plug_e_mss110_main_channel
mode: single
- id: '1622134831468'
alias: BdrmAC - Off at 7am
description: ''
trigger:
- platform: time
at: '7:00:00'
condition: []
action:
- service: switch.turn_on
target:
entity_id: switch.meross_plug_e_mss110_main_channel
mode: single
- id: '1622134937892'
alias: BdrmAC - Mirror Thermostat On
description: ''
trigger:
- platform: state
entity_id: climate.thermostat
attribute: hvac_action
to: cooling
condition:
- condition: time
after: '20:00:00'
before: '7:00:00'
action:
- service: switch.turn_on
target:
entity_id: switch.meross_plug_e_mss110_main_channel
mode: single
- id: '1622134961363'
alias: BdrmAC - Mirror Thermostat Off
description: ''
trigger:
- platform: state
entity_id: climate.thermostat
attribute: hvac_action
to: idle
condition: []
action:
- service: switch.turn_off
target:
entity_id: switch.meross_plug_e_mss110_main_channel
mode: single
- id: '1622135590806'
alias: BdrmAC - Off if Door Open2
description: ''
trigger:
- platform: state
entity_id: input_boolean.sliding_door
to: 'on'
for: 00:20:01
condition: []
action:
- service: switch.turn_off
target:
entity_id: switch.meross_plug_e_mss110_main_channel
mode: single
If this were my configuration, I’d probably try to set it up as two automations: one for on, one for off — but as @Stiltjack says, there’s no actual benefit in two versus six.
For what it’s worth, have you considered using more, shorter automations?
Not quite sure what it is you’re switching on and off (heater?), but if you represent it with an input_boolean flag and have two short automations to switch the device on when the flag in ‘on’ and off when the flag is ‘off’, you can then create as many conditions as you like in other short automations to manipulate the flag. I’m a big fan of flags.
I’m with Stiltjack here. I’ve found it easier, when I have a set of independent conditions, to put them in separate automations. That way, I don’t have to read through the other code to figure out whether a particular one is working.
However, it might be a good idea to put some comments in the separate automations that say “switch.meross_plug_e_mss110_main_channel is also affected by these other automations [list]”.
Back in my early coding days I learned about the “Shirley Temple Principle”, which is basically “Don’t get too cute.”
1. Trigger: Switch ON
Condition: Door Open FOR 20 minutes
Action: Switch OFF
isn’t faithfully implemented by the first automation (it waits for the switch to be on for 20 minutes and then simply checks if the door is open):
Click to reveal code
- id: '1622133713481'
alias: BdrmAC - Off if Door Open
description: ''
trigger:
- platform: state
entity_id: switch.meross_plug_e_mss110_main_channel
to: 'on'
for: 00:20:00
condition:
- condition: state
entity_id: input_boolean.sliding_door
state: 'on'
#Note: 'sliding_door' = ON means door is open
action:
- service: switch.turn_off
target:
entity_id: switch.meross_plug_e_mss110_main_channel
Which represents what you really want?
Troon has already mentioned that the third automation does the opposite of what the third requirement states (it turns the switch on when the requirement is to turn it off).
The second and third automations can be easily combined into one:
- id: 'whatever'
alias: BdrmAC - On at 8pm off at 7am
trigger:
- platform: time
at:
- '20:00:00'
- '07:00:00'
action:
- service: "switch.turn_{{ 'on' if trigger.now.hour == 20 else 'off' }}"
target:
entity_id: switch.meross_plug_e_mss110_main_channel
mode: single
The fourth and fifth automations have some commonality in that they both employ a State Trigger based on your thermostat. However, one uses a condition whereas the other doesn’t. The combined automation needs to use choose to determine the specifics of which trigger occurred and employ the appropriate condition.
- id: 'something'
alias: BdrmAC - Mirror Thermostat On or Off
trigger:
- platform: state
entity_id: climate.thermostat
attribute: hvac_action
to:
- 'cooling'
- 'idle'
action:
- choose:
- conditions:
- "{{ trigger.to_state.state == 'cooling' }}"
- "{{ now().hour >= 20 or now().hour < 7 }}"
sequence:
- service: switch.turn_on
target:
entity_id: switch.meross_plug_e_mss110_main_channel
- conditions: "{{ trigger.to_state.state == 'idle' }}"
sequence:
- service: switch.turn_off
target:
entity_id: switch.meross_plug_e_mss110_main_channel
mode: single
Awesome, thank you all for your feedback, tips, and advice. I’m always looking for BDP’s (best domesticated practices [jeez, my corporate lingo is entering my everyday life]).
I appreciate all of you! And, good catch on the “off at 7am” mistype, too.