Motion activate Light OR Switch, based on Daylight (Sun elevation)

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.

Or import this Blueprint by using the forum topic URL:

blueprint:
  name: Motion-activated Switch and or Light with Elevation
  domain: automation
  source_url: https://gist.github.com/Menethoran/74818bc5bad4f71a34e1e3bff2e37cdf
  input:
    motion_entity:
      name: Motion Sensor
      selector:
        entity:
          domain: binary_sensor
          device_class: motion
    switch_target:
      name: Switch
      selector:
        target:
          entity:
            domain: switch
      default: ""
    light_target:
      name: Light
      selector:
        target:
          entity:
            domain: light
      default: ""
    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: switch.turn_on
  target: !input 'switch_target'
- wait_for_trigger:
    platform: state
    entity_id: !input 'motion_entity'
    from: 'on'
    to: 'off'
- delay: !input 'no_motion_wait'
- service: switch.turn_off
  target: !input 'switch_target'
- 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

In my case is not possible to use the automation.
When I save the automation the code is set in automation.yaml but the automation is not in the list of the automations.

I have the same issue. Automation is not showing up

You need to save it in the blueprints/automation/homeassistant folder and the file should have a unique name with a trailing .yaml

I tried to import using the import function of HA. Now I am getting this error:

Blueprint Motion-activated Switch and or Light with Elevation generated invalid automation with inputs OrderedDict([('motion_entity', 'binary_sensor.presence_78'), ('light_target', OrderedDict([('entity_id', 'light.flur_eg')]))]): expected a dictionary for dictionary value @ data['action'][0]['target']. Got None
How do I get rid of this message?

+1. Pls help…?

2021-03-05 08:39:43 ERROR (MainThread) [homeassistant.components.automation] Blueprint Motion-activated Switch and or Light with Elevation generated invalid automation with inputs OrderedDict([(‘motion_entity’, ‘binary_sensor.motion_front_yard’), (‘light_target’, OrderedDict([(‘entity_id’, [‘light.front_yard’, ‘light.front_porch’])])), (‘no_motion_wait’, 180), (‘below_elevation’, 3)]): expected a dictionary for dictionary value @ data[‘action’][0][‘target’]. Got None

HA created an entry in automation.yaml. I deleted that and the blueprint. Error is gone now.

…but the automation does not work…?

No, automation was not working. That’s why I deleted everything.

I had the same issue as people mention above but figured it was probably due to not using and configuring the switch. I removed the switch section and the switch actions and now the automation appears.

The blueprint now looks as follows:

blueprint:
  name: Motion-activated Switch and or Light with Elevation
  domain: automation
  source_url: https://community.home-assistant.io/t/motion-activate-light-or-switch-based-on-daylight-sun-elevation/265010
  input:
    motion_entity:
      name: Motion Sensor
      selector:
        entity:
          domain: binary_sensor
          device_class: motion
    light_target:
      name: Light
      selector:
        target:
          entity:
            domain: light
      default: ''
    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:
- wait_for_trigger:
    platform: state
    entity_id: !input 'motion_entity'
    from: 'on'
    to: 'off'
- delay: !input 'no_motion_wait'
- 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'

The same. Didn’t appear in the automation list, it adds to automation.yaml
Doesn’t work. Removed