Conditions based on how long the motion sensor has been 'away'

I’m trying to figure out how to trigger my lights to turn on when motion is detected, but ONLY if there hasn’t been any motion in the last half hour. That way if we’re watching TV with the lights off it won’t keep turning them back on.

Has anyone done something similar?

I have a somewhat oblique answer -

My previous HA setup used SmartThings - they had some good ideas, among them a concept of a “Mode” for the house. I have adopted this in HA using an Input_select as the mode. The idea is you use specific events to set the mode, e.g. set it to evening when the light goes below a certain intensity. Set it to morning when motion is first detected after a certain time. The upside of this is that you can then use the mode as conditions in other automations. So for instance, I have lights come on low at night if anyone goes downstairs, but only if the mode is set to “night”, that way they are not triggering during the evening when I am watching TV.

You could maybe adapt something similar for your setup? I end up having the following modes:

Morning (triggered by motion after 5am) - downstairs lights come on
Day (triggered by outside light > 200lux) - all lights off
Evening (triggered by outside light < 200 lux within 2hrs of sunset) - Lights on
Night (manually triggered by telling Alexa its bedtime) - all lights off, motion detection triggered for downstairs lighting

I now am at the point where I rarely have to worry about the lights downstairs they just take care of themselves :slight_smile:

If you need something a little more complicated, you could for instance set an additional “do not disturb” flag when you are running your “watch tv” scene that would disable the motion lights.

Hopefully some food for thought!

Interesting, would you mind sharing your config (or that part)? I think I could use that with a script o set a “living room is empty” flag if no motion is detected for 30 minutes, and then have a trigger that only fires if motion is detected and the flag is set.

I tried just setting it for transitions from off to on, but if we sat too still and then moved it would trigger.

Sure - I might put the whole thing up as an example, but for now, here are some bits to get you started (apologies, all my files are split up so you’ll have to figure out the indentation and missing keywords, let me know if you have trouble with that part)

First I create the input boolean.

house_mode:
  name: House Mode
  icon: mdi-home
  options:
   - Morning
   - Day
   - Evening
   - Night
  initial: Day

Here is an example of me setting it for evening mode - an automation:

- alias: 'Evening Mode'
  trigger:
  - platform: numeric_state
    entity_id: sensor.side_multisensor_luminance_25
    below: 200
  condition:
  - condition: state
    entity_id: input_select.house_mode
    state: Day
  - condition: sun
    after: sunset
    after_offset: "-2:00:00"
  action:
    service: script.turn_on
    entity_id: script.evening_transition

And the script which is where I actually set the mode:

alias: Evening Transition
sequence:
- event: LOGBOOK_ENTRY
  event_data:
    message: 'Switching mode to Evening'
- service: input_select.select_option
  data:
    entity_id: input_select.house_mode
    option: Evening
- service: script.turn_off
  data:
     entity_id: script.timed_dim_off
- service: scene.turn_on
  data_template:
    entity_id: >
      {% if is_state('group.all_devices', 'home') %}
        scene.downstairs_on_transition
      {% else %}
        scene.downstairs_front
      {% endif %}
- service: notify.notify
  data:
    message: 'Switching mode to Evening'
    title: ''

And finally here is an example of my motion lights automation taking into account the mode:

- alias: 'Night Lights 1'
  trigger:
    platform: state
    entity_id: binary_sensor.downstairs_sensor_26
    to: 'on'
  condition:
  - condition: time
    before: '23:59:59'
  - condition: time
    after: '20:00:00'
  - condition: state
    entity_id: input_select.house_mode
    state: Night
  action:
    service: script.turn_on
    entity_id: script.timed_dim_on

- alias: 'Night Lights 2'
  trigger:
    platform: state
    entity_id: binary_sensor.downstairs_sensor_26
    to: 'on'
  condition:
  - condition: time
    before: '05:30:00'
  - condition: time
    after: '00:00:00'
  - condition: state
    entity_id: input_select.house_mode
    state: Night
  action:
    service: script.turn_on
    entity_id: script.timed_dim_on

I have this as 2 separate automations to avoid spanning midnight as that did not work at the time, I believe it now should be possible to do this with a single automation as of 0.21 but I haven’t tried it out yet.

Let me know if you have any questions!

1 Like

Hi kelbot, die you already found a way to do something with the “time motion sensor away”? I want to do the same with the sensors in my living room and turning out the lights.
Alternative could be that the state of the sensor stays “on” as long there is movement in every 10 minutes. Important will be that it will not be set to off by a script after 10 minutes after a certain movement while there was movement all the time.

@bart, probably a little late, but maybe helpful for others. You can setup something like the below:

alias: 'Study Lights OFF'  
trigger:
  platform: state
  entity_id: binary_sensor.aeotec_zw074_multisensor_gen5_sensor_5
  from: 'on'
  to: 'off'
  for:
    minutes: 4
action:
  service: light.turn_off
  entity_id: light.study
2 Likes

It is also possible to use last_updated or last_changed:

    - condition: template
      value_template: '{{(as_timestamp(now())-as_timestamp(states.climate.termostat_stue.last_updated)) > 300 }}'
4 Likes

I used the format from the cookbook. With the timed lamp script. Works good.

@Danielhiversen Would this template also work for a group? So e.g. group.motion (which contains all motion sensors)?

Yes, you can test it in the template editor

Good point! Actually have never used the template editor but it works! Thanks!