Time Trigger in Automation Chooser

So I have an Automation that has a bunch of triggers. One of these triggers is Time based, example

  - platform: time
    at: '11:00:00'
  - platform: time
    at: '15:00:00'

After that I’m using a chooser for the actions.

My question is can I use the time in a chooser condition and guarantee it will match the trigger?

For example, in the chooser condition, how can I test for 11:00:00? Do I have to do After 10:59:59 and Before 11:00:01 ?

What is the best way for me to do the Chooser with different time triggers?

It would be helpful to be able to pull out easily which trigger caused the automation to run.

Thanks!!

Yes that would be the best way.

Basically, like this:

- alias: Example 1
  id: ex_1
  trigger:
  - platform: time
    at:
    - '11:00:00'
    - '15:00:00'
  action:
  - choose:
    - conditions: "{{ trigger.now.hour == 11 }}"
      sequence:
      - service: notify.persistent_notification
        data:
          title: "Example 1"
          message: "Triggered at eleven."
    - conditions: "{{ trigger.now.hour == 15 }}"
      sequence:
      - service: notify.persistent_notification
        data:
          title: "Example 1"
          message: "Triggered at fifteen."

Replace the service calls in choose with whatever you need.

2 Likes

I overlooked to mention that if the goal is simply to turn on something at 11:00:00 and turn it off at 15:00:00 then you don’t need to use choose. It can be done like this:

- alias: Example 2
  id: ex_2
  trigger:
  - platform: time
    at:
    - '11:00:00'
    - '15:00:00'
  action:
  - service: "light.turn_{{ 'on' if trigger.now.hour == 11 else 'off' }}"
    target:
      entity_id: light.whatever

You do need to use choose if, for example, the service call at 11:00:00 is not the same as at 15:00:00.

1 Like