Don't turn off light, if not turned on by motion detection

Just a thought but if you set an additional boolean variable during the on automation you can use this boolean variable as a condition in the off automation to limit it from continuing if it was not set originally.

PCWII is just a little quicker then me :slight_smile:

I’ve made a boolean to turn on to check if it is turned on by motion.
If the light is on, the automations won’t trigger.

And after 10 minutes the boolean is on, trigger it and run the second action. turn off lights and turn off boolean.

alias: Beweging beneden
description: ""
trigger:
  - platform: state
    entity_id:
      - binary_sensor.motion_woonkamer_motion
    to: "on"
    id: Beweging_Gedetecteerd
  - platform: state
    entity_id:
      - input_boolean.ingeschakeld_door_beweging_woonkamer
    from: "off"
    to: "on"
    for:
      hours: 0
      minutes: 10
      seconds: 0
    id: 10 minutes after motion
condition:
  - condition: or
    conditions:
      - condition: state
        entity_id: person.x
        state: home
      - condition: state
        entity_id: person.y
        state: home
  - condition: state
    entity_id: sun.sun
    state: below_horizon
action:
  - if:
      - condition: trigger
        id:
          - Beweging_Gedetecteerd
      - condition: state
        entity_id: light.beneden
        state: "off"
    then:
      - parallel:
          - service: scene.turn_on
            data:
              transition: 20
            target:
              entity_id: scene.beneden_dagelijks
          - service: input_boolean.turn_on
            data: {}
            target:
              entity_id: input_boolean.ingeschakeld_door_beweging_woonkamer
  - if:
      - condition: trigger
        id:
          - 7 minuten na motion
    then:
      - parallel:
          - service: light.turn_off
            data:
              transition: 30
            target:
              entity_id: light.beneden
          - service: input_boolean.turn_off
            data: {}
            target:
              entity_id: input_boolean.ingeschakeld_door_beweging_woonkamer
mode: single

1 Like

You probably could create an Automation which disables your existing Auromation when the light switch is activated to ON and enables your Automation when the light is activated to OFF.

1 Like

Context is a great way to achieve this without adding a helper input_boolean, but less flexible.
I personally like/want to be able to disable the automations in unforeseen situations the automation was not programmed for.
As for enabling/disabling the automation rather than using a helper, I was convinced a long time ago to not do this, but I don’t remember the arguments :wink:

Yeah I actually use an input select that specifies the light “mode”:

lounge_light_mode:
  name: Lounge Light Mode
  options:
  - 'Off'
  - 'Automatic'
  - 'Manual'
  - 'Bright'
  - 'Night Light'
  - 'Watch Movie'
  icon: mdi:format-list-bulleted

This can be changed by any number of automations (e.g. bright if smoke alarm on) including manually. It is then used in other automation conditions.

2 Likes

I just found out a custom integration -entity controller- for this purpose.

https://danobot.github.io/ec-docs/

I solved this using Shelly module with switch in detached mode. So there is one automation that switches the light on/off when switch changes state to respectively and the second that controls light from motions sensor and uses state of physical switch as condition. E.g. if physical switch is in on state then motion sensor does not turn off the light. Different implementation, but logic is the same :slight_smile:

Considered an automation that waits for the trigger motion sensor off (duration x)?

I think that’s the easiest approach.
So if it’s turned on by the automation, it will turn it off. Just one automation with a wait for trigger.

You neglected to address this requirement:

But this has been suggested already in post #3.

Was this a response to me?
Cause I don’t think I neglected to address a requirement.

In baby steps:

  1. trigger: Motion detect
  2. Condition: light equals off
    If not, the light was already on and so this automation should not do anything
  3. Action 1: Turn on the light
  4. Action 2: Wait for Motion detect is off
  5. Action 3: Turn off the light

If the light was:

  1. already on at the time motion sensor detected motion, it’s considered to be turned on manually
  2. on based on the motion sensor, there is no need to turn it on manually

Even in the rare situation where one might want to turn the light off and on again to prevent it to go off after the motion detect is finished, one could add a condition in between action 2 and 3, that compares the light on time and the automation trigger time. If not within say 0.05 second (which is on the safe side), then it’s considered to be turned on manual and so you do not turn it off.

What requirement did I miss?

1 Like

Ok I re-read what you wrote. I misunderstood your suggestion, sorry. I see you mean to wait in the motion-light-on automation. Sure that might work. Waiting in automations for long periods of time is not best practice though.

No worries :+1:

Define long periods of time. Since it’s likely a single execution automation, the risk of unexpected behavior or other interference is very limited. I use this scenario in several occasions and it works perfectly fine. Especially since an individual automation-update doesn’t reload all automations anymore nowadays. So basically the only thing that breaks it is a restart of HA or a manual reload of all automations.

Having that said, I started my post with the question if “my solution” has been considered. If it is/was and considered not suitable, it’s fine of course.

From my point of view it’s the easiest and most robust way to achieve the goal. But perhaps there are contractual arguments that make my solution not desired.

Anything over a minute or two. And with motion possibly occurring in the room for over an hour the chances of the automation being reset by a restart or reload are not insignificant.

Well I guess we are not on the same page here.

If a motion detection sensor detects motion and turns on the light and the automation is in single mode, it won’t break anything when it tries to trigger it again. I think it might even work if it’s not in Single, as long as your condition is correct. But since I have mine in single mode, I am not sure.

Anyway, I have my kitchen sensor waiting an hour to get a 'motion clear" for the period of 35 seconds. It works flawless. I even have multiple automations that get triggered by this one motion sensor.

One that handles the speaker (follow me audio), another that handles the lights, a third that notifies my at motion when presence at home is 0. Never experienced/noticed one issue with it. And I am pretty keen on detecting automation failures :wink:

But if anyone considers it too big a risk, I’d suggest the solution I had before, being two automations.

  1. The turn on lights at motion detect, with the condition that the light is not already on
  2. The turn off lights with the condition that the timestamp of the ‘light turn on’ must be at most 0.05 seconds later than the trigger time of the ‘turn on automation’.

The trigger timestamp of an automation is only updated once the conditions are matched.So if it tries to trigger the ‘on automation’ again and the conditions are not matched, the timestamp is not updated. If it’s toggled manual, the ‘on automation’ also doesn’t match, so the ‘off automation’ doesn’t match the conditions of 0.05 sec.

And there are probably a lot of solutions where a couple (and perhaps also my “previous solution”) likely are already suggested in this thread.

I was excited by this solution, but than thought of couple of real sencarios, maybe you could help me solve them as well and maintain the same method.

  1. what happens if i’m already in the room and motion was detected and the lights turns on? i know that the motion sensor will detect i’m not in the room after ±2 minutes and will turn off, and as soon as i’ll reach to my phone to turn the lights on - the motion sensor will recognize motion and will turn on again. so i won’t make (turn on the light) before the motion detection. so i will never be able to turn the light on manually before the automation runs.
    Even if i will manually turn off the light and turn it on again, it doesn’t meter since the automation is running and pending “no motion” which wasn’t the case.

  2. Edge case so maybe not worth thinking about it, but can happen - motion detected → lights turn on → waiting for no motion → system restart/power outage → HA restarts (so automation is killed).
    Now motion detected again → but lights is already on, so the automation won’t run hence the light will stay on and won’t turn off (again, rare case)

Edit:
For number 1,
maybe we can do “pending for no motion OR light is off (meaning manual shut down”)
Then i can turn off and on the light and the light should stay on. no?

Lets debug the scenario’s.

Scenario 1

  • what happens if i’m already in the room and motion was detected and the lights turns on?
    I don’t exactly get your point. If you are in the room and the motion sensor detects you while the light is still off, it will turn it on. If the light is already on, it will do nothing since the condition (step 2) requires the light to be off.

  • i know that the motion sensor will detect i’m not in the room after ±2 minutes and will turn off, and as soon as i’ll reach to my phone to turn the lights on - the motion sensor will recognize motion and will turn on again.
    That’s no problem since the condition (step 2) requires the light to be off to continue, so the automation will not run in this scenario and you will have to turn the light manually off again.

  • so i won’t make (turn on the light) before the motion detection. so i will never be able to turn the light on manually before the automation runs.
    If you want the light to turn off after the motion detect stops while you turned it on manually, you should add a trigger to the (same) automation that responds to the light on. Next you can remove the condition that requires the light to be off (step 2) cause you want to have the automation to run weather your light is on or of.
    If you have your automation mode set to Single, it won’t run twice. Add max_exceeded: silent to your automation to keep your log clean.

  • Even if i will manually turn off the light and turn it on again, it doesn’t meter since the automation is running and pending “no motion” which wasn’t the case.
    I am not sure what you mean with this, but I think this part is covered in the above elaboration.

P.s. Just to be clear, the topic I responded to was a scenario to not turn off the light unless it was triggered by motion detect.

Scenario 2
Edge case so maybe not worth thinking about it, but can happen - motion detected → lights turn on → waiting for no motion → system restart/power outage → HA restarts (so automation is killed). Now motion detected again → but lights is already on, so the automation won’t run hence the light will stay on and won’t turn off (again, rare case)
No big challenge that can be solved in multiple ways.

  1. The easiest way probably is to add ‘motion detect clear’ as trigger. Since the automation will run only once (in single mode) nothing will happen if the automation is running. Next, the clear should also only run when the light is on.

  2. Another approach would be to have the automation triggered by ‘When Home Assistant Starts’. That might even be safer since I don’t know what the state of a motion detect sensor is at reboot. Likely it’s ‘clear’ as in not detecting.

Of course, in the case of a reboot, there is currently no certainty that the light was turned on by motion detect unless you write it to a (permanent) helper or something.

I hope I understood you correctly and that the above elaboration helps you in solving the situation.

genius I love it

1 Like

I am just coming to Home Assistant, and have encountered this problem.

Is the entity controller integration now the best way to tackle this? Or is it better to save the switch context and use it in later automations ? (I gather from reading these forums that using boolean helpers is the least preferred option).

@tom_l are you using the entity controller now, or continue to use context?

Thanks