Automation and triggers

Hey all, I’m trying to understand how trigger names can be used in automations. I have a simple thing, I have a switch which turns on at 8am and off a 8pm. I wanted to just make an automation with two triggers, on at 8:00 off a 20:00. Can I use those both in one automation to do different things? It’s kind of dumb now, I have one automation to turn it on and one to turn it off.

Yes, you can.

alias: Scheduled Switch
trigger:
- platform: time
  at:
  - '08:00:00'
  - '20:00:00'
action:
- service: "switch.turn_{{ 'on' if now().hour == 8 else 'off' }}"
  target:
    entity_id: switch.whatever
1 Like

Another alternative, though not as compact as Taras’ suggestion, this can survive Home Assistant crash/restart should it happen at 8AM/8PM-

trigger:
  - platform: time
    at: '08:00'
  - platform: time
    at: '20:00'
  - platform: homeassistant
    event: start
condition: []
action:
  - choose:
      - conditions:
          - condition: time
            after: '08:00'
            before: '20:00'
        sequence:
          - service: switch.turn_on
            target:
              entity_id: switch.your_switch
    default:
      - service: switch.turn_off
        target:
          entity_id: switch.your_switch
1 Like

I can be equally compact by simply enhancing the template:

alias: Scheduled Switch
trigger:
- platform: time
  at:
  - '08:00:00'
  - '20:00:00'
- platform: homeassistant
  event: start
action:
- service: "switch.turn_{{ 'on' if 8 <= now().hour < 20 else 'off' }}"
  target:
    entity_id: switch.whatever
3 Likes

Awesome! Thank you!

Hey, I’m missing something here, what is the default action? I’m just trying to set this up through the UI, do I have to do this in the yaml?

From the UI-


You can read more about Choose Action here-

Why suffer? Just switch the Automation Editor from visual to YAML mode then copy-paste the example I posted above. Save it and done.