Turn on a switch when motion is detected

I changed the default motion sensor blueprint to match any switch!
Turn on switch when motion is detected. Leave it on for X seconds after motion is detected.

Edit 2020-12-17: Added time conditions, hope it works.

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 Switch
  description: Turn on a switch when motion is detected.
  domain: automation
  input:
    motion_entity:
      name: Motion Sensor
      selector:
        entity:
          domain: binary_sensor
          device_class: motion
    lightsensor_entity:
      name: Illuminance Sensor
      selector:
        entity:
          domain: sensor
          device_class: illuminance
    illuminace_level:
      name: Max Illuminance
      description: Maximal immuminance level in lux. If illuminance is higher, light
        will not be enabled
      default: 300
      selector:
        number:
          min: 0.0
          max: 5000.0
          unit_of_measurement: lux
          mode: slider
          step: 1.0      
    switch_target:
      name: Switch
      selector:
        target:
          entity:
            domain: switch
    time_from:
      name: Active from
      description: A time input which defines the time from which motion is detected
      selector:
        time: {}
    time_to:
      name: Active to
      description: A time input which defines the time to which motion is detected
      selector:
        time: {}        
    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
mode: restart
max_exceeded: silent
trigger:
- platform: state
  entity_id: !input 'motion_entity'
  from: "off"
  to: "on"
condition:
- condition: and
  conditions:
    - condition: time
      after: !input 'time_from'
      before: !input 'time_to'
    - condition: numeric_state
      entity_id: !input 'lightsensor_entity'
      below: !input 'illuminace_level'
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'
3 Likes

Just curious what kind of switches you’re using this blueprint for?

I can’t speak for OP, but I have light switches throughout my house that only show up as switches in HA.

You can use the light.switch component to make them into light entities in that case.

I was not aware of that component, thank you very much!

I wanted to use it for all my 30+ wifi tuya switches that don’t show up as “light”.

I was not aware of the “light.switch component”, but now you can use both :wink:

Thanks. maybe add a sun Condition

2 Likes

Yes that’s a great idea

Perhaps consider adding a time condition, so the trigger only happens during specific hours of the day?

i.e. If I use this for light switches, I don’t want this executing during “daylight”, so I might want to limit to “dark” hours or between sunset/sunrise etc.

2 Likes

I have like 20 switches and if I used that I would have a lot of duplicate entities.

I created a config with that, but also struggle with the missing condition feature like sun state or time. Even the room has enough light, it always triggers the lights. I also use tuya wifi switches and can’t use them here, only my lightify via services or Philips via device config are selectable. I have some lights controlled with power plugs, they also won’t appear to select.

Would be cool if you could extend it to define/use dependencies and more devices like power plugs and wall switches.

1 Like

Did someone manage to have this with conditions?

I don’t believe more features will be added. It’s just an exact copy of the original, but it uses switch instead of light. Try asking in #configuration:blueprints and see if anyone will help you.

I’m the first to admit that I’m not a programmer, I try to learn but don’t have much time to spare.
I tried with this, tell me if it don’t work!

blueprint:
  name: Motion-activated Switch
  description: Turn on a switch when motion is detected.
  domain: automation
  input:
    motion_entity:
      name: Motion Sensor
      selector:
        entity:
          domain: binary_sensor
          device_class: motion
    lightsensor_entity:
      name: Illuminance Sensor
      selector:
        entity:
          domain: sensor
          device_class: illuminance
    illuminace_level:
      name: Max Illuminance
      description: Maximal immuminance level in lux. If illuminance is higher, light
        will not be enabled
      default: 300
      selector:
        number:
          min: 0.0
          max: 5000.0
          unit_of_measurement: lux
          mode: slider
          step: 1.0      
    switch_target:
      name: Switch
      selector:
        target:
          entity:
            domain: switch
    time_from:
      name: Active from
      description: A time input which defines the time from which motion is detected
      selector:
        time: {}
    time_to:
      name: Active to
      description: A time input which defines the time to which motion is detected
      selector:
        time: {}        
    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
mode: restart
max_exceeded: silent
trigger:
- platform: state
  entity_id: !input 'motion_entity'
  from: "off"
  to: "on"
condition:
- condition: and
  conditions:
    - condition: time
      after: !input 'time_from'
      before: !input 'time_to'
    - condition: numeric_state
      entity_id: !input 'lightsensor_entity'
      below: !input 'illuminace_level'
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'

Thanks, I’m going to try it

Oh man… Perfecto!:wink:

Was working but am getting an error with latest version of HA system

unknown tag !<!input> at line 62, column 36:
entity_id: !input ‘motion_entity’

Any ideas?

You’re trying to add the blueprint as an automation.

Not yet. I just imported the blueprint and went to the file editor and opened this blueprint and the error shows up instantly. Have not created the automation yet.