Motion/Occupancy-activated Light Blueprint for Home Assistant

A Home Assistant automation blueprint that provides advanced motion-activated lighting control with multiple conditions and customization options.

Features

  • Control lights and switches based on motion detection from one or more sensors
  • Optional custom brightness and color settings of lights
  • Conditional activation based on the state of another entity
  • Sun position awareness with configurable offset (day/night conditions)
  • Blocking functionality to prevent activation in certain scenarios
  • Configurable wait time after motion stops

Open your Home Assistant instance and show the blueprint import dialog with a specific blueprint pre-filled.

blueprint:
  name: Motion-activated Light with Conditions, Exceptions, Optional Settings, and Sun Offset
  description: Turn on lights when motion is detected, with optional brightness and color settings, conditional entity state check, and sun position check with offset.
  domain: automation
  input:
    motion_sensors:
      name: Motion and Occupancy Sensors
      description: Select one or more motion or occupancy sensors.
      selector:
        entity:
          domain: binary_sensor
          device_class:
            - motion
            - occupancy
          multiple: true
    entity_target:
      name: Lights and Switches
      description: Select one or more lights or switches to control.
      selector:
        target:
          entity:
            domain:
              - light
              - switch
    no_motion_wait:
      name: Wait Time
      description: Time to leave the light on after last motion is detected.
      default: 120
      selector:
        number:
          min: 0
          max: 3600
          unit_of_measurement: seconds
    use_custom_settings:
      name: Use Custom Brightness and Color
      description: Enable to use custom brightness and color settings.
      default: false
      selector:
        boolean:
    brightness:
      name: Brightness
      description: Set the brightness level (0-255).
      default: 255
      selector:
        number:
          min: 0
          max: 255
    color:
      name: Color
      description: Set the color (in RGB format).
      default: [255, 255, 255]
      selector:
        color_rgb:
    condition_entity:
      name: Condition Entity (Optional)
      description: Select an entity to check its state (optional). If no entity is defined, this condition is not evaluated.
      default:
      selector:
        entity:
    condition_states:
      name: Allowed States for Condition Entity
      description: Enter the states that the condition entity should be in (comma-separated). If a Condition Entity is not defined these values are ignored.
      default:
      selector:
        text:
    sun_condition:
      name: Sun Condition (Optional)
      description: Select the sun condition to check.
      default: none
      selector:
        select:
          options:
            - none
            - day
            - night
    sun_offset:
      name: Sun Offset (Optional)
      description: Offset from sunrise/sunset in format 'HH:MM' (e.g., '01:00' or '-01:00')
      default: "00:00"
      selector:
        text:
    blocking_entity:
      name: Blocking Entity (Optional)
      description: Select an entity that will prevent the automation from running when in specified states.
      default:
      selector:
        entity:
    blocking_states:
      name: Blocking States
      description: Enter the states that will prevent the automation from running (comma-separated).
      default:
      selector:
        text:

mode: restart
max_exceeded: silent

trigger:
  - platform: state
    entity_id: !input motion_sensors
    to: "on"

variables:
  use_custom_settings: !input use_custom_settings
  condition_entity: !input condition_entity
  condition_states: !input condition_states
  sun_condition: !input sun_condition
  sun_offset: !input sun_offset
  blocking_entity: !input blocking_entity
  blocking_states: !input blocking_states
  entity_target: !input entity_target

condition:
  - condition: and
    conditions:
      - condition: or
        conditions:
          - condition: template
            value_template: "{{ blocking_entity == None or blocking_entity == '' }}"
          - condition: template
            value_template: >
              {% set blocking_states_list = blocking_states.split(',') | map('trim') | list %}
              {{ states(blocking_entity) not in blocking_states_list }}
      - condition: or
        conditions:
          - condition: template
            value_template: "{{ condition_entity == None or condition_entity == '' }}"
          - condition: template
            value_template: >
              {% set states_list = condition_states.split(',') | map('trim') | list %}
              {{ condition_entity != None and condition_entity != '' and states_list | length > 0 and states(condition_entity) in states_list }}
      - condition: or
        conditions:
          - condition: template
            value_template: "{{ sun_condition == 'none' }}"
          - condition: and
            conditions:
              - condition: template
                value_template: "{{ sun_condition == 'day' }}"
              - condition: sun
                after: sunrise
                after_offset: !input sun_offset
                before: sunset
                before_offset: !input sun_offset
          - condition: and
            conditions:
              - condition: template
                value_template: "{{ sun_condition == 'night' }}"
              - condition: or
                conditions:
                  - condition: sun
                    after: sunset
                    after_offset: !input sun_offset
                  - condition: sun
                    before: sunrise
                    before_offset: !input sun_offset

action:
  - choose:
      - conditions:
          - condition: template
            value_template: "{{ use_custom_settings }}"
        sequence:
          - choose:
              - conditions:
                  - condition: template
                    value_template: "{{ 'light.' in entity_target.entity_id|string }}"
                sequence:
                  - service: light.turn_on
                    target: !input entity_target
                    data:
                      brightness: !input brightness
                      rgb_color: !input color
              - conditions:
                  - condition: template
                    value_template: "{{ 'switch.' in entity_target.entity_id|string }}"
                sequence:
                  - service: switch.turn_on
                    target: !input entity_target
    default:
      - choose:
          - conditions:
              - condition: template
                value_template: "{{ 'light.' in entity_target.entity_id|string }}"
            sequence:
              - service: light.turn_on
                target: !input entity_target
          - conditions:
              - condition: template
                value_template: "{{ 'switch.' in entity_target.entity_id|string }}"
            sequence:
              - service: switch.turn_on
                target: !input entity_target
  - wait_for_trigger:
      platform: state
      entity_id: !input motion_sensors
      to: "off"
  - delay: !input no_motion_wait
  - choose:
      - conditions:
          - condition: template
            value_template: "{{ 'light.' in entity_target.entity_id|string }}"
        sequence:
          - service: light.turn_off
            target: !input entity_target
      - conditions:
          - condition: template
            value_template: "{{ 'switch.' in entity_target.entity_id|string }}"
        sequence:
          - service: switch.turn_off
            target: !input entity_target

Thank you for this. I have just tried it, but it does not allow me to use my Nest Protect as a motion sensor, and suspect because the device class is not correct. The built-in Home Assistant motion light blueprint does work with the Nest Protect. I wonder whether you might modify this to allow it to work? Thank you.

Hey, this blueprint is perfect for what I need! Super easy to use. Thanks a lot!

I can choose the IKEA Tradfri motion sensor, but not the IKEA Vallhorn sensor. Are you planning to include more sensors or the way they are identified?

Hey,
Ive figgured out how to integrate all sensors that were not shown in list initially. When opening the sensor you can change the Sensor type to “motion” sensor.

1 Like

I’m sorry you were having issues, I did not get notified about your message. I’ll have to check my account settings. Happy you resolved your issue.

I can have a look into this and see if there is something that I can do.

This blueprint looks pretty nice, it has the advanced options I found were missing in others to make it much more simple to use - like sun and blocking states.

I’m guessing the light selection is filtered to certain device types. Is it possible to add device type switch ? I have many Lutron switches and dimmers that control my outside lights, I do see the dimmers but not the switches for some reason.

Here is an example of a missing device - device type is switch.

Use a helper. There is a helper type that creates a Light entity from a Switch entity. Then you can use the Light entity in places where the Switch is not available.

Can you add a time condition for this blueprint? I want the ability to turn on the light for most of the time but not in the middle of the night if I get up to use the bathroom.

This looks like a nice, full featured blueprint. A couple of things - pretty much all of my motion sensors are available through Zigbee2MQTT which seems to expose them as “occupancy” sensors by default - can you add that to the sensor class list (I did that locally).

Could I ask what the syntax is for “Allowed States for Condition Entity”? I want to use the illuminance sensor on the motion sensor to turn on lights for values <50. I tried just putting “<50” in that field but it always fails on that condition

I will have a look at all of these requests, and see what I can do. I did look into the Google Nest request and saw there was issue on how the motion sensor is exposed.

Actually, can you do what @jannishansen did, @jamespetts? Can you go into the entity that you are discussing for Nest, and then change it to be a motion type so it shows up?

So I am working on making this support occupancy too, regarding your other issue - it’s quite complex to implement based on luminance - but I have a suggestion.

can you add a helper, maybe called “light state” or similar, and then you can make it’s state dim when less than 50%, and brith when greater (for example). then in the blueprint, you can select the helper as the conditional entity and set it to dim or bright as needed.

I have a mode for my house, morning, evening, bedtime, and goodnight… I use this entity to restrict actions to the various states.

Similar to other suggestion I have made, I will tell you how I handle this (adding a time condition that could overlap days could get complex).

For me I have a helper entity called Mode, and it represents the state of my house. Certain actions change the state of Mode between: morning, evening, bedtime, goodnight. I then use this entity as my conditional evaluator, so certain actions are not carrier out it goodnight (because everyone is sleeping), having this approach will allow you to use the helper state in other automations for you.

OK, I may have got this to work for an occupancy sensor - no way for me to test, let me know.

I have added switches as a target type - please reimport and let me know

I have added occupancy as an input, feel free to reimport, test, and let me know. Thanks for using.

I believe you should be able to put <50 in as a state… but I have not tested, I suggest you use my other suggestion of creating a helper entity.

Does anyone know how I can edit my original post, I made changes to address the comments - but I can’t see how to edit. The pencil icon just brings up history of the post???

I’d like to update the code block, it is old now based on my changes. Use the button to import the latest version.

It should work via the … menu below the post.
There you find the pencil icon to edit it.