Trying to turn on smart bulbs with motion detector but set certain brightness after 9pm

I am very new to Home Assistant and it looks like an amazing tool but a little intimidating. I have my laundry room setup with a motion sensor that turns on the lights when you enter. Now what I would like to do is set the brightness level of the bulbs to 40% after 9pm and I just have no idea where to set that up. At this point I am assuming some kind of script but I don’t know HA scripting. I am well versed in scripting using other script languages like Powershell so I don’t think it would take me long to figure things out if I can get pushed in the right direction. Any help is greatly appreciated. Thanks.

Please share the YAML of your motion light automation correctly formatted for the forum. See: https://community.home-assistant.io/t/how-to-help-us-help-you-or-how-to-ask-a-good-question/114371#oneone-format-it-properly-16

I have an Automation that is triggered by a motion sensor.

This is my action to set the brightness to 100% between 7am and 10pm. After 10pm and before 7am it sets the brightness to only 10%.

service: light.turn_on
data:
  brightness_pct: "{{ [10,100][7 <= now().hour <= 21] }}"
target:
  entity_id: light.some_lights

The part enclosed by the “{{” and “}}” are called templates and can contain code that is evaluated before the action is run. Templates use Jinja syntax.

Nice solution. I used if-then conditions, but not nearly as elegant as templates.

This is how new I am with HomeAssistant. I don’t know where to put this code in. If there is a how to or some place that gives me a quick overview I can check that out.

Is this what you are looking for?

id: '1708208287600'
alias: Laundry Room Lights
description: Turn on lights with motion detector
use_blueprint:
  path: homeassistant/motion_light.yaml
  input:
    motion_entity: binary_sensor.motion_sensor_motion
    light_target:
      device_id:
        - 52f17608af19812d87397450ad88bb57
        - 5fe79449cd902068e692c6d3abca0732
    no_motion_wait: 5

I think I found a light automation blueprint you can use to do what you want (time based brightness) that isn’t overly complicated.

Go there and click on the blue “Import blueprint” button at the top and it should appear in the blueprints you can make an automation from. Hope this helps, it should avoid needing to write any code yourself.

I finally have it working not sure if I made it more complicated then it needed to be but here is my automation yaml for doing this setup.

- id: '1709913762033'
  alias: Laundry Room w/Brightness
  description: after 9pm and before 7am the light will be very dimmed
  trigger:
  - platform: state
    entity_id:
    - binary_sensor.motion_sensor_motion
    from: 'off'
    to: 'on'
    for:
      hours: 0
      minutes: 0
      seconds: 0
  condition: []
  action:
  - if:
    - condition: time
      after: '21:00:00'
      weekday:
      - sun
      - mon
      - tue
      - wed
      - thu
      - fri
      - sat
      before: 07:00:00
    then:
    - service: light.turn_on
      metadata: {}
      data:
        brightness_pct: 12
      target:
        area_id: laundry_room
    - wait_for_trigger:
      - platform: state
        entity_id:
        - binary_sensor.motion_sensor_motion
        from: 'on'
        to: 'off'
    - service: light.turn_off
      metadata: {}
      data: {}
      target:
        area_id: laundry_room
    else:
    - service: light.turn_on
      metadata: {}
      data:
        brightness_pct: 100
      target:
        area_id: laundry_room
    - wait_for_trigger:
      - platform: state
        entity_id:
        - binary_sensor.motion_sensor_motion
        from: 'on'
        to: 'off'
        for:
          hours: 0
          minutes: 0
          seconds: 10
    - service: light.turn_off
      metadata: {}
      data: {}
      target:
        area_id: laundry_room
  mode: single

if you’d like it simpler you could do something like the below. and even the below can get simpler…

note that one functional difference i have in my version is that it always waits for 10 seconds after motion detection is off. whereas you waited 10 seconds after 9pm and didn’twait at all before 9pm. if that’s important then i’d have to tweak mine a little.

also, i’m guessing you chose 9pm because that’s a good guess as to when things get dark… but you might take advantage of the fact that homeassistant can know when sunset is… so you could change that to be something like 1hour after sunset… until 1 hour before sunrise or something like that…

other notes… if you want every day, you don’t have to specify each day. just leave it off and it will assume every day. only do that if you don’t want every day.

notice also that i put no motion as a primary trigger. i didn’t do a “wait” there are a few reasons for this. e.g. if homeassistant reboots during the wait, you never turn off.

there’s more code beauty you could achieve beyond this, but since you’re brand new and learning, i thought i’d share some of the aspects that would actually impact your learning and the resilience of your automation…

cheers!

alias: Laundry Room w/Brightness
description: after 9pm and before 7am the light will be very dimmed
trigger:
  - platform: state
    entity_id:
      - binary_sensor.motion_sensor_motion
    from: "off"
    to: "on"
    id: motion_detected
  - platform: state
    entity_id:
      - binary_sensor.motion_sensor_motion
    from: "on"
    to: "off"
    id: no_motion_detected
    for:
      seconds: 10
condition: []
action:
  - choose:
      - conditions:
          - condition: trigger
            id:
              - motion_detected
        sequence:
          - if:
              - condition: time
                after: "21:00:00"
                before: "07:00:00"
            then:
              - service: light.turn_on
                data:
                  brightness_pct: 12
                target:
                  area_id: laundry_room
            else:
              - service: light.turn_on
                data:
                  brightness_pct: 100
                target:
                  area_id: laundry_room
      - conditions:
          - condition: trigger
            id:
              - no_motion_detected
        sequence:
          - service: light.turn_off
            target:
              area_id: laundry_room
mode: single

Thank you very much for this. I thought about using sunset but my reason for it is the laundry room is really a laundry closet outside of our master bedroom and I didn’t want the bright light coming on when I was putting my close in the basket at night waking up my wife. I really appreciate you putting this together showing me another way.