I’m finding that I have a lot of automations for the same device, like for instance one to turn on a light, one to turn it off, and figure there has to be a way to combine the two into one using a choose option, but every time I go down that path, I get really confused.
I would like the fan to turn on at 2030 and turn off at 0630 every day. Figure it can’t be that hard, but still learning this whole YAML thing.
Anyone able to help point me in the right direction?
- id: '1638223769335'
alias: Fan Off
description: Tun off fan in the morning
trigger:
- platform: time
at: 06:30:00
condition: []
action:
- type: turn_off
device_id: 7bd734ad91a848408f4105ba0c31012d
entity_id: switch.fan_controller
domain: switch
mode: single
and
- id: '1638223845671'
alias: Fan On
description: Turn on fan at bedtime
trigger:
- platform: time
at: '20:30:00'
condition: []
action:
- type: turn_on
device_id: 7bd734ad91a848408f4105ba0c31012d
entity_id: switch.fan_controller
domain: switch
mode: single
You will have to give that up if you want to do more advanced automations like this:
- id: '1638223769335'
alias: Fan Control
description: Tun off fan in the morning, on at bedtime
trigger:
- platform: time
at: 06:30:00
- platform: time
at: '20:30:00'
id: 'on'
condition: []
action:
- service: >
switch.turn_{{'on' if trigger.id = 'on' else 'off' }}
target:
entity_id: switch.fan_controller
mode: single
You make one up that reflects what the trigger is for. I could just as easily have called it this:
- id: '1638223769335'
alias: Fan Control
description: Tun off fan in the morning, on at bedtime
trigger:
- platform: time
at: 06:30:00
- platform: time
at: '20:30:00'
id: 'bedtime' # whatever you want here
condition: []
action:
- service: >
switch.turn_{{'on' if trigger.id = 'bedtime' else 'off' }}
target:
entity_id: switch.fan_controller
mode: single
You can also just use the default numbering of trigger ids that starts at 0 for the first trigger then counts up, but it is less intuitive.
- id: '1638223769335'
alias: Fan Control
description: Tun off fan in the morning, on at bedtime
trigger:
- platform: time # trigger id 0
at: 06:30:00
- platform: time # trigger id 1
at: '20:30:00'
condition: []
action:
- service: >
switch.turn_{{'on' if trigger.id = 1 else 'off' }}
target:
entity_id: switch.fan_controller
mode: single
Whats the best site you recommend for trying to learn better YAML code / more complex actions?
One of the reasons why I have issues with YAML, and why I didn’t become a coder, is the spacing and the inability to figure out why something isn’t working when its in code…
If you use the automation editor, automations are reloaded when you click ‘save’. If creating automations directly in .yaml, you need to reload automations.
That was all done with the mouse, except the switch name that I had to copy paste since that entity isn’t mine, and the ID ‘on’ and ‘off’ in the time triggers.