Motion Activated Light, but with conditions

The motion activated light blueprint is nearly perfect, it’s just missing conditions: It should only activate when the sun has set and people are in the house.

Without writing this myself from scratch, does anyone have an updated blueprint to share?

(PS: Kinda mind blowing that this basic thing is not included in the default…)

Why don’t you add the conditions yourself?

condition:
  - condition: numeric_state
    entity_id: zone.home
    above: 0
  - condition: numeric_state
    entity_id: sun.sun
    attribute: elevation
    above: 0

I personally prefer to use a illuminance sensor instead of Sun’s position, so it will work in a cloudy dark day.

Also, I have a helper (toggle) which will be on when the apartment is empty (controlled by another automation) and then I use this as a “override entity” in other automation.
I have more of those “override entities” (usually toggle helpers), lime “Party mode”, “Travel mode”, “Maintenance mode”, etc.) used to prevent the different automation to run.

Maybe I misunderstand but when I use the blueprint I do not see an option to add conditions myself.
Yes, I could create this for myself entirely from scratch but then I have to deal with timeout etc (the included blueprint has the option to set the “Wait Time”. Hence I would like to use a blueprint.

You haven’t informed which blueprint you are using… Maybe you should publish this in the Blueprint thread.
There are dozens of blueprints related to control lights with a motion sensor.

By the way, you can always edit a blueprint you are using. Just find the file under /config/blueprints and do the changes you want. But please don’t edit it if you are not sure about what you are doing, as you can have undesired results.

Hi Edward, I am using the standard one from HA (homeassistant/motion_light.yaml).

When I create an automation from it, it looks like:

I have looked into this file but I have no clue what I need to add in order to add the conditions.

Hence I was hoping someone did this already (I think this is a pretty obvious desired extension)

Actually I just added your lines to the blueprint yaml file.

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/52307708c843b947a2d631f2fe7ddaa8bd9a90d7/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

# 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"

## BEGIN ADDED BY ME
condition:
  - condition: numeric_state
    entity_id: zone.home
    above: 0
  - condition: numeric_state
    entity_id: sun.sun
    attribute: elevation
    above: 0
## END ADDED BY ME

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

but now it does not turn on at all (although sun elevation is -52.88 and I am at home…)

EDIT: I cross checked the conditions with the GUI (by creating a dummy automation) and they evaluate to true. Strange…

My fault… This condition will only run when the Sun is above the horizon. It should be bellow.

1 Like
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/52307708c843b947a2d631f2fe7ddaa8bd9a90d7/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

# 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"

## BEGIN ADDED BY ME
condition:
  - condition: numeric_state
    entity_id: zone.home
    above: 0
  - condition: numeric_state
    entity_id: sun.sun
    attribute: elevation
    below: 0
## END ADDED BY ME

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
2 Likes

I would suggest you to do this in another file, so you keep the original blueprint intact just in case…

Hold on, I will create this new blueprint for you. Give me 5min.

Meanwhile, revert back your blueprint to the original state (removing the conditions).

1 Like

I tried it and I think it worked :slight_smile: Yaaay thank you!!

But of course, a separate blueprint would be EVEN better

1 Like

Just click this button to import it as a new Blueprint:

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

blueprint:
  name: Motion-activated Light (only when sun is up and someone at home)
  description: Turn on a light when motion is detected.
  domain: automation
  source_url: https://community.home-assistant.io/t/motion-activated-light-but-with-conditions/466228/12
  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
    sun_entity:
      name: Sun entity
      description: This is normally "sun.sun".
      default: sun.sun
      selector:
        entity:
          domain: sun
    home_entity:
      name: Home entity
      description: This is typically "zone.home".
      default: zone.home
      selector:
        entity:
          domain: zone

# 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 home_entity
    above: 0
  - condition: numeric_state
    entity_id: !input sun_entity
    attribute: elevation
    below: 0

action:
  - alias: "Turn on the light"
    service: light.turn_on
    target: !input light_target
  - alias: "Wait until there is no motion from device"
    wait_for_trigger:
      platform: state
      entity_id: !input motion_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
1 Like

If you want to go for a really detailled automation with lots of conditions, you can have a look here:

But maybe that’s an overkill for your request…

1 Like

@dydx, have you succeeded on importing (and using) the new Blueprint?

Thank you for creating this. Amazing!

I am actually struggling with the import button :wink: Weirdly I get the error “No valid blueprint found in the topic. Blueprint syntax blocks need to be marked as YAML or no syntax.”

Having said that, I was just trying around because I have never used this button and find it nice to import with a simple click … but I can just copy it manually which I’ll do now

Works like a charm! Thank you very much!!
(And this also helps me a lot as a starting point for adjusting more things)

1 Like

Thanks for the reference. Yes I think for now that’s a bit too overkill.

Good to hear that.

But please be aware that with this additional complexity you can have additional unexpected results… I mean, if your phone gets ou of battery the system might interpret you are not at home and then stop your motion based lights.

The way I solved this is to use a blueprint automation for motion detection and then two additional automations to enable and disable the automation (call service automation.turn_off/on) at sunrise and sunset.

1 Like

Since Edward created the solution for this, HA has added a condition selector.
Here is exactly what Edward wrote, but instead of locked in conditions, I inserted the condition selector code, so the user can write their own condition when they set up their automation. Or just leave it blank, and it will run without conditions…

blueprint:
  name: Motion-activated Light (With condition selector that can be set by user at automation build.)
  description: Turn on a light when motion is detected.
  domain: automation
  input:
    motion_entity:
      name: Motion Sensor
      selector:
        entity:
          filter:
            domain: binary_sensor
            device_class: motion
    light_target:
      name: Light
      selector:
        target:
          entity:
            domain: light
    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:
    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
    sun_entity:
      name: Sun entity
      description: This is normally "sun.sun".
      default: sun.sun
      selector:
        entity:
          filter:
            domain: sun
    home_entity:
      name: Home entity
      description: This is typically "zone.home".
      default: zone.home
      selector:
        entity:
          filter:
            domain: zone

# 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"

  # If you see 'Missing property 'conditions' it is a false error.
  # See https://github.com/keesschollaart81/vscode-home-assistant/issues/2786
condition:
  - alias: User pick
    condition: !input additional_conditions

action:
  - alias: "Turn on the light"
    service: light.turn_on
    target: !input light_target
  - alias: "Wait until there is no motion from device"
    wait_for_trigger:
      platform: state
      entity_id: !input motion_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
3 Likes