Understanding Automation Time Conditions

I was trying to create a single automation to turn an input_boolean on or off depending on time (on at 4pm and off at 8pm). I have two time triggers (4pm / 8pm) and a choose that uses time but I don’t seem to understand how the time choose conditions work.

In the below automation, the choose conditions are for (after) 3:59pm & (before) 4:01pm and the other condition is 7:59pm / 8:01pm.

At 4pm, the trigger fires but the result of the choose is “default”. I’m not sure why it is not choosing the 3:59/4:01pm condition.

Here’s the automation:

id: '1656530783891'
alias: Turn Peak Electricity Toggle On/Off
description: >-
  Turns on Peak Electricity Period boolean
  (input_boolean.peak_electricity_period) at 4pm and turns it off at 8pm
trigger:
  - platform: time
    at: '16:00:00'
  - platform: time
    at: '20:00:00'
condition: []
action:
  - choose:
      - conditions:
          - condition: time
            before: '16:01:00'
            after: '15:59:00'
            weekday: []
        sequence:
          - service: input_boolean.turn_on
            data: {}
            target:
              entity_id: input_boolean.peak_electricity_period
          - service: notify.alexa_media_everywhere
            data:
              data:
                type: announce
              message: Electricity Peak Pricing, Minimize Electrical Usage
      - conditions:
          - condition: time
            before: '20:01:00'
            after: '19:59:00'
            weekday: []
        sequence:
          - service: input_boolean.turn_off
            data: {}
            target:
              entity_id: input_boolean.peak_electricity_period
          - service: notify.alexa_media_everywhere
            data:
              message: Electricity Off Peak Pricing, Resume Electrical Usage
              data:
                type: announce
    default: []
mode: single

and here are the step details:

Trigger:

platform: time
at: '16:00:00'

Choose:

Executed: June 30, 2022 at 4:00:00 PM
Result:
choice: default

Of course, there is no “default” set, so it doesn’t actually do anything.

Originally, I just had a 4pm option and the default was the 8pm (turn off). I also tried just having a time of 4pm/4pm for one option and 8pm/8pm for the other, but that also didn’t work.

In the meantime, I’ve created two automations (one for on and one for off), but I’d prefer to have just one.

What am I not understanding about the time condition?

Thanks,
phlepper

So you should just add a Trigger ID, click the 3 dots menu for the trigger to show the field, to each of your triggers and then you can use the Trigger By ID in your choose without the need for other conditions.

Here’s a YAML example of one of my automations:

platform: time
at: '02:00:00'
id: 2am

and an example of my choose:

choose:
  - conditions:
      - condition: or
        conditions:
          - condition: trigger
            id: 2am
          - condition: trigger
            id: 8am
          - condition: trigger
            id: 2pm
          - condition: trigger
            id: 8pm
id: '1656530783891'
alias: Turn Peak Electricity Toggle On/Off
description: >-
  Turns on Peak Electricity Period boolean
  (input_boolean.peak_electricity_period) at 4pm and turns it off at 8pm
trigger:
  - id: 'on'
    platform: time
    at: '16:00:00'
  - id: 'off'
    platform: time
    at: '20:00:00'
condition: []
action:
  - service: 'input_boolean.turn_{{ trigger.id }}'
    target:
      entity_id: input_boolean.peak_electricity_period
  - service: notify.alexa_media_everywhere
    data:
      data:
        type: announce
      message: "Electricity {{ iif(trigger.id == 'on', '', 'Off') }} Peak Pricing, {{ iif(trigger.id == 'on', 'Minimize', 'Resume') }} Electrical Usage"
mode: single

Thanks for the point in the right direction. This worked great!

Paul

Glad to hear it solved the problem.

Please consider marking my post above with the Solution tag. It will automatically place a check-mark next to the topic’s title which signals to other users that this topic has been resolved. This helps users find answers to similar questions.

For more information refer to guideline 21 in the FAQ.