Turn on switches with motion based on 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 with Elevation
  domain: automation
  source_url: https://gist.github.com/Menethoran/219aff4827ef561065d28e53742485b7
  input:
    motion_entity:
      name: Motion Sensor
      selector:
        entity:
          domain: binary_sensor
          device_class: motion
    switch_target:
      name: Switch
      selector:
        target:
          entity:
            domain: switch
    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'

This is borrowed from

  source_url: https://gist.github.com/reinder83/8c5838c16af8f6ec3d7cb5258444b8b8t

Edited to work for switches

Invalid blueprint: Missing input definition for switch_target

Hi! i’m really interest you’re Blueprint but home assistant tell me unknown error when i’m copy the link on the importing blueprint, can you help me?

my apologies, I had a light target set instead of switch… Should work now…

sorry, had an error… try now

can you leave me the working url??

url is in the code…

  source_url: https://gist.github.com/Menethoran/219aff4827ef561065d28e53742485b7

But can we help me to add second condition to check swtch_target that it is OFF. ANd only run when it is OFF. Please help

the light does not switch off after the time has elapsed. thus useless

Confirm that light dont turn off after setting time.

What are you using to monitor motion? (and are you sure that your motion sensors arent being repeatedly tripped?)

I use binary sensor which is sensor motion from Integration alarm Satel with HA. Motion Sensors i am sure that is tripped only one. WHen i create manual automation it works ok but on your schema not working.

I use Philips Hue Outdoor Motion Sensor outside, movement is not triggered multiple times. otherwise after 20 minutes the light is still on …

Please try now… I had to mess with a bunch of the code to get it to work right… but does seem to be working correctly now… (testing in my house anyway)

Please try now

Hi
Is any chance to do when motion detect and turn on switch example for 5 min and under this 5min motion also again detect motion then time i reset and from this time again count 5 min ? Summary after again motion detect reset time and start time from beginning. Is any chance ?

This is already how the automation works… Detection of additional movement resets the timer.

Now blueprint works ok :slight_smile:
Next question…Is any chance to add as condition optio to set time betwen which hours this switch start and also when example other switch is not turn ON ?

Also i have a question. Can you help me how write this automation as this blueprint where timer is restart every detect motion ? Because if i know how i can write automation for me with setup time hours betwen it works and when other switch is off. Please and thanks.

at this time, you cant pick the hours that this will run… but ill look at adding it as a possible add-on. The sun elevation is a pretty good indicator of need if it’s lighting though… and effectively acts as a “time of day” requirement… if you want something turning on or off at times outside of the default, just move the sun elevation slider (default is 3 degrees above horizon… you can set this as high or low as you want to simulate times of day… 0 degrees is sunrise, any negative value is the sun below the horizon, any positive number is sun above the horizon… noon is dependant on where on earth you are (noon for me right now i think is 45 degrees)