Automations with scenes based on time of day and sun

Hi all,

I’m new to HA and have been trying to figure out this automation for several days now. I wonder if someone could take a look and point out where I’m going wrong.

I’d like to enable the following rules, in response to the bathroom motion sensor detecting motion.

  1. During the daylight hours, activate the bathroom_bright scene
  2. When it gets dark in the evening, activate the bathroom_dim scene, up until 11pm
  3. Overnight, when I’m asleep, activate the bedroom_night scene. Use this scene all night, from 11pm, until sunrise in the morning.

Here’s what I have in my automation…

- id: '1608850590263'
  alias: Bathroom Motion Lights Time-Aware
  description: ''
  trigger:
  - type: motion
    platform: device
    device_id: 76895285ba3f80a9b8388425696f3017
    entity_id: binary_sensor.motion_bathroom_hue_sensor
    domain: binary_sensor
  condition: []
  action:
  - condition: sun
    after: sunrise
    before: sunset
  - scene: scene.bathroom_bright
  - condition: or
    conditions:
    - condition: time
      after: '23:00'
    - condition: time
      before: '11:00'
  - scene: scene.bathroom_night
  - condition: time
    before: '23:00'
  - scene: scene.bathroom_dim
  mode: single

Thank you in advance! :slight_smile:

Don’t use conditions in the action block. As soon as one of them evaluates as false the automation stops.

Use the choose action instead: https://www.home-assistant.io/docs/scripts#choose-a-group-of-actions

1 Like

Here are two ways to do it:

Example 1

Using choose to determine which scene to turn on:

- id: '1608850590263'
  alias: Bathroom Motion Lights Time-Aware
  trigger:
  - platform: state
    entity_id: binary_sensor.motion_bathroom_hue_sensor
    to: 'on'
  action:
  - choose:
    - conditions: "{{ states('sun.sun', 'above_horizon') }}"
      sequence:
      - service: scene.turn_on
        data:
          entity_id: scene.bathroom_bright
    - conditions: "{{ now().hour >= 23 or now().hour < 11 }}"
      sequence:
      - service: scene.turn_on
        data:
          entity_id: scene.bathroom_night
    - conditions: "{{ now().hour < 23 }}"
      sequence:
      - service: scene.turn_on
        data:
          entity_id: scene.bathroom_dim

Example 2

Using a template to determine which scene to turn on:

- id: '1608850590263'
  alias: Bathroom Motion Lights Time-Aware
  trigger:
  - platform: state
    entity_id: binary_sensor.motion_bathroom_hue_sensor
    to: 'on'
  action:
  - service: scene.turn_on
    data:
      entity_id: >
        scene.bathroom_{{ 'bright' if states('sun.sun', 'above_horizon')
                          else 'night' if now().hour >= 23 or now().hour < 11 
                          else 'dim' if now().hour < 23 else 'bright' }}
3 Likes

This is incredibly helpful. Thank you so much!

Am I right in saying that “choose” will test those statements in order, and execute the sequence the first time it matches?