Motion detect lights with solar elevation check

Tags: #<Tag:0x00007fc40f1be128> #<Tag:0x00007fc40f1bdfc0> #<Tag:0x00007fc40f1bde30>

I have added an sun elevation check to the motion detect blueprint, so the lights only go on when it’s dark outside.

Gist URL: https://gist.github.com/reinder83/8c5838c16af8f6ec3d7cb5258444b8b8

Get started

Click the badge to import this Blueprint: (needs Home Assistant Core 2021.3 or higher)

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

blueprint:
  name: Motion-activated Light with Elevation
  domain: automation
  source_url: https://gist.github.com/reinder83/8c5838c16af8f6ec3d7cb5258444b8b8
  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 wait until the light should be turned off.
      default: 120
      selector:
        number:
          min: 0.0
          max: 3600.0
          unit_of_measurement: seconds
          step: 1.0
          mode: slider
    below_elevation:
      name: Below sun elevation
      description: Solar elevation. This is the angle between the sun and the horizon.
        Negative values mean the sun is below the horizon.
      default: 3
      selector:
        number:
          min: -90.0
          max: 90.0
          unit_of_measurement: degrees
          step: 1.0
          mode: slider
mode: restart
max_exceeded: silent
trigger:
  platform: state
  entity_id: !input 'motion_entity'
  from: 'off'
  to: 'on'
variables:
  below_elevation: !input 'below_elevation'
condition:
- '{{ state_attr(''sun.sun'',''elevation'') <= (below_elevation | float)}}'
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'
10 Likes

Awesome!
I have an upgrade suggestion that will be great (for me at least :innocent:).
At night I use a red light if I wake up because it has no blue so it’s easier to stay sleepy. It would be nice to be able to set a color (or to add data) in the blueprint.

Either way, thanks!

Ah, my light is a simple on/off, but feel free to use this blueprint to create your own :slight_smile:

sorry for my mix up — so if i have elevation at “0” this should turn the light on when the sun goes below horizon, right ?

Yup, I have it set myself at 3, because it starts getting dark once it’s below 3

1 Like

for me the blueprint is not working with this in the logs.

Logger: homeassistant.helpers.condition
Source: helpers/condition.py:458
First occurred: 5:20:40 PM (8 occurrences)
Last logged: 6:27:11 PM

Error during template condition: TypeError: '<=' not supported between instances of 'float' and 'str'

screenshot-homeassistant-local-8123-config-logs-1608486003105

1 Like

How did you add it, when I add through the UI it’s just fine, make sure it’s setup like this in the automation:

- id: '1608486330207'
  alias: Motion-activated Light with Elevation
  description: ''
  use_blueprint:
    path: reinder83/motion_light_with_elevation.yaml
    input:
      below_elevation: 3
      motion_entity: binary_sensor.pir_driveway
      light_target:
        entity_id: light.driveway_light

below_elevation should be numeric

- id: '1608481223494'
  alias: Prullenbak
  description: ''
  use_blueprint:
    path: reinder83/motion-detect-lights-with-elevation-check.yaml
    input:
      below_elevation: '3'
      motion_entity: binary_sensor.kitchen_door_sensor
      light_target:
        entity_id:
        - light.patio_led_1
        - light.patio_led_2

the only thing i’ve changed is the device_class to opening to work with my door sensor

I have done an update the to blueprint, could you try again?

1 Like

Now it’s working, thnx for fixing :+1:

Is it possible to use multiple binary sensors for motion entity?

You can only select one motion sensor for this blueprint, but what you could do the following:
create an template binary sensor, I just tested with the sensor below and that seems to work:

- platform: template
  sensors:
    all_motion_sensors:
      device_class: motion
      friendly_name: "All motion sensors"
      value_template: >-
        {{ is_state('binary_sensor.hall_motion', 'on') or is_state('binary_sensor.pir_driveway', 'on') }}

Put that under binary_sensor: in your configuration.yaml

Thank you. I setup the binary sensor template as suggested.

Its working great. Thanks again.

I don’t use smart bulb but using a switch but it does not give me an option to select a switch. Please help

You can use this to make a light out of a switch: https://www.home-assistant.io/integrations/light.switch

It works thanks heaps

One thing I am trying to understand with blueprints is why for some variables we pass to the blueprint we also define under variables?

Is this to just reduce having to specify !input if they are used multiple times in the blueprint?

Because you need a variable in the template, the !input wont work there

Yes, that makes sense but I see many who just set variables for all things passed to the blueprint.