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

Thanks for the nice Elevation setup, but unfortuanally the actions did not work. So I spend some time fixing them. And maybe for some who still looking for Elevation + Motion + Timer. For the newer ones like me you can paste these in the Blueprint folder with the Home Assistant editor.

My final working result is:

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
          multiple: false
    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
    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: 1
      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

Guys has this been solved yet. I use save this blueprint but does not show up under my automation list however it shows in my automation.ymal file.
Thanks

Did you paste this file in the right location ? It should be like that: /config/blueprints/automation/ → YourFile

And in Home Assistant it will show in the Blueprint section.

1 Like

Yes it does show up in my Blueprints BUT when I have finished creating the Automation and I click save, the automation does not appear in my where all my Automations are ? That automation is missing ?

Mmmmhhh I just deleted mine for testing and it worked normal. But is it still working? And there should be a add to your automations file too. (Code below) Or have you tried that with a other working Blueprint from the Home Assistant forum? Maybe theres a error in your confinguration.

id: '1659296007324'
  alias: Motion-activated Switch and or Light with Elevation
  description: ''
  use_blueprint:
    path: homeassistant/Lichtautomatik_Flur.yaml
    input:
      motion_entity: binary_sensor.0x00158d0008367c92_occupancy
      light_target:
        entity_id: light.licht_flur
      no_motion_wait: 5
      below_elevation: 5

When you paste the File dont forget the ending of the file “.yaml” :smiley:

Hey I’d love to use this, it’s perfect for my usecase.

However, as others have mentioned – there’s something that’s preventing this automation from being created. I’ve gone through the config steps, and when I hit save it simply doesn’t save or show up in my automations list.

I tried deleting and reinstalling the blueprint, no dice. Other blueprints save & work as expected.

Lmk if I can share anything to help debug.

Edit: after some digging and testing on my end, afaik the script as originally posted is simply broken due to the switch functionality.

I updated the blueprint that OP posted in a follow-up comment (requires HA file editor, and a basic knowledge of messing around with your core .yaml files), and that seemed to work with the notable change that it no longer includes ‘switch,’ just ‘light’ with the sun functionality.

For my situation with a smartplug I was hoping for switch, I ultimately just updated the smartplug switch entity to light and proceedes to setup the automation in my UI. It finally showed up in my automations list, yay.

i am getting error:

Message malformed: expected a dictionary for dictionary value @ data[‘action’][4][‘target’]