Motion-activated Light Extended

MOTION ACTIVATE LIGHT EXTENDED

This is blueprint for Home Assistant intended to help to create automations to turn on/off lights as a reaction of a movement sensor.
This is an evolution of standard motion activated light.

In this blueprint, I’ve supported some important additional features intended to improve the flexibility of the component.

  • Ability to activate enable/disable the motion activation light based on sunset and sunlight

  • Ability to override the sunset / sunrise conditional activation based on the weather

  • Ability to define a timeout after that the light turn off also if movement sensor is already on (useful to situations in which sensors sometimes fails to communicate deactivation due to connectivity problems).

All this options can be activated or not so you can freely combine them. For use the blueprint in its more general form you need one or more entities of this domains/class:

  • binary_sensor / motion
  • light
  • weather

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

blueprint:
  name: "Motion-activated Light extended"
  description: "Turn on a light when a movement occurs"
  domain: automation
  source_url: https://github.com/giannisigalotti/HomeAssistant/blob/main/blueprints/motion-activated-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: "The time to wait before turning off the light after no movement is detected."
      default: 120
      selector:
        number:
          min: 0
          max: 3600
          unit_of_measurement: seconds
    no_sunset_handling:
      name: "Disable day/night management?"
      description: "When you turn this option to off, you can define how many hour before sunset and after sunshine the automation turn on the light"
      selector:
        boolean:
      default: false
    wheater_handling:
      name: "Enable meteo managment?"
      description: "Does it also take weather into account to enable/disable motion sensors (use the time range only when it's sunny and disable otherwise)?"
      selector:
        boolean:
      default: false
    wheater_entity:
      name: "Weather status sensor"
      description: "This sensor is used only with the Meteo managment option to determine if the weather is sunny or not (useful if you are in a room usually dark in case of bad weather condition"
      selector:
        entity:
          domain: 
            - weather
          multiple: false 
      default: "weather.home"
    sunset_start_hh:
      name: "Pre-sunset hours activation"
      description: "How many hours before sunset does the management activate (negative numbers indicate a delay)?"
      selector:
        number:
          min: 0
          max: 12
          unit_of_measurement: hours
      default: 0
    sunset_start_mm:
      name: "Pre-sunset minutes activation"
      description: "How many minutes before sunset does the management activate (negative numbers indicate a delay)?"
      selector:
        number:
          min: 0
          max: 59
          unit_of_measurement: minutes  
      default: 0
    sunrise_start_hh:
      name: "Post-sunrise hours activation"
      description: "How many hours after sunrise does the management deactivate (negative numbers indicate an advance)?"
      selector:
        number:
          min: 0
          max: 12
          unit_of_measurement: hours
      default: 0
    sunrise_start_mm:
      name: "Post-sunrise minutes activation"
      description: "How many minutes after sunrise does the management deactivate (negative numbers indicate an advance)?"
      selector:
        number:
          min: 0
          max: 59
          unit_of_measurement: minutes   
      default: 0
    timeout:
      name: "Deactivation timeout?"
      description: "After how many minutes does the switch turn off automatically (-1 if no timeout)"
      selector:
        number:
          min: -1
          max: 60
          unit_of_measurement: minutes  
      default: 5
# 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"
variables:
  # Make input my_input available as a script level variable
  dusk_start_hour: !input sunset_start_hh 
  dusk_start_minute: !input sunset_start_mm
  rising_start_hour: !input sunrise_start_hh 
  rising_start_minute: !input sunrise_start_mm
  use_weather: !input wheater_handling
  sun_rising: >  
    {% if (states.sun.sun.state == "above_horizon") %}
        {{ states.sun.sun.last_changed }}
    {%- else -%}    
        {{ states.sun.sun.attributes.next_rising }}    
    {%- endif %}
  sun_dusk: >
    {% if (states.sun.sun.state == "above_horizon") %}
        {{ states.sun.sun.attributes.next_dusk }}
    {%- else -%}    
        {{ states.sun.sun.last_changed }}
    {%- endif %}    
  wheater_sensor: !input wheater_entity  
  no_sunny_wheater: >
    {{ states(wheater_sensor) != 'sunny' and use_weather }}
condition:
  condition: or
  conditions:
      - condition: template
        value_template: !input no_sunset_handling 
      - condition: template
        value_template: "{{ no_sunny_wheater }}"
      - condition: and
        conditions:
          -  "{{ as_timestamp(now()) >= ((as_timestamp(sun_dusk) - dusk_start_hour*3600 - dusk_start_minute*60) )}}"
          -  "{{ as_timestamp(now()) < ((as_timestamp(sun_rising) + rising_start_hour*3600 + rising_start_minute*60) )}}"
action:
  - service: light.turn_on
    target: !input light_target
  - wait_for_trigger:
      platform: state
      entity_id: !input motion_entity
      from: "on"
      to: "off"
    timeout:
      hours: 0
      minutes: !input timeout
      seconds: 0
      milliseconds: 0
  - delay: !input no_motion_wait
  - service: light.turn_off
    target: !input light_target
1 Like

Looks good, but in my experience someone or several someones will ask to not trigger for different and random reasons. My suggestion is to add a user entered conditional. I put them in many of my blueprints and it should provide users the ability to skip days or change the hours or have an override if they are interacting with something else or the infinite number of things users can think of.

You can lift the code from one of by blueprints linked below if that’s something you think is beneficial…

Thanks for contributing to the community!

2 Likes

@bladerunner70 Gianni,

This is good, but I find that the CCTV motion detection sees that change caused by the light being turned off, as being enough to say that motion has been detected.
This then causes the light to turn off, then immediately turn back on… i.e. an endless loop of the light going on, off, on etc.

Is there any chance you could add a pause/delay so that after turning off, it won’t trigger again within x seconds? Or alternatively maybe a condition that the light hasn’t just been turned off in the last x seconds?

Thanks.

@bladerunner70 thank you for the blueprint.
One bug i see is that the weather entity used in ‘no_sunny_wheater’ variable is not the same as ‘wheater_entity’.
So unless someone has pirateweather as their weather, that part will not work.
Thank you.

Thank you for signaling issue. I’ve found also another issue and committed in repo but forgot to signal here and to change the url of the blueprint.
I’ll try to fix also your problem ASAP

I’ve fixed the issue. Thanks for signaling.

Excuse me for the delay. I’m not sure to understand the problem. Seems to me that you motion sensor have a very short duration time. Is it right? usually, motion sensors change state when a motion is found and then keep that state since motion finishes. But based on your description, seems to me that in your case the motion sensor detect movement and turn on, but turns off imediatly… am I right?