Create own version of blueprint "motion-activated light"?

Yes, and it’s quite tricky (or elegant). The blueprint uses mode “restart”, meaning everytime motion is triggered the delay to turn off is cancelled and automation’s action sequence starts again. This works because it doesn’t harm to turn an already “on” light on again.

mode: restart
max_exceeded: silent
1 Like

but wouldn’t I need two automations if I’m trying to recreate the blueprint? One for turning on and one for off?

No,

check the blueprint’s automation part at Github (or your local drive).

It triggers only if motion is detected (off to on), turns the lights on and starts the delay to turn them off. If during that time motion is detected again, the script part is started again, effectively restarting the delay again.

Only thing that isn’t matched by this is continuous motion (trigger alwas on).

Okay, I think I’ll have to do a lot more reading on blueprints vs. automations, because I don’t understand the sequence of the blueprint. Thank you for the assist!

Actually it’s quite easy:

  • lines 31 to 50 is the actual automation, but instead of fixed values or entities, it uses variables (declared by !input)
  • lines 1 to 30 is the actual blueprint “overhead”, some metadata and the declaration of the supported !input variables.

When you create an automation based on a blueprint, that automation references the blueprint and defines values for the given variables. If you change the blueprint afterwards (and don’t break it), all automations created from that blueprint will be using the new logic.

so I could just create an automation based on the blueprint and afterwards insert the lux-criteria in the created automation?

No. An automation created from a blueprint does not support trigger, condition or action sequence. It looks like this (automation.yaml):

- id: 'xzy'
  alias: My Alias
  description: ''
  use_blueprint:
    path: m0wlheld/control_nightlight.yaml
    input:
      transition_var: '5'
      darkness_entity: input_boolean.outdoor_darkness
      cover_entity: cover.my_cover_entity
      light_entity: light.my_light

So it’s just a link to the blueprint and the values for the variables.

Show me the ‘lux’ condition of your automation and I will show you the blueprint to create.

Thanks - my lux condition is just:

condition:
  - condition: numeric_state
    entity_id: sensor.light
    below: '200'

Copy this to config/blueprints/automation/[custom_subdir]/[custom filename].yaml and reload.

blueprint:
  name: Motion-activated Light
  description: Turn on a light when motion is detected.
  domain: automation
  source_url: https://github.com/home-assistant/core/blob/ea47e5d8af49de4cdaf2908b010207a2536e1238/homeassistant/components/automation/blueprints/motion_light.yaml
  input:
    motion_entity:
      name: Motion Sensor
      selector:
        entity:
          domain: binary_sensor
          device_class: motion
    light_target:
      name: Light
      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_sensor:
      name: Light sensor
      description: Light sensor to read from
      selector:
        target:
          entity:
            domain: sensor
    light_threshold:
      name: Light threshold 
      description: Light sensor‘s lower threshold 
      default: 200
      selector:
        number:
          min: 0
          max: 255

# If motion is detected within the delay,
# we restart the script.
mode: restart
max_exceeded: silent

trigger:
  platform: state
  entity_id: !input motion_entity
  from: "off"
  to: "on"

condition:
  - condition: numeric_state
    entity_id: !input light_sensor
    below: !input light_threshold

action:
  - service: light.turn_on
    target: !input light_target
  - wait_for_trigger:
      platform: state
      entity_id: !input motion_entity
      from: "on"
      to: "off"
  - delay: !input no_motion_wait
  - service: light.turn_off
    target: !input light_target
1 Like

Thought it worked, but apparently not. Getting this error message:

unknown tag !<!input> at line 50, column 34:
      entity_id: !input motion_entity

Did you put the file into the correct location? There are no errors in it, with respect to being a blueprint, but it will fail miserable if being used as an automation’s source …

I created a new folder and placed it there. Still getting an unknown tag error :thinking:

I copied the code 1:1 from my post into a file at /config/blueprints/automation/m0wlheld/motion_lights.yaml and is does not trigger an error. Also created an automation from that blueprint (yet, didn’t save it), and it appeared okay.

That‘s all I can say …

Looks like the issue is somewhere in my HA. Current error message points to my configuration.yaml file, stating that it “failed to load blueprint”.

Probably have to do some forensics to try and find the issue.

Edit: No longer getting an error message in the log, but still no luck in creating the lux-blueprint. The blueprint shows up, I can enter the data and then press create automation without issue, but it doesn’t result in any automation in the automation list. The weird thing is that when I go to automations.yaml, I can see the just created automation.

Don’t really understand where the issue might be. Using the original blueprint works fine.

Hello Everyone,

I want to add a condition to this BluePrint. I’ve tried multiple times but it doesn’t work. I just want to check if the TV is on. If I’m watching TV it shouldn’t turn the lights on/off.

How can I add the this condition to BluePrint? Can somebody help me about this?

Regards

Try this…

blueprint:
  name: Presence-activated Light
  description: Turn on a light when presence is detected.
  homeassistant:
    min_version: 2023.8.0
  domain: automation
  source_url: https://community.home-assistant.io/t/presence-sensor-light-automation/564590
  input:
    presence_entity:
      name: Presence Sensor
      description: Presence sensor device
      selector:
        entity:
          filter:
            domain:
            - binary_sensor
            device_class:
            - occupancy
          multiple: false
    light_target:
      name: Light
      description: Light device
      selector:
        target:
          entity:
          - domain:
            - light
    bright_percentage:
      name: Bright Percentage
      description: Brightness level that the light will turn into once is turned on
      default: 100
      selector:
        number:
          min: 0.0
          max: 100.0
          unit_of_measurement: percentage
          mode: slider
          step: 1.0
    no_motion_wait:
      name: Wait time
      description: Time to leave the light on after last motion is detected.
      default: 0
      selector:
        number:
          min: 0.0
          max: 3600.0
          unit_of_measurement: seconds
          mode: slider
          step: 1.0
    off_transition:
      name: Transition time
      description: Time in seconds of transition turning off
      default: 0
      selector:
        number:
          min: 0.0
          max: 60.0
          unit_of_measurement: seconds
          mode: slider
          step: 1.0
    light_sensor:
      name: Light sensor
      description: Light sensor to read from
      default: []
      selector:
        entity:
          filter:
            domain:
            - sensor
            device_class:
            - illuminance
          multiple: false
    light_threshold:
      name: Light threshold
      description: Light sensor‘s lower threshold
      default: 200
      selector:
        number:
          min: 0.0
          max: 255.0
          mode: slider
          step: 1.0
    additional_conditions:
      name: Additional conditions
      description: |
        Extra conditions you may want to add to this automation 
        (Example: Home occupied, TV on, etc)
      default: []
      selector:
        condition:
trigger:
  platform: state
  entity_id: !input presence_entity
  from: 'off'
  to: 'on'
condition:
- condition: numeric_state
  entity_id: !input light_sensor
  below: !input light_threshold
- condition: !input additional_conditions

action:
- alias: Turn on the light
  service: light.turn_on
  target: !input light_target
  data:
    brightness_pct: !input bright_percentage
- alias: Wait until there is no motion from device
  wait_for_trigger:
    platform: state
    entity_id: !input presence_entity
    from: 'on'
    to: 'off'
- alias: Wait the number of seconds that has been set
  delay: !input no_motion_wait
- alias: Turn off the light
  service: light.turn_off
  target: !input light_target
  data:
    transition: !input off_transition

mode: restart
max_exceeded: silent


Fixed the selector filters to the new format, moved mode to the end because sometimes, oddly, I have seen it cause issues located other places, and add a condition selector so that you can add any condition you like. Requires HA version 2023.8.0 or newer to handle the condition selector.

If this solves your problem please consider hitting solution checkbox here.

2 Likes

Thank you once again!

I can’t see any solution checkbox?

Regards.

1 Like

Thanks for posting this snippet! I had been hunting for a way to add a condition to an existing blueprint, and this was more than what I had hoped to find!

I made a copy of the “motion activated light” default HA blueprint (motion_light.yaml) and called it a “motion activated switch” blueprint (motion_switch.yaml), and then added this conditional component.

My use case: I have an overhead light which is controlled by a Z-Wave enabled wall switch in our laundry room. The laundry room happens to be attached to our garage as well, so it has no windows nor other light source and is always dark if the light isn’t on. Since we often enter the laundry room from within the house (with clothes to wash) and/or the garage with hands full (groceries, etc.), I put a motion sensor in the laundry room to assist with turning on the lights (in addition to the garage door opening triggering the light to turn on). What I didn’t want to happen was that IF the laundry room light switch was already ON for whatever reason, I wanted it to stay on and didn’t want the motion sensor blueprint routine to turn OFF the light after the timer expired. So, with the additional_conditions block, I added a check to see if the light was not already on, then the blueprint could trigger turning the light on and enable the countdown timer within the blueprint to turn it off later.
I have another automation that runs periodically to check for recent motion and turn the light off at night.

Thanks again for putting this out here!

Hi Todd,

I put that in most of my blueprints now, and it GREATLY cuts down on people asking for small tweaks for this or that. It provides options.

1 Like

It absolutely provides options that way! Well done, sir. Thanks!