Two motion sensors, one light


#1

Guys, I’d need a little help with designing a piston for automating my kitchen light:

1) Give a description of the problem
I have two motion sensors in my kitchen, one directing outside of the kitchen door (let’s call it motion1), one inside the kitchen (motion2). The idea is that the kitchen light switches on when someone approaches the kitchen door (motion1 fires), so that the light is on when entering the kitchen (and we don’t have a delay). I now want a piston that switches off the light again when motion2 doesn’t fire within 10 to 15 seconds after motion1 - that would indicate that someone passed the kitchen door, but did not actually enter the kitchen (usually my dog :-)). Only if motion2 is active or has become active 10 to 15 seconds after motion1, should the light stay on.

2) What is the expected behaviour?
In short:

  • make “motion1 change to active” turn on the light - easy as cake
  • switch off the light if after 10-15 seconds motion2 did NOT change to active

3) What is happening/not happening?
I have experimented with “wait”, but this only worked unreliably and somehow messed up the automation. I have experimented with different conditions and triggers for motion2 to switch off the light, but that also didn’t lead to the wanted behavior.
I have the impression that the solution should be super simple and obvious, but I don’t find the correct logic for it.

thanks a lot in advance for your input!


#2

There are several ways to do this. I have not tested this, but give it a try.

IF Motion1 changes to active
  Then
    Turn light on
    IF Motion2 stays inactive for 15 seconds (you will need to change this to "never subscribe")
      Then
        Turn light off
    END IF
END IF

#3

A trigger inside a trigger is asking for trouble…


I would likely start with something like this:

IF Motion1 changes to active  <-- Trigger
Then
    Turn on Light
    Wait 15 seconds
    IF Motion2 is inactive    <-- Condition
    Then
        Turn off Light
    END IF
END IF

Note: You will still need logic to turn off the light if the kitchen is occupied.


#4

If I’m understanding you correctly, I think I do something similar with one of my stairwell lights. Assuming you adjust the inactive time values, does this do what you want?

My logic: Turn the stairwell base light on when either motion sensor detects motion
Turn off the stairwell base light when sensor #1 has been inactive for 10 minutes and #2 for 5.


#5

I have a hunch that the triggers on lines 29 & 31 cannot both be true at the same instant…


#6

Even if you “never subscribe” the second IF?


#7

It seems to be working. My programming logic teacher in college used to say “I have a hard time arguing with something that works”, even when something was probably not the most elegant. I suspect the other trigger line becomes a condition when it wasn’t the trigger, if that makes sense.

I got curious about this after you posted, and did some additional testing. I added this log line:

Here’s the log output:

I’m thinking that when Motion 1 sends temperature, humidity or lux updates, the piston triggers. Perhaps I’m confused why this seems to work, but the timing doesn’t appear quite right.


#8

Thanks, that’s something I had tried before, but without the “never subscribe”, and it didn’t work (I need to do some reading on the “never subscribe” part). I have now tested it with your logic, and it doesn’t work properly, unfortunately. It does switch on the light after motion1 triggers, but irrespective of whether motion2 changes to active or not, switches the light off after 15 seconds.
I’ll try the other ideas/suggestions now, and will report on the outcome. Maybe it’s not as simple as I thought!


#9

Normally, perhaps, but remember STAYS is a fickle beast… it begins the timer at the trigger, and runs the commands after the timer expires.

I think if you check you will see executions at the incorrect times with that method.


#10

This one seems to be working as I expect.

Here are the logs:

If this doesn’t work for you, perhaps I’m not understanding what you’re trying to do.


#11

WCmore’s logic seems to work. I had tried “something” similar before without success, so I wanted to stay away from “wait”, but if it works there is nothing to complain.

That’s my piston now, please comment when something isn’t good in your opinion, I am eager to learn:


#12

Yep. I ran some tests and just as you said. Also, it is important to keep the recycle time of the motion sensor in mind. Not sure if they differ by brand, but my recycle time from active back to inactive is 20 seconds. And that change in state can cause a piston to fire (or not fire as expected). SO, one must code their triggers and conditions accordingly.


#13

Yes, for my sensors I can say for sure that the recycle time is different. One (a Tuya Zigbee motion sensor) is at around 20 seconds, another one (one of the new Sonoff Zigbee motion sensors) is way longer, around 1 minute. So, there is some trial and error involved in coding, and also in the positioning of the sensors.