Motion Sensor automation for light with conditions

Hi :slight_smile:

In my bedroom I have 4 IKEA TRÅDFRI Bulps connected with one remote, and one TRÅDFRI motion sensor (HA entity: sensor.underseng_motionsensor) which is connected to gateway only but no bulps yet … and I need this motion sensor to turn on 1 of the bulps (HA entity: light.under_seng) under the following conditions:

  • If the bulp is already on then it should leave it as it is = do nothing!

  • If the bulp is off, and motion is detected it should turn it on for 2 minutes and then off again, but only between sunset minus 30 minutes and sunrise.

  • If possible define in the automation which color that bulp should have.

Can someone help me get this working? … I am really new to this automation stuff in HA so please bear with me. :smiley:

(Running HassOS 2.12, HASS.IO Supervisor 164, HA 0.93.2)

Best Regards
Stig

I have now made the following automation, which should give me what I need, except for the part where I need at condition that only trigger the on and off from motion sensor if the light is not already turned on … Can someone please help me on this, I am totally clueless how to make this work.

- alias: Turn on Underseng light when there is movement
  trigger:
    platform: state
    entity_id: sensor.underseng_motionsensor
    to: 'on'
  action:
    service: light.turn_on
    entity_id: light.under_seng
    data:
      transition: 1
      brightness: 255
      color_name: purple

- alias: Turn Underseng light 2 minutes after last movement
  trigger:
    platform: state
    entity_id: sensor.underseng_motionsensor
    to: 'off'
    for:
      minutes: 2
  action:
    service: light.turn_off
    entity_id: light.under_seng
    data:
      transition : 5

Add a condition to the first automation requiring that the light.under_seng is off to allow the action to be executed. If it is already on then the action will not be executed.

- alias: Turn on Underseng light when there is movement
  trigger:
    platform: state
    entity_id: sensor.underseng_motionsensor
    to: 'on'
  condition:
    condition: state
    entity_id: light.under_seng
    state: 'off'
  action:
    service: light.turn_on
    entity_id: light.under_seng
    data:
      transition: 1
      brightness: 255
      color_name: purple
1 Like

Maybe I’m stupid, but since when do we support tradfri motion sensors as entities? I only get the battery level…

That is great!! … But will the Motion sensor not trigger the off when it is already turned on if nothing is set in the OFF automation?

I read this question several times but it only made my head hurt. :face_with_head_bandage:

Can you re-phrase it please?

@123
I see what you mean…

Case : The bulp is already on … a person gets in front of the motion sensor … what will then happen to the “Turn Underseng light 2 minutes after last movement” when no condition on that part is set? … will it then
turn the light off after 2 min or will it let it be turned on as it should?

@LukasQ
Hmm … so you think the IKEA Motion Sensor will not work? … what about a Philips Hue motion sensor then? … I am not at home at the moment so I am not able to see if the automation is working or not, but your statement worries me.

The second automation works exactly the way you wrote it. It will always turn off the light if there is no motion detected for at least 2 minutes.

If you turn on the light and then walk out of the room, if the motion sensor detects no motion for 2 minutes, the second automation will turn off the light.

If this is not the way you want it to work, then the second automation will have to be modified.

Okay, that was what I thought … I need it not to turn off if it was turned on by remote/HA intercae, only if it was turned on by the motion sensor.

OK, that means the second automation needs a condition that checks if the light was turned on by the motion sensor (or by someone/something else). It will turn off the light only if the light was turned on by the motion sensor, otherwise it will leave the light on.

One simple way to do this is by using an input_boolean to serve as an indicator.

input_boolean:
  motion_detected:
    name: Motion Detected

The first automation enables the input_boolean.

- alias: Turn on Underseng light when there is movement
  trigger:
    platform: state
    entity_id: sensor.underseng_motionsensor
    to: 'on'
  condition:
    condition: state
    entity_id: light.under_seng
    state: 'off'
  action:
  - service: input_boolean.turn_on
    entity_id: input_boolean.motion_detected
  - service: light.turn_on
    entity_id: light.under_seng
    data:
      transition: 1
      brightness: 255
      color_name: purple

The second automation uses a condition to check the input_boolean’s state. If it is on then it disables the input_boolean and turns off the light.

- alias: Turn Underseng light 2 minutes after last movement
  trigger:
    platform: state
    entity_id: sensor.underseng_motionsensor
    to: 'off'
    for:
      minutes: 2
  condition:
    condition: state
    entity_id: input_boolean.motion_detected
    state: 'on'
  action:
  - service: input_boolean.turn_off
    entity_id: input_boolean.motion_detected
 -  service: light.turn_off
    entity_id: light.under_seng

Motion Sensor. No status.


But as 123 say, you could use the connected light status

I’ll likely be using something similar to this for my motion lighting implementation, but I have one question. If I walk into a room and the light is triggered by motion, and I want to stay in that room with the light on, but I am sitting without enough motion to retrigger, what would keep the light from turning off with these automations? I would assume nothing as I couldn’t turn the light on manually once it’s already triggered and on by the motion.

The answer to your question is precisely what you’ve already guessed it is: nothing will prevent it.

There are a few very sensitive occupancy detectors available, able to detect even subtle movements, but they’re usually not battery-powered (a sensitive motion sensor would be reporting frequently and drain its battery quickly).

The natural solution is to indicate to Home Assistant that it should not automatically turn off the light because you’ll do that manually. In other words, you want to temporarily disable auto-off.

This is easier said than done. How does one easily indicate auto-off should be disabled but only temporarily?

The simplest way would be to manually turn the light off and then back on. The fact it is now manually turned on is an indication to Home Assistant that the “automatic light” function should be temporarily suspended. However, the awkward aspect of this approach is that you first have to turn the light off (which was originally turned on automatically due to detected motion) before turning it back on.

I solved this dilemma for myself a while ago in HomeSeer using a virtual device with 4 settings and have just ported everything over into HA. Rather than a boolean, I used a drop down helper called XYZ Motion Control. I have one for bedroom lighting and one for fireplace lighting. The contents of the drop downs are Disabled, Enabled, Manual and Motion. If I set the drop down to Disabled, motion control of the lights is disabled which I frequently use for the bedroom lights. To override No Motion from turning the light off after motion has turned it on (i.e. while you are sitting quietly reading a book or meditating) just manually turn the light off and back on either via the physical switch, Alexa/Google or in HA, to change the helper status to Manual mode. Here are my automations:

- id: '1593757952019'
  alias: Motion - Living Room
  description: ''
  trigger:
  - device_id: fd35c05fb8844c6ab36412de31ecc280
    domain: binary_sensor
    entity_id: binary_sensor.lumi_lumi_sensor_motion_38216304_ias_zone
    platform: device
    type: motion
  condition:
  - condition: not
    conditions:
    - after: sunrise
      before: sunset
      condition: sun
  - condition: state
    entity_id: input_select.fireplace_motion_control
    state: Enabled
  - condition: state
    entity_id: switch.pole_lamp
    state: 'off'
  - condition: state
    entity_id: light.living_room_light
    state: 'off'
  - condition: state
    entity_id: switch.switchlinc_relay_dual_band_1f_b7_25
    state: 'off'
  action:
  - data:
      option: Motion
    entity_id: input_select.fireplace_motion_control
    service: input_select.select_option
  - data: {}
    entity_id: switch.switchlinc_relay_dual_band_1f_b7_25
    service: switch.turn_on

- id: '1593758340565'
  alias: No Motion - Living Room
  description: ''
  trigger:
  - device_id: fd35c05fb8844c6ab36412de31ecc280
    domain: binary_sensor
    entity_id: binary_sensor.lumi_lumi_sensor_motion_38216304_ias_zone
    platform: device
    type: no_motion
  condition:
  - condition: state
    entity_id: switch.switchlinc_relay_dual_band_1f_b7_25
    state: 'on'
  - condition: state
    entity_id: input_select.fireplace_motion_control
    state: Motion
  action:
  - data:
      option: Enabled
    entity_id: input_select.fireplace_motion_control
    service: input_select.select_option
  - data: {}
    entity_id: switch.switchlinc_relay_dual_band_1f_b7_25
    service: switch.turn_off

- id: '1593760273567'
  alias: Fireplace Light - Manual On
  description: ''
  trigger:
  - entity_id: switch.switchlinc_relay_dual_band_1f_b7_25
    platform: state
    to: 'on'
  condition:
  - condition: state
    entity_id: input_select.fireplace_motion_control
    state: Enabled
  action:
  - data:
      option: Manual
    entity_id: input_select.fireplace_motion_control
    service: input_select.select_option

- id: '1593760519203'
  alias: Fireplace Light - Manual Off
  description: ''
  trigger:
  - entity_id: switch.switchlinc_relay_dual_band_1f_b7_25
    platform: state
    to: 'off'
  condition:
  - condition: not
    conditions:
    - condition: state
      entity_id: input_select.fireplace_motion_control
      state: Disabled
  action:
  - data:
      option: Enabled
    entity_id: input_select.fireplace_motion_control
    service: input_select.select_option

And my drop down helper…

I am now thinking about how to tell Alexa (or Google) to change the motion control to Manual mode…

1 Like

@Stkol76 @123
Im trying to do the same thing but with a door sensor. But the condition that it will only trigger if the light is off is not working, the light is turned off when I close the door even if I turn the light on manually before I opened the door.

  - alias: Turn on light
    trigger:
      platform: state
      entity_id: binary_sensor.porta_cozinha
      to: 'on'
    condition:
      condition: state
      entity_id: light.hall
      state: 'off'
    action:
      - service: input_boolean.turn_on
        entity_id: input_boolean.motion_detected
      - service: light.turn_on
        entity_id: light.hall
        
  - alias: Turn off light after 1 minute
    trigger:
      platform: state
      entity_id: binary_sensor.porta_cozinha
      to: 'off'
      for:
        minutes: 1
    condition:
      condition: state
      entity_id: input_boolean.motion_detected
      state: 'on'
      action:
      - service: input_boolean.turn_off
        entity_id: input_boolean.motion_detected
      - service: light.turn_off
        entity_id: light.hall

In the second automation, the word action is indented incorrectly. Move it two spaces to the left.

If you are using a text editor to create your automations, after making changes, execute Configuration > Server Controls > Check Configuration, if there are no errors, execute Configuration > Server Controls > Reload Automations.

omg how could i have missed that, thanks!
My Check Configuration button had disappeared so I didnt check that :sweat_smile:

Im still having the same problem, the light turn off no matter what

  - alias: turn on
    trigger:
      platform: state
      entity_id: binary_sensor.porta_sala
      to: 'on'
    condition:
      - condition: sun
        after: sunset
      - condition: state
        entity_id: light.sala_estar
        state: "off"
    action:
      - service: input_boolean.turn_on
        entity_id: input_boolean.porta_sala
      - service: light.turn_on
        entity_id: light.sala_estar
        
  - alias: turn off
    trigger:
      platform: state
      entity_id: binary_sensor.porta_sala
      to: 'off'
      for:
        minutes: 1
    condition:
      condition: state
      entity_id: input_boolean.porta_sala
      state: 'on'
    action:
      - service: input_boolean.turn_off
        entity_id: input_boolean.porta_sala
      - service: light.turn_off
        entity_id: light.sala_estar

I’m not sure what you are expecting it to do because, given the way the automations are written, the light will always be turned off as long as the input_boolean is on.

The input_boolean is turned on whenever the door is opened. So whenever the door is closed (for 1 minute), and the input_boolean is on (which it will be because it was turned on when the door was opened) the light is turned off. In other words, opening/closing the door turns on/off the light. It’s working exactly as its written.

How do you want it to work?

I thought that with this condition

      - condition: state
        entity_id: light.sala_estar
        state: "off"

it wouldnt turn the boolean on when the light.sala_estar is already on before I open the door