Multiple time conditions with actions

Not sure if I completely understand the and/or function of conditions or my formatting is wrong, but I can’t get this to work. Looking for a little guidance.

I have a PIR sensor that turns the bathroom light on, but I’m trying to add additional conditions to produce different brightness at different time windows.

Here’s the automation:

- alias: "Bathroom Light PIR"
  trigger:
  - entity_id: binary_sensor.bathroom_pir
    platform: state
    to: 'on'
  condition:
    - condition: or
      conditions:
      - condition: time
        after: '16:00'
        before: '23:30'
        action:
        - device_id: abc123
          domain: light
          entity_id: light.bathroom_light
          type: turn_on
          brightness: 229
      - condition: time
        after: '23:30'
        before: '05:00'
        action:
        - device_id: abc123
          domain: light
          entity_id: light.bathroom_light
          type: turn_on
          brightness: 76
      - condition: time
        after: '05:00'
        before: '08:00'
        action:
        - device_id: abc123
          domain: light
          entity_id: light.bathroom_light
          type: turn_on
          brightness: 229

What was used to create this? The Automation Editor? It’s quite an interesting mish mash.

I suggest you review the new choose option:

my understanding of automations

the logic is

trigger
condition
action

looks like you have

trigger
condition
action
condition
action
condition
action

which sound right but looks wrong

If im copy paste right you can do this with a CHOOSE in your action.
I make use of this in many scripts to create scenes for certain moments in year:

- alias: "Bathroom Light PIR"
  trigger:

  - entity_id: binary_sensor.bathroom_pir
    platform: state
    to: 'on'

  action:
    - choose:
# BETWEEN 16:00 and 23:30
        - conditions:
            - condition: time
              after: '16:00'
              before: '23:30'
          sequence:
            - service: light.turn_on
              data:
                entity_id: light.bathroom_light
                brightness: 229

# BETWEEN 23:30 and 05:00
        - conditions:
            - condition: time
              after: '23:30'
              before: '05:00'
          sequence:
            - service: light.turn_on
              data:
                entity_id: light.bathroom_light
                brightness: 76

# BETWEEN 05:00 and 08:00
        - conditions:
            - condition: time
              after: '23:30'
              before: '05:00'
          sequence:
            - service: light.turn_on
              data:
                entity_id: light.bathroom_light
                brightness: 229

# A DEFAULT WHEN NOTHING ABOVE IS TRUE IN CONDITION
      default:
        - service: light.turn_on
          data_template:
            entity_id: light.bathroom_light
            brightness_pct: 229

You can use conditions combined with different actions, but then you have to narrow down the conditions.
Say:
Between 16:00 and 08:00 lights on ‘brightness: 229’
Between 23:30 and 05:00 lights on ‘brightness: 76’

The second will dim the lights back to 76/255th if it is between 23:30 and 05:00.

However the easier and more flexible way is to use the rather new CHOOSE option. It will explain itself once you pick the CHOOSE option, give your condition, followed by your action.

My preference would be to use a data_template to compute the desired brightness.

- alias: "Bathroom Light PIR"
  trigger:
  - platform: state
    entity_id: binary_sensor.bathroom_pir
    to: 'on'
  condition:
    condition: not
    conditions:
      - condition: time
        after: '08:00'
        before: '16:00'
  action:
    - service: light.turn_on
      data_template:
        entity_id: light.bathroom_light
        brightness: >
          {% set t = (now().time() | string)[:5] %}
          {% if   '16:00' <= t < '23:30' %} 229
          {% elif '23:30' <= t < '05:00' %} 76
          {% elif '05:00' <= t < '08:00' %} 229
          {% else %} 0
          {% endif %}

The condition excludes the time period between 08:00 and 16:00.
The brightness template gets the current local time as a string (like 07:35) and proceeds to compare it to three time ranges.

2 Likes

Thanks everyone, this really helps. Going to test run the data_template method as it’s more compact. Much appreciated. I don’t use code generators, purely experimenting based on other codes and writing it myself (well, trying :wink: )

I used same templates. When you gonna use more lights in the action then my way would be OK

I’m trying to implement the “1st” option, but somehow the light does’t turn on the light between 22:00h and sunrise.
Looks like I’m missing or misinterpreting something, right?

- id: motion_sensor
  alias: "Motion Sensor"
  trigger:

  - entity_id: binary_sensor.tradfri_motion_sensor_2
    platform: state
    to: 'on'

  action:
    - choose:
  # BETWEEN sunrise and 22:00
        - conditions:
            - condition: sun
              after: sunrise
            - condition: time
              before: '22:00'

          sequence:
            - service: light.turn_on
              data:
                entity_id: light.color_temperature_light_2
                brightness_pct: 100
                color_temp: 250
                transition: 5

  # BETWEEN 22:00 and sunrise
        - conditions:
            - condition: time
              after: '22:00'
            - condition: sun
              before: sunrise

          sequence:
            - service: light.turn_on
              data:
                entity_id: light.color_temperature_light_2
                brightness_pct: 10
                color_temp: 454
                transition: 5

  # A DEFAULT WHEN NOTHING ABOVE IS TRUE IN CONDITION
      default:
        - service: light.turn_off
          data_template:
            entity_id: light.color_temperature_light_2

Thanks for the help!

Btw, I’m turning the light off after 3 min when motion sensor is off, with an automation.

BETWEEN sunrise and 22:00
BETWEEN 22:00 and sunrise
A DEFAULT WHEN NOTHING ABOVE IS TRUE IN CONDITION

The first two conditions cover all 24 hours in a day so when do imagine the default condition will ever occur?

Sometimes you try couple of things a few days after each other, post a question and 10 min later you find the answer. :thinking:

Below is what I meant and is working. :wink:

  trigger:

  - entity_id: binary_sensor.tradfri_motion_sensor_2
    platform: state
    to: 'on'

  mode: queued
  action:
    - choose:
  # (IF Motion detected) BETWEEN sunrise and 22:00
        - conditions:
            - condition: sun
              after: sunrise
            - condition: time
              before: '22:00'

          sequence:
            - service: light.turn_on
              data:
                entity_id: light.color_temperature_light_2
                brightness_pct: 100
                color_temp: 250
                transition: 5

  # (ELSE)
      default:
        - service: light.turn_on
          data:
            entity_id: light.color_temperature_light_2
            brightness_pct: 10
            color_temp: 454
            transition: 5