Home Assistant Blueprint - Advanced Motion Detection

Home Assistant Blueprint YAML

In this post, I'll guide you through creating a Home Assistant blueprint for a motion-activated light automation. This blueprint is designed to activate lights when motion is detected, with optional settings for brightness and color, and conditions based on time of day and/or an entity state.

Tools Used

  • Cursor (VS Code Port): For writing and editing YAML code.
  • GitHub: To host and share the blueprint with the community.

Blueprint Structure

Start by defining the blueprint's metadata, including its name, description, and domain:

blueprint:
  name: Motion-activated Light with Conditions and Optional Settings
  description: Turn on lights when motion is detected, with optional brightness and color settings, conditional entity state check, and sun position check.
  domain: automation

Input and Output Configuration

Define inputs to customize the automation, outputs (lights to turn on), and wait times to keep the lights on:

input:
    motion_sensors:
      name: Motion Sensors
      description: Select one or more motion sensors.
      selector:
        entity:
          domain: binary_sensor
          device_class: motion
          multiple: true
    light_targets:
      name: Lights
      description: Select one or more lights to control.
      selector:
        target:
          entity:
            domain: light
    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

Light Settings

Color and brightness configuration for selected lights:

 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:

Trigger Section

The trigger monitors motion sensor states:

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

This trigger activates when any selected motion sensor detects movement.

Variable Definition

Define the variables to be used within the automation script:

variables:
  use_custom_settings: !input use_custom_settings
  condition_entity: !input condition_entity
  condition_states: !input condition_states
  sun_condition: !input sun_condition

Conditions Section

Set conditions based on the mode (input_select) and sun position:

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

These conditions ensure that the automation only runs if the mode is correct and if it matches the specified time of day.

Automation Controls

Configuration regarding the process if conditions for trigger are met while automation is already executing:

mode: restart
max_exceeded: silent

Actions Section

Define actions to control the lights:

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

The actions turn on the lights with custom settings if enabled, wait for no motion, then turn off the lights after a delay.

Uploading to GitHub

After testing your blueprint in Home Assistant, upload it to GitHub. You can view my blueprint here.

Conclusion

Creating a Home Assistant blueprint enhances your smart home by automating tasks efficiently. Sharing your work on platforms like GitHub helps others automate their homes effectively. I created mine because there wasn't a solution out there to fit my needs, and the triggers, actions, and conditions were all useful for several automation activities.

Feel free to leave comments or questions below!

Previous Post Next Post