Help with Complex Motion Sensor Automation

That’s why I set the mode to restart. I cancel the running the timer, and restart it when motion is detected, that way the timer will keep running for as long as there is motion. I then react to timer.finished which will only fire when no motion has been detected for the duration of the timer. mode: restart is crucial to making that work.

And I use door contact sensors, and motion sensors. Here is how I turn up the volume as I walk past a speaker when I am playing music across the house, and how I reset the volume to almost muted after a period of time:

alias: 'Music Walk With Me: Hall'
description: ''
trigger:
  - type: no_motion
    platform: device
    device_id: 16942ffea955cab30431d0ef8be0e53a
    entity_id: binary_sensor.hall_motion_home_security_motion_detection
    domain: binary_sensor
    id: motion_end
  - type: motion
    platform: device
    device_id: 16942ffea955cab30431d0ef8be0e53a
    entity_id: binary_sensor.hall_motion_home_security_motion_detection
    domain: binary_sensor
    id: motion_start
  - platform: event
    event_type: timer.finished
    id: timer_finished
    event_data:
      entity_id: timer.music_walk_with_me_hall
condition:
  - condition: device
    device_id: b1dd9b31c5204c4a9068af5307db22d6
    domain: media_player
    entity_id: media_player.hall_speaker
    type: is_playing
  - condition: state
    entity_id: input_boolean.music_walk
    state: 'on'
action:
  - choose:
      - conditions:
          - condition: trigger
            id: motion_start
          - condition: device
            device_id: b1dd9b31c5204c4a9068af5307db22d6
            domain: media_player
            entity_id: media_player.hall_speaker
            type: is_playing
        sequence:
          - service: script.turn_on
            target:
              entity_id: script.music_walk_volume_adjust
            data:
              variables:
                media_target: media_player.hall_speaker
                volume_target: >-
                  {{ states('input_number.master_volume_walk_with_me')|float /10
                  }}
          - service: timer.cancel
            target:
              entity_id: timer.music_walk_with_me_hall
          - service: timer.start
            data:
              duration: '01:30:00'
            target:
              entity_id: timer.music_walk_with_me_hall
      - conditions:
          - condition: trigger
            id: timer_finished
          - condition: device
            device_id: b1dd9b31c5204c4a9068af5307db22d6
            domain: media_player
            entity_id: media_player.hall_speaker
            type: is_playing
        sequence:
          - service: script.turn_on
            target:
              entity_id: script.music_walk_volume_adjust
            data:
              variables:
                media_target: media_player.hall_speaker
                volume_target: 0.1
      - conditions:
          - condition: trigger
            id: motion_end
        sequence:
          - service: timer.cancel
            data: {}
            entity_id: timer.music_walk_with_me_hall
          - service: timer.start
            data:
              duration: '00:15:00'
            target:
              entity_id: timer.music_walk_with_me_hall
    default: []
mode: restart
1 Like

You also don’t need two triggers of the same source and state to look at different conditions. Was there something additional in this automation that wasn’t listed? I would +1 looking at the timer finished event. Looking at the timer idle state would evaluate as true and trigger when starting up home assistant.

Hello Andrew and all,

At first let me thank you all again for trying to help. I did try the solution of restart which totally makes sense, however, timer finished and it didn’t restart resulting into turning the lights off even if there is movement. I set the restart on the first automation that is turning the lights on and starts the timer.

The way that I solved it is by changing the automation that turns the light off. Originally, it had a trigger when the timer expires (by state). Now I added a trigger when the motion sensor has off state.

That way, even when the timer expires but the sensor reports movement the automation triggers uppon expiration but it doesn’t turn the lights off. When the movement stops it triggers again and the lights turn off.

For a beginner I think that it’s fine even if I would like to have everything within one automation. I will study yours and create a test automation to join everything!

Thanks

That’s because you were originally listening to the state of the timer, rather than the timer finished event. With the timer state, even if you cancel the timer and then start the timer every time motion is detected, briefly the timer state will change from active to idle which would trigger the automation to turn the lights off.

Listen for timer.finished instead that will only fire when the timer actually completes the countdown.

I also had a problem with a light controlled by a motion sensor, which also had to work in different modes.
With just 1 automation and a helper, I couldn’t get it to work as wanted.
Now I have 2 automations:

  • 1 that turns the light on at motion
  • the second that turns it off when there is no motion after some time

With a helper, you could influence the second automation from firing or not.
Don’t know if this is the ‘best’ way but it seems to do the job - only a few days in place now.

Hi Andrew,

Unfortunately i cannot set it up… I would like please your help if you have the patience. I started from the beginning removing all automations and set up the first one that is supposed to turn the light on, when motion is detected. Please keep in mind that depending on the time, different scenes should activate. Here is what i have so far:

id: '1639355234973'
alias: Turn On Kitchen Lights on Motion
description: ''
trigger:
  - type: motion
    platform: device
    device_id: 5ebd7d8857ed491b7ba83e91f740c20c
    entity_id: binary_sensor.motion_sensor_1_ias_zone
    domain: binary_sensor
    id: 16:00 - 21:59
  - type: motion
    platform: device
    device_id: 5ebd7d8857ed491b7ba83e91f740c20c
    entity_id: binary_sensor.motion_sensor_1_ias_zone
    domain: binary_sensor
    id: 22:00 - 8:00
condition: []
action:
  - choose:
      - conditions:
          - condition: trigger
            id: 16:00 - 21:59
        sequence:
          - scene: scene.kitchen_ceiling_lights_warm
          - service: timer.cancel
            target:
              entity_id: timer.kitchen_motion_timer
          - service: timer.start
            data:
              duration: '0'
            target:
              entity_id: timer.kitchen_motion_timer
      - conditions:
          - condition: trigger
            id: 22:00 - 8:00
        sequence:
          - scene: scene.kitchen_ceiling_middle
          - service: timer.cancel
            target:
              entity_id: timer.kitchen_motion_timer
          - service: timer.start
            data:
              duration: '0'
            target:
              entity_id: timer.kitchen_motion_timer
    default: []
mode: restart

It seems to be evaluating correctly (so it is choosing the correct scene) however, even when set to restart, it doesn’t replenish the timer and therefore, even if there is motion the timer expires.

Let’s fix this at first and then we go to the automation that turns the lights off.

Do you see that i am doing something wrong?

Thanks

The trigger will ALWAYS either be the first or second trigger because the ID is just how you refer to the trigger. Your time checks need to be in the choose blocks, in order to decide what scene is activated.

You should only have ONE trigger for this, because both triggers are pointing to the same device.

alias: Turn On Kitchen Lights on Motion
description: ''
trigger:
  - type: motion
    platform: device
    device_id: 5ebd7d8857ed491b7ba83e91f740c20c
    entity_id: binary_sensor.motion_sensor_1_ias_zone
    domain: binary_sensor
    id: motion
condition: []
action:
  - choose:
      - conditions:
          - condition: trigger
            id: motion
        sequence:
          - choose:
              - conditions:
                  - condition: time
                    after: '15:59'
                    before: '22:00'
                sequence:
                  - scene: scene.kitchen_ceiling_lights_warm
              - conditions:
                  - condition: time
                    before: '08:00'
                sequence:
                  - scene: scene.kitchen_ceiling_middle
            default: []
          - service: timer.cancel
            target:
              entity_id: timer.kitchen_motion_timer
          - service: timer.start
            data:
              duration: '0'
            target:
              entity_id: timer.kitchen_motion_timer
    default: []
mode: restart

How does this work?

That’s a really awesome automation, TX1

I’ve managed to copy it and use it for a light:

alias: 'Light: Bathroom Light'
description: ''
trigger:
  - type: motion
    platform: device
    device_id: d0fe2ea622662aded1f47fa576fc746e
    entity_id: binary_sensor.upstairs_motion_sensor_occupancy
    domain: binary_sensor
    id: door_open
  - platform: device
    type: turned_on
    device_id: 0773e356b0e4d174faaa73890348b335
    entity_id: light.bathroom_mirror_light_2
    domain: light
    id: light_on
    for:
      hours: 0
      minutes: 0
      seconds: 5
      milliseconds: 0
  - platform: event
    event_type: timer.finished
    id: timer_finish
    event_data:
      entity_id: timer.bathroom_light
  - platform: device
    type: turned_off
    device_id: 0773e356b0e4d174faaa73890348b335
    entity_id: light.bathroom_mirror_light_2
    domain: light
    id: light_off
condition: []
action:
  - choose:
      - conditions:
          - condition: and
            conditions:
              - condition: trigger
                id: door_open
              - condition: or
                conditions:
                  - condition: state
                    entity_id: binary_sensor.day_night_time
                    state: 'off'
        sequence:
          - type: turn_on
            device_id: 0773e356b0e4d174faaa73890348b335
            entity_id: light.bathroom_mirror_light_2
            domain: light
            brightness_pct: 40
          - service: timer.cancel
            target:
              entity_id: timer.bathroom_light
            data: {}
          - service: timer.start
            data:
              duration: '00:02:00'
            target:
              entity_id: timer.bathroom_light
      - conditions:
          - condition: trigger
            id: light_on
          - condition: state
            entity_id: input_boolean.house_in_night_mode
            state: 'off'
        sequence:
          - service: timer.cancel
            target:
              entity_id: timer.bathroom_light
            data: {}
          - service: light.turn_on
            target:
              entity_id: light.bathroom_mirror_light_2
            data:
              transition: 20
              brightness_pct: 40
          - service: timer.start
            data:
              duration: '00:02:00'
            target:
              entity_id: timer.bathroom_light
      - conditions:
          - condition: trigger
            id: light_on
          - condition: state
            entity_id: input_boolean.house_in_night_mode
            state: 'on'
        sequence:
          - service: timer.cancel
            target:
              entity_id: timer.bathroom_light
            data: {}
          - service: light.turn_on
            target:
              entity_id: light.bathroom_mirror_light_2
            data:
              transition: 20
              brightness_pct: 8
          - service: timer.start
            data:
              duration: '00:10:00'
            target:
              entity_id: timer.bathroom_light
      - conditions:
          - condition: trigger
            id: timer_finish
        sequence:
          - service: light.turn_off
            target:
              entity_id: light.bathroom_mirror_light_2
            data: {}
      - conditions:
          - condition: trigger
            id: light_off
        sequence:
          - service: timer.cancel
            target:
              entity_id: timer.bathroom_light
            data: {}
    default: []
mode: restart

That worked awesome! However, when I try it with a switch, it doesn’t function at all:

alias: 'Light: Kitchen Light'
description: ''
trigger:
  - type: motion
    platform: device
    device_id: 99d0207b6166987d66d3708b126d16a2
    entity_id: binary_sensor.kitchen_motion_sensor_occupancy
    domain: binary_sensor
    id: kitchen_active
  - platform: device
    type: turned_on
    device_id: 3054cba49027e0960c4066d5e10a3924
    entity_id: switch.kitchen_fan_light
    domain: switch
    id: kitchenlight_on
    for:
      hours: 0
      minutes: 0
      seconds: 5
      milliseconds: 0
  - platform: event
    event_type: timer.finished
    id: timer_finish
    event_data:
      entity_id: timer.kitchen_light
  - platform: device
    type: turned_off
    device_id: 3054cba49027e0960c4066d5e10a3924
    entity_id: switch.kitchen_fan_light
    domain: switch
    id: kitchenlight_off
condition: []
action:
  - choose:
      - conditions:
          - condition: and
            conditions:
              - condition: trigger
                id: kitchenlight_on
              - condition: or
                conditions:
                  - condition: state
                    entity_id: binary_sensor.day_night_time
                    state: 'off'
        sequence:
          - type: turn_off
            device_id: 3054cba49027e0960c4066d5e10a3924
            entity_id: switch.kitchen_fan_light
            domain: switch
          - service: timer.cancel
            target:
              entity_id: timer.kitchen_light
            data: {}
          - service: timer.start
            data:
              duration: '00:02:00'
            target:
              entity_id: timer.kitchen_light
      - conditions:
          - condition: trigger
            id: light_on
          - condition: state
            entity_id: input_boolean.house_in_night_mode
            state: 'off'
        sequence:
          - service: timer.cancel
            target:
              entity_id: timer.kitchen_light
            data: {}
          - service: switch.turn_on
            data: {}
            target:
              entity_id: switch.kitchen_fan_light
          - service: timer.start
            data:
              duration: '00:02:00'
            target:
              entity_id: timer.kitchen_light
      - conditions:
          - condition: trigger
            id: switch_on
          - condition: state
            entity_id: input_boolean.house_in_night_mode
            state: 'on'
        sequence:
          - service: timer.cancel
            target:
              entity_id: timer.kitchen_light
            data: {}
          - service: switch.turn_on
            data: {}
            target:
              device_id: 3054cba49027e0960c4066d5e10a3924
          - service: timer.start
            data:
              duration: '00:10:00'
            target:
              entity_id: timer.kitchen_light
      - conditions:
          - condition: trigger
            id: timer_finish
        sequence:
          - service: switch.turn_off
            target:
              device_id: 3054cba49027e0960c4066d5e10a3924
            data: {}
      - conditions:
          - condition: trigger
            id: switch_off
        sequence:
          - service: timer.cancel
            target:
              entity_id: timer.kitchen_light
            data: {}
    default: []
mode: restart

Did I mess something up, doesn’t this work with switches - or a I just a complete n00b? Your time would be appreciated.

Does it have error messages?

1 Like

I mixed up the states of the motionsensor… :shushing_face:

1 Like

The way I did mine was to set up scenes for the lights for different brightness.
Then 2 scripts to turn on/off the lights and in the ON script manage the time of day logic to call each the required scene.
Then use the automation just to decide when to call the on/off scripts.

e.g. is there motion: yes, is it dark enough: yes, call script ceiling.light.on
In the ON script, is the house set to “sleep”: no, is it after midnight: yes, call the 30% brightness scene

The script logic looks for a match from the top down, so best to set your dimmest setting first then gradually brighter until you get to 100% at the bottom of the script (found that out the hard way)

Sample script ON:

alias: Hallway Ceiling Light ON
sequence:
  - choose:
      - conditions:
          - condition: state
            entity_id: input_boolean.sleeping
            state: 'on'
        sequence:
          - service: scene.turn_on
            target:
              entity_id: scene.hallway_ceiling_light_nightlight
            metadata: {}
      - conditions:
          - condition: time
            after: '23:00:00'
            before: '04:00:00'
            weekday:
              - mon
              - tue
              - wed
              - thu
              - sun
        sequence:
          - service: scene.turn_on
            target:
              entity_id: scene.hallway_ceiling_light_30
            metadata: {}
      - conditions:
          - condition: time
            after: '00:00:00'
            before: '04:00:00'
            weekday:
              - fri
              - sat
        sequence:
          - service: scene.turn_on
            target:
              entity_id: scene.hallway_ceiling_light_30
            metadata: {}
      - conditions:
          - condition: or
            conditions:
              - condition: sun
                after: sunset
              - condition: sun
                before: sunrise
                before_offset: '+01:00:00'
        sequence:
          - service: scene.turn_on
            target:
              entity_id: scene.hallway_ceiling_light_60
            metadata: {}
    default:
      - service: scene.turn_on
        target:
          entity_id: scene.hallway_ceiling_light_normal
        metadata: {}
mode: restart
icon: mdi:lightbulb-on

Sample Script OFF:

alias: Hallway Ceiling Light OFF
sequence:
  - service: light.turn_off
    data:
      transition: 3
    target:
      entity_id: light.hallway_ceiling_lights
mode: restart
icon: mdi:lightbulb-on

Sample automation:

alias: Hallway Ceiling Light Actions
description: ''
trigger:
  - platform: state
    entity_id:
      - binary_sensor.hallway_motion
    id: hallway-motion-on
    to: 'on'
  - platform: state
    entity_id:
      - binary_sensor.hallway_motion
    id: hallway-motion-off
    to: 'off'
  - type: opened
    platform: device
    device_id: 724d40e724d40e724d40e724d40e724d40e
    entity_id: binary_sensor.magnet_front_door
    domain: binary_sensor
    id: front-door-open
  - platform: state
    entity_id:
      - binary_sensor.hallway_occupancy
    id: hallway-occupied-off
    to: 'off'
condition: []
action:
  - if:
      - condition: trigger
        id: hallway-motion-on
      - condition: numeric_state
        entity_id: sensor.internal_illuminance_mean
        below: '45'
    then:
      - service: script.hallway_ceiling_light_on
        data: {}
    else: []
  - if:
      - condition: trigger
        id: front-door-open
      - condition: state
        entity_id: binary_sensor.hallway_motion
        state: 'off'
      - condition: numeric_state
        entity_id: sensor.internal_illuminance_mean
        below: '45'
    then:
      - service: script.hallway_ceiling_light_on
        data: {}
    else: []
  - if:
      - condition: trigger
        id: hallway-motion-off
    then:
      - delay:
          hours: 0
          minutes: 0
          seconds: 30
          milliseconds: 0
      - if:
          - condition: state
            entity_id: binary_sensor.hallway_motion
            state: 'off'
        then:
          - service: script.hallway_ceiling_off
            data: {}
          - delay:
              hours: 0
              minutes: 0
              seconds: 1
              milliseconds: 0
          - service: script.hallway_ceiling_off
            data: {}
        else:
          - service: script.hallway_ceiling_light_on
            data: {}
    else: []
  - if:
      - condition: trigger
        id: hallway-occupied-off
    then:
      - service: script.hallway_ceiling_off
        data: {}
      - delay:
          hours: 0
          minutes: 0
          seconds: 1
          milliseconds: 0
      - service: script.hallway_ceiling_off
        data: {}
mode: restart

In case you’re wondering why there are double OFF commands, my Ikea Tradfri bulbs sometimes don’t fully switch off but running the request twice fixes that.

For the Aqara button I have something similar, but use it to black out a room and ignore motion.

To have it as an on override, something like this:

  1. Create a boolean called kitchen_override
  2. Set an automation from the Aqara button to toggle the kitchen_override on/off
  3. Create an automation to monitor the boolean state and set the lights on and the motion-light automation off

This is my “blackout” automation to give you an idea. Reverse logic, but hopefully useful

alias: Hallway Blackout
description: ''
trigger:
  - platform: state
    entity_id:
      - input_boolean.blackout_hallway
    to: 'on'
    id: blackout-on
  - platform: state
    entity_id:
      - input_boolean.blackout_hallway
    to: 'off'
    id: blackout-off
condition: []
action:
  - if:
      - condition: trigger
        id: blackout-on
    then:
      - service: automation.turn_off
        data: {}
        target:
          entity_id: automation.hallway_ceiling_top_light
      - service: script.hallway_ceiling_off
        data: {}
      - delay:
          hours: 0
          minutes: 0
          seconds: 2
          milliseconds: 0
      - service: script.hallway_ceiling_off
        data: {}
  - if:
      - condition: trigger
        id: blackout-off
    then:
      - service: automation.turn_on
        data: {}
        target:
          entity_id: automation.hallway_ceiling_top_light
      - if:
          - condition: numeric_state
            entity_id: sensor.internal_illuminance_mean
            below: '45'
          - condition: state
            entity_id: binary_sensor.hallway_motion
            state: 'on'
          - condition: state
            entity_id: input_boolean.is_there_somone_at_home
            state: 'on'
        then:
          - service: script.hallway_ceiling_light_on
            data: {}
          - delay:
              hours: 0
              minutes: 0
              seconds: 2
              milliseconds: 0
          - service: script.hallway_ceiling_light_on
            data: {}
        else:
          - service: script.hallway_ceiling_off
            data: {}
          - delay:
              hours: 0
              minutes: 0
              seconds: 2
              milliseconds: 0
          - service: script.hallway_ceiling_off
            data: {}
mode: restart

I will try to revive this topic.
How about his automation:
if motion is not detected for 2 minutes dim lights to 50% of its current value.
if no motion is detected for another minute, turn lights off.
if motion is detected within that 1 minute bring lights to original brightness and start waiting cycle again.

I am new to HA, played with some automations, but here I am reaching my current limit, could somebody please point me to right direction?

Hello Martin,

Yes you are correct, although I didn’t reply to this post I have solved it like you propose. The motion sensor turns the light on when motion is detected and then instead of turning the light off immediately, it reduces the brightness to lower percentage twice to give a visual indication that the light will eventually turn off.

Although the ultimate solution would be an mmWave sensor, this is the best workaround I have found so far.

I explain everything here : Lights with Motion, the proper way - Home Automation Ideas

Thanks for pointing me out, I am going to study your code.

EDIT: Unfortunatelly I can’t open that link, browser is refusing to open because too many redirections :confused:

Hi Martin, changed the name servers but it takes some time to propangate. I will let you know when available again!

EDIT: Site is back up again!

So, i figured it out thanks to your post, however I am using timer helpers instead of bulit in timer, so it sholud survive restart, that is just technicality.

However there is one issue I can’t figure out yet. I want the lights to dim to 50% of their actual brightness and then bring them back to that original brightness (rather than 100%)

I created an input helper to store original brightness into before dimming, but when calling a service in automation I don’t know how to use that value in brightness_pct and how to store the brightness in it. How to properly handle that in automation?

- conditions:
          - condition: trigger
            id: movement in bedroom
        sequence:
          - service: timer.cancel
            data: {}
            target:
              entity_id: timer.light_timer
          - service: timer.cancel
            data: {}
            target:
              entity_id: timer.light_timer_2
          - service: light.turn_on
            data:
              brightness_pct: 100
            target:
              device_id: 448dbb6c28ee71252cc037a4eddede35
      - conditions:
          - condition: trigger
            id: timer finished
        sequence:
          - service: light.turn_on
            data:
              brightness_pct: 50
            target:
              device_id: 448dbb6c28ee71252cc037a4eddede35

I have code like that above but instead of hardcoded value of 100% and 50% I want yo use value stored in input_number.bedroom_lights_brigtness and half of that value.

So before the lights is dimmed I need to read brightness of that light, sotre into helper and dim it to half of that brightness.
When motion is detected I need to restore brightness to the value stored in that helper.

Hi Martin,

I haven’t used it myself as I have hardcodwd values but there is a service called: scene: create. It’s function is to create a temporary scene with the brightness/color of your light.

If you use this, just before the lights will dim, you can then re-apply it to restore the original brightness.

Please give it a try in the developer tools to understand how it works. Then you can apply it in your automation via a call service

Thanks.
I was reading about this service, but if I understand it correctly, it might help me to restore lights to the state before dimming.

But still I need to read actuall brightness of the lights so I can calculate half of it and set brightness to that calculated value.

EDIT: I am slowly getting there, this is the proper syntax to set brighness to the value of input helper

data:
  brightness_pct: "{{ states('input_number.bedroom_lights_brigtness') | int }}"

And this is how to set input value to actuall brightness

- service: input_number.set_value
  data:
    value: >-
      {{ (states.light.bedroom.attributes.brightness/255*100)|round(0) }}
    target:
      entity_id: input_number.bedroom_lights_brigtness

Only thing left to figure out is how to store the initial brightness before the automation even starts, otherwise on motion it will restore light to brightness level stored in the helper on previous run.

One solution could be to run separate trigger every time brightness of those lights changes that will store actuall brightness into input helper. But this will also rewrite my stored value even when the brightness is changed within the original automation.

Any idea how to overcome this?