Motion activated entity. Optional lux sensor, time of day, blocking boolean

Tags: #<Tag:0x00007fc4216ea8d8>

#Motion Activated Entity

All that is required for this blueprint is a motion sensor and an entity to be controlled.

  • Optional lux limit
  • Optional time of day
  • Optional blocking boolean (like don’t turn on when ‘asleep’ or ‘away’)
  • Optional off entity (in case controlling a script or scene and something else needs used to turn it off)
  • Optional off action (either off entity or off action can be used, not both. Will default to entity if both are defined)

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

blueprint:
  name: Turn on based on motion
  description: "Turn on a light, switch, scene, script or group based on motion detection,\
    \ and low light level.\n\
    \nRequired entities:\n  - Motion sensor (single sensor or group)\n  - Target entity\
    \ (light, switch, scene or script)\n\n\nOptional features:\n- You can set a cutoff\
    \ entity of which the value determines whether the illuminance level is low and\
    \ the automation needs to trigger. \n- You can define a blocking entity, which blocks\
    \ the automation from running when this entity's state is on. \n- You van define\
    \ a turn-off blocking entity, which blocks the entity from turning off after the\
    \ set delay. \n- Time limits can also be defined to limit the time before and after\
    \ the automation should trigger. \n- If you want the entity to turn off after a\
    \ certain amount of seconds, you can use the Wait Time input. \n- If you want another\
    \ entity than the target_entity to turn off after the delay, you can define a\
    \ separate Turn-off entity. \n- If you do not enable the optional entities the automation\
    \ will skip these conditions."
  domain: automation
  input:
    motion_sensor:
      name: Motion Sensor
      description: This sensor will trigger the turning on of the target entity.
      selector:
        entity:
          domain: binary_sensor
    target_entity:
      name: Target entity.
      description: The light, switch, scene to turn on (or script to run) when the
        automation is triggered.
      selector:
        entity: {}
    no_motion_wait:
      name: Turn off wait time (seconds)
      description: Time in seconds to leave the target entity on after last motion
        is detected.
      default: 0
      selector:
        number:
          min: 0
          max: 300
          unit_of_measurement: seconds
    illuminance_sensor:
      name: (OPTIONAL) Illuminance sensor
      description: This sensor will be used to determine the illumination.
      default:
      selector:
        entity:
          domain: sensor
          device_class: illuminance
    illuminance_cutoff:
      name: (OPTIONAL) Illuminance cutoff value
      description: This input_number will be used to compare to the current illumination
        to determine if it is low.
      default:
      selector:
        number:
          min: 0
          max: 100
          unit_of_measurement: lx
    blocker_entity:
      name: (OPTIONAL) Blocking entity
      description: If this entity's state is on, it will prevent the automation from
        running. E.g. sleepmode or away mode.
      default:
      selector:
        entity: {}
    time_limit_after:
      name: (OPTIONAL) Only run after time.
      description: Automation will only run when time is later than this input_datetime
        value.
      default:
      selector:
        entity:
          domain: input_datetime
    time_limit_before:
      name: (OPTIONAL) Only run before time.
      description: Automation will only run when time is earlier than this input_datetime
        value.
      default:
      selector:
        entity:
          domain: input_datetime
    turn_off_blocker_entity:
      name: (OPTIONAL) Turn-off Blocking entity
      description: If this entity's state is on, it will prevent the target entity
        from turning off after the set delay.
      default:
      selector:
        entity: {}
    target_off_entity:
      name: (OPTIONAL) Turn-off entity
      description: If defined, this entity will be turned off instead of the default
        target entity. This can be helpful when using target entities of type scene
        or script. 
        You cannot use Turn-off entity and Turn-off action, only one or the other. If both are defined, Turn-off entity will be used.
      default:
      selector:
        entity: {}
    target_off_action:
      name: (OPTIONAL) Turn-off action
      description: If defined, this action will be run instead of the turning off the default target entity. This can be helpful when using taget entities of type scene or script. 
        You cannot use Turn-off entity and Turn-off action, only one or the other. If both are defined, Turn-off entity will be used.
      default:
      selector:
        action: {}
  source_url: https://github.com/apollo1220/blueprints/blob/main/motion_activated_entity.yaml
mode: restart
max_exceeded: silent
variables:
  target_entity: !input 'target_entity'
  illuminance_currently: !input 'illuminance_sensor'
  illuminance_cutoff: !input 'illuminance_cutoff'
  blocker_entity: !input 'blocker_entity'
  time_limit_before: !input 'time_limit_before'
  time_limit_after: !input 'time_limit_after'
  no_motion_wait: !input 'no_motion_wait'
  turn_off_blocker_entity: !input 'turn_off_blocker_entity'
  target_off_entity: !input 'target_off_entity'
  target_off_action: !input 'target_off_action'
trigger:
  platform: state
  entity_id: !input 'motion_sensor'
  to: 'on'
condition:
- condition: template
  value_template: '{{ (states(target_entity) == ''on'') or (illuminance_currently
    == none) or (illuminance_cutoff == none) or (states[illuminance_currently].state
    | int < illuminance_cutoff | int) }}'
- condition: template
  value_template: '{{ (blocker_entity == none) or (states[blocker_entity].state ==
    ''off'') }}'
- condition: template
  value_template: '{% set current_time = now().strftime("%H:%M")  %}

    {% if time_limit_before != none and time_limit_after == none %} {{ states[time_limit_before].state
    > current_time }} {% elif time_limit_before == none and time_limit_after != none
    %} {{ states[time_limit_after].state < current_time }} {% elif time_limit_before
    != none and time_limit_after != none %} {% set before_limit_is_on_next_day = time_limit_after
    > time_limit_before  %} {% if not before_limit_is_on_next_day %} {{ (states[time_limit_after].state
    < current_time) and (states[time_limit_before].state > current_time) }} {% elif
    before_limit_is_on_next_day %} {{ (states[time_limit_before].state > current_time)
    or (states[time_limit_after].state < current_time) }} {% endif %} {% else %} true
    {% endif %}

    '
action:
- service: homeassistant.turn_on
  entity_id: !input 'target_entity'
- condition: template
  value_template: '{{ no_motion_wait != none }}'
- wait_for_trigger:
    platform: state
    entity_id: !input 'motion_sensor'
    from: 'on'
    to: 'off'
- delay:
    seconds: '{{ no_motion_wait | int }}'
- condition: template
  value_template: '{{ (turn_off_blocker_entity == none) or (states[turn_off_blocker_entity].state
    == ''off'') }}'
- choose:
  - conditions:
    - condition: template
      value_template: '{{ (target_off_entity != none) }}'
    sequence:
    - service: homeassistant.turn_off
      entity_id: !input 'target_off_entity'
  - conditions:
    - condition: template
      value_template: '{{ (target_off_action != none) }}'
    sequence: !input 'target_off_action'
  default:
  - service: homeassistant.turn_off
    entity_id: !input 'target_entity'

1 Like

I tried your blueprint and it works great. I then tried to configure the “run after” and “run before” times, because I want to have the automation only run at night. The Entity pull down menu says “No matching entities found”. I tried typing in the after and before times in the format 17:00:00, and also “17:00:00”, and the automation saved, but neither worked during a valid time period. Can you please clarify how the time entries work? Thanks

1 Like

You need to create an input.date_time helper (Configuration->Helpers, Add, Date Time). Then you pass that in to the blueprint.

    time_limit_before:
      selector:
        entity:
          **domain: input_datetime**
1 Like

Thanks for the pointer. That is a pretty cool way to be able to change the time from the Overview. My automation is now working great within the before and after parameters. One more quick question. I have the motion sensor activate a Scene, which has a set of lights that are set to different colors and brightnesses. Since HA doesn’t have a way to turn off Scenes, I have two automations running in parallel. One automation turns on the lights in the Scene (so the wait time will turn them off later), and the other turns on the Scene. It seems to be a bit of a brute force way to do it, but it is working. Is there a better way to do this, maybe in a Script?

I’ve never used scenes nor this blueprint, but maybe you can put the scene and all of the lights into a Group, and then just use one automation to trigger that group. I don’t know if turning ‘on’ a group containing a scene runs that scene, but if it does then this may work?

The way I would approach it would be to create a script which turns off the scene and then that could be set for the target_off_entity. Personally I never use scenes because scripts can do everything a scene can and more.

I’ve modified the blueprint adding an optional off action. This way you can do any action you need to when it should turn off. So you could individually turn things off, run a script, set a different scene or even turn things on :grin:

Thanks! That sounds exactly like what I was looking for. I will try it out later today.

1 Like

Nice! I love it! Now if only you could select dusk/dawn for times…

But I’m deploying your blueprint profusely here. It’s great as is.

Thanks! I’ll take a look at adding dusk/dawn options.

You can setup your input_datetime entity to reflect the current dusk and dawn values using set_datetime. Would that work for you?

I have the same question. Thanks!

Unfortunately, my lack of logic brain can’t figure out how to work an after a time in the evening AND before a time in the morning without that pesky midnight and starting over thingie.
I think I’ve got it figured out by using the SUN: binary attribute (above horizon/below horizon)

Yeah if you don’t need an offset, you can just use sun above horizon as the blocking entity. Then it will only run from sunset to sunrise.

I just tested the new turn-ff action and it is perfect. I am using your blueprint for multiple things, including for security lights that only turn on from a motion sensor after dark, like Dixey. I tried to use the blocking entity, but the only choice I see in the drop-down list is “sun.sun”. I did not see a way to create a “sun above the horizon” Blocking Entity using the Helper. It looks like limiting between Sunset and Sunrise (with offset) could be created by using an “Action Type, Condition”, like in a script. Is this something that has to be done using yaml using your Blueprint?

You can setup a binary sensor which converts above_horizon or below_horizon to make it easier to consume in the blueprint. I haven’t tested this out, so the syntax might be a little bit off, but the configuration would look something like this.

binary_sensor:
  - platform: template
    sensors:
      night_time:
        value_template: "{{ is_state('sun.sun', 'below_horizon') }}"

So it looks like from the Template Binary Sensor doc page that I would just add this to my configuration.yaml using a file editor? I haven’t directly edited any files yet, but have browsed the files and seen how the UI writes out the blueprints, scenes and automations, etc.

Yes. and it does work without any errors so just copy and paste it as is in your configuration.yaml.

I got it all up and working (and edited my first yaml file successfully). I modified the binary sensor slightly to have both a night_time and a day_time sensor. For my outdoor lights, I want the motion sensor to turn on the lights between 5 pm and 7 am, but only when it is dark (the sun below the horizon). When the blocking entity’s state is on, it prevents the automation from running. So the blocking entity needs to be day_time. (I use night_time so I am able to test the automation during the day.) My configuration looks like this:

binary_sensor:
  - platform: template
    sensors:
      night_time:
        friendly_name: "Night Time"
        value_template: "{{ is_state('sun.sun', 'below_horizon') }}"

      day_time:
        friendly_name: "Day Time"
        value_template: "{{ is_state('sun.sun', 'above_horizon') }}"

This blueprint is so flexible that I am able to configure exactly what I want my lights to look like with amazing precision. I have a script that activates a scene, so I can set both the brightness and the color of the lights I want to turn on, and have a transition time so they can turn on smoothly. The new turn-off action then allows me to turn all the lights off after the time-out, again with a nice smooth transition.

The only other wish list item would be to have a way to set the run before time, and the run after time, as an offset to sunset and sunrise. I have some rooms inside my house that get darker than others before sunset. So, for example, I would like to be able to have my family room lights turn on an hour before sunset. You mentioned that you could setup a set_datetime entity with sunset and sunrise, but that option is not in the Helper UI. So it looks like you would have to create a yaml configuraiton to do this. I found an example that looks like it might work in Getting sunset (with offset) as a time value

alias: update sunset minus one
trigger:
  platform: sun
  event: sunset
  offset: "-01:00:00"
action:
  service: input_datetime.set_value
  data_template:
    entity_id: input_number.sunset_minus_one_hour
    value: "{{ now().strftime('%H:%M:00') }}"

Would this approach work? Would I add this to configuration.yaml like the binary sensor?

Yes that approach can work. That is an automation which you should be able to setup in the UI. But the input_datetime would need to be setup in your configuration.yaml similar to the binary sensor. Like

input_datetime:
  sunset_offset: