One Automation for two events?

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

Long delays is generally not a good solution since HA could be restarted or automations reloaded which makes the automation break

I tried to mock up what you had, but got sidetracked in the choose conditions phase.

What am I doing wrong here?

- id: '1638287241495'
  alias: New Automation
  description: ''
  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
    - conditions:
      - condition: trigger
        id: 'off'
      sequence:
      - service: switch.turn_off
        target:
          entity_id: switch.fan_controller
    default: []
  mode: single

You only have one choose.
I don’t think that would work.

You could have one choose, and use the other as default.

Did you make that in the GUI?

I actually copied yours into the GUI just to see how it formatted it.

Once i figured that out, went back into the yaml and edited my original. I think it’ll work now, I"ll let you know when the fan kicks on tonight.

Thanks for your help. as @tom_l mentioned, I’ve since started using the Visual Studio editor with the rainbow tabs and that really helps me visualize the code better…

You could just set a different time and see if it works and then switch it off

The start of the process worked so I assume the whole automation will work…

Thank you!

Please mark Hellis81’s post as the solution.

This will work in this case, but you only need one Choose action for this automation.

Just to point out, the automation can be further simplified by simply adding an id to the other trigger too. If the one that turns the fan on has the id ‘on’ and the one to turn the fan off has the id ‘off’ then you can use:

service: switch.turn_{{ trigger.id }}

And then you don’t need to do any of the if stuff.

1 Like

When I had this in my Automations I kept getting errors, so I went with the slightly more complex idea…