One Automation for two events?

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?

Yes. Post your two automations.

- 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

Are you using the GUI automation editor?

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

Ya, I am

I guess I"ll need to read up on YAML and the purpose of the curly brackets…

No. When this trigger occurs:

  - platform: time
    at: '20:30:00'
    id: 'on'

It has the trigger id ‘on’. Which means that this service:

switch.turn_{{'on' if trigger.id = 'on' else 'off' }}

Resolves to

switch.turn_on

If the other trigger occurs the service template resolves to

switch.turn_off

i.e. the “else” case.

Note: there was an error in my template. I forgot to quote the ‘on’ value. I have corrected this in my post above.

2 Likes

How do you know what ID to come up with during the coding?

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

Actually, I meant the ID at the top, when you do an automation via YAML file, how do you come up with the number to create the ID…like 1638223769335?

Oh right.

You can put anything there but it must be unique. If you make sure your alias’s are unique you can use them. e.g.

- id: 'fan_control'
  alias: Fan Control

I use Visual Studio Code editor and it has a UUID generator plugin. So I just right click and select “Generate UUID”.

Or you can use an online generator. LIke this, http://guid.one/ and copy / paste.

Awesome thanks.

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…

http://thomasloven.com/blog/2018/08/YAML-For-Nonprogrammers/

https://blog.ceard.tech/2020/05/yaml-in-automations-and-scripts.html

And just looking closely at the home assistant documentation examples, Templating - Home Assistant

Is there any program you use that can show when errors, or catch errors in YAML?

1 Like

Yes.

Visual Studio Code. With the following extensions:

https://marketplace.visualstudio.com/items?itemName=keesschollaart.vscode-home-assistant

https://github.com/oderwat/vscode-indent-rainbow

https://github.com/lukas-tr/vscode-materialdesignicons-intellisense

https://marketplace.visualstudio.com/items?itemName=vscode-icons-team.vscode-icons

https://marketplace.visualstudio.com/items?itemName=2gua.rainbow-brackets

https://marketplace.visualstudio.com/items?itemName=ban.spellright

If you use the Addon some of these are pre-packaged with it.

1 Like

This did not work. The automaton never showed up in the list and the fan didn’t turn on at 2030

If you use the automation editor, automations are reloaded when you click ‘save’. If creating automations directly in .yaml, you need to reload automations.

The GUI is perfectly capable to make good automations.

The same can be done without the template in the GUI like this:

alias: New Automation
description: ''
mode: single
trigger:
  - platform: time
    at: '06:30:00'
    id: 'off'
  - platform: time
    at: '20:30:00'
    id: 'on'
condition: []
action:
  - choose:
      - conditions:
          - condition: trigger
            id: 'on'
        sequence:
          - service: switch.turn_on
            target:
              entity_id: switch.fan_controller
    default: []
  - choose:
      - conditions:
          - condition: trigger
            id: 'off'
        sequence:
          - service: switch.turn_off
            target:
              entity_id: switch.fan_controller
    default: []

But yes templates is good to learn but that does not make it impossible to use the GUI since you can use templates in sections of the GUI

2 Likes

This is something I’d also like to learn, I’ll have a read up on it too

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.

You can configure it in the GUI.

Trigger: time at 20:30.
Action1 : Turn the fan on.
Action 2: Delay for 10 hours.
Action 3: Turn the fan off.

1 Like