Different actions based on time of day

What I’m trying to do is:
if motion is detected between 8am - 10pm
turn on lights 1 and 2 at full brightness
shut off after 4 minutes

if motion is detected between 10:01pm and 7:59am
turn on lights 1 and 2 at 30% brightness
shut off after 4 minutes

What is the best way to do this?

multiple automations with specific conditions.

Trigger on motion, filter on time condition.

This is what i have now - but don’t know python at all.
Sensor day is set via a simple rule of

  • alias: Kitchen light auto on
    trigger:
    platform: state
    entity_id: binary_sensor.motion_sensor_158d00012545b5
    from: ‘off’
    to: ‘on’
    condition:
    condition: numeric_state
    entity_id: sun.sun
    value_template: ‘{{ state.attributes.elevation }}’
    below: 7
    action:
    service: light.turn_on
    entity_id: light.fibaro_system_fgd211_universal_dimmer_500w_level_6_0
    data_template:
    brightness: >
    {%- if is_state(“sensor.day_night”, “Evening”) %}
    255
    {%- elif is_state(“sensor.day_night”, “Night”) %}
    60
    {%- else %}
    255
    {% endif %}

sensor.yaml

  • platform: template
    sensors:
    day_night:
    friendly_name: ‘Day and night’
    value_template: >-
    {%- if now().hour >= 20 and now().hour < 24 %}
    Evening
    {% elif now().hour >= 0 and now().hour < 6 %}
    Night
    {% else %}
    Day
    {%- endif %}
1 Like

Ok so this is what I have so far. This turns the lights on to the specified brightness at certain times but it still won’t wait the 4 minutes before turning off:

  - alias: Bathroom On
    trigger:
      - platform: state
        entity_id: switch.bathroom_motion
        to: 'on'
    condition:
      - condition: time
        after: '08:01:00'
        before: '20:59:00'
    action:
      - service: scene.turn_on
        entity_id: scene.bath_bright_x2
      - delay: 00:04:00
      - service: scene.turn_on
        entity_id: scene.bath_lights_off

  - alias: Nightlight
    trigger: 
      - platform: state
        entity_id: switch.bathroom_motion
        to: 'on'
    condition:
      - condition: time
        after: '21:00:00'
        before: '08:00:00'
    action:
      - service: scene.turn_on
        entity_id: scene.bath_low_x2
      - delay: 00:04:00
      - service: scene.turn_on
        entity_id: scene.bath_lights_off

Try adding some single or double quotes around your delay like so.

- delay: '00:04:00'

This helped me when I was trying to accomplish a similar thing https://home-assistant.io/cookbook/turn_on_light_for_10_minutes_when_motion_detected/

HASS Cookbook has a lot of great tips and tricks.

Having all those scenes handling different brightness levels didn’t look good to me so instead i call them up like below. As you have a motion sensor isn’t it better to use it instead of delay?

########## BATHROOM LIGHT ON
- alias: Bath light auto on
  trigger:
    platform: state
    entity_id: binary_sensor.motion_sensor_bath
    from: 'off'
    to: 'on'
  action:
    service: light.turn_on
    entity_id: light.fibaro_system_fgd212_dimmer_2_level_2_0
    data_template:
      brightness: >
        {%- if is_state("sensor.day_night", "Evening") %}
        255
        {%- elif is_state("sensor.day_night", "Night") %}
        60
        {%- else %}
        255
        {% endif %}
########## BATHROOM LIGHT OFF
- alias: Bath light auto off
  trigger:
    platform: state
    entity_id: binary_sensor.motion_sensor_bath
    from: 'on'
    to: 'off'
    for:
      minutes: 2
#The following condition uses a door sensor as well so light won't switch off
  condition:
    condition: state
    entity_id: input_boolean.bathroom_occupancy
    state: 'off'
  action:
    service: light.turn_off
    entity_id: light.fibaro_system_fgd212_dimmer_2_level_2_0