Help a Newbie with some code : Motion & Lights


#1

1) Give a description of the problem
My first decent attempt at a piston appears to be working ok, but I’m sure there’s a better method.

2) What is the expected behaviour?
On motion of my Hallway sensor, the lights come on at various levels depending on the time of day (in my case -$hour24), in conjunction with a virtual switch as a Lux meter.

3) What is happening/not happening?
appears to work ok, but could it be made more efficient?

4)

**5) **
** |+0ms|╔Received event [Home].time = 1589668922916 with a delay of -868ms|

|—|---|
|+280ms|║RunTime Analysis CS > 30ms > PS > 52ms > PE > 198ms > CE|
|+283ms|║Runtime (42492 bytes) successfully initialized in 52ms (v0.3.110.20191009) (282ms)|
|+284ms|║╔Execution stage started|
|+307ms|║║Condition #22 evaluated true (1ms)|
|+308ms|║║Condition group #21 evaluated true (state did not change) (2ms)|
|+310ms|║║Cancelling statement #23’s schedules…|
|+320ms|║║Skipped execution of physical command [Hall Dimmer].setLevel([40]) because it would make no change to the device. (4ms)|
|+321ms|║║Executed [Hall Dimmer].setLevel (6ms)|
|+323ms|║╚Execution stage complete. (38ms)|
|+324ms|╚Event processed successfully (324ms)|


#2

I am a bit tied up at the moment, but I think line 39 should be “OR”

pic

… since they can’t both be true.


Pro Tip:

You could also combine both statements into one:

pic

(which is true from 11pm to 1:59am)


#3

ahh yes… that would explain why it didn’t work just now… :slight_smile:

Thank you WCmore


#4

I just noticed overlapping times… the 23rd hour is in more than one statement.

Also, from 8am thru 2:59pm, no variables will be touched… So this means that Dimmer 3 will be set to 10 during those hours. (line 55)


#5

thanks, I’ve corrected those now.

I’ve noticed that outside of those 3 time ranges defined in the IF- THEN, the hall lights would still light up with the last known value of HALL_LEVEL_MOTION.

I can see this is happening because the action to set the Hall dimmer is outside the IF block, how could this behavior be corrected please? beside scrapping the variables and directly setting the device level.


#6

I would drag & drop a few blocks around, and only adjust the level if it is not daytime.

Maybe something like this:

I took it a step farther here, and used variables as times, so you can tweak up top, and the correct numbers will be inserted into the right sections below.


Note: You also had a $hour in there I had to convert to $hour24


#7

Thank you for your input, following your ideas I’ve decided to tie in my Virtual Lux meter with the action to set the dimmer. This relieves my need to manage the extremes of UK seasonal lighting up times.

I’ve noticed some other odd behaviors now and hopefully you can shed some light on them please.

  1. If I use a single motion device to detect ‘no motion’, the countdown timer seems to restart on motion (which is great), however, with two separate motion devices, the timer does not restart, (then I’m stuck in the dark until I reach the other device)

  2. When looking at the motion device within the smartthing app, there’s a lag between it’s changing state (not a problem when it comes to motion detected), but where the was motion within the countdown period, when the icon changes to ‘no motion’, it seems to trigger a motion event within webcore.

  3. I’ve noticed triggers for ‘change’ and ‘stay’, what are the definitions for these and would one worked better than another regarding changing motion detection.

Thank you very much.


#8

Sorry, I do not see a timer anywhere…

Edit: Oh I see, down in the STAYS block. Yes, this makes sense, since it cannot make a decision until the last flag comes thru. I don’t see how this can keep you in the dark though… Inactive is programmed to dim, not brighten.

(You should see a scheduled wakeup down in the log though)


When there is a lightning bolt next to Motion Sensor 1’s motion, then the piston will execute, and process from top to bottom whenever there is either activity OR inactivity.
(thankfully, your conditions should direct the flow)


Change is immediate… it happens at the moment it changes
Stay has to happen once, and then remain that way up thru the timer runs out.


#9

Thanks for your reply. To explain being left in the dark, there’s a motion sensor at the bottom of the stairs and another at the top. In the wee hours between 2am and 7am, my motion level is 20%, nomotion 0% and a duration of just 30 seconds ( I have 2 cats wandering around so I dont want the lights staying on for too long :slight_smile: )

If activate one sensor, leave its vicinity , the return to it within the 30 seconds, the countdown will continue to where level 0% is activated. The lights wont come on again until I reach the other sensor.

I’ve manage to test a work around using a LOGICAL OR expression of the motion status for both sensors. This seems to do the trick of restarting the countdown if I re-activate a sensor. It also cured that false activation mentioned. Hopefully this will be of use to someone with a similar issue. It maybe just more noticeable where I’m using a short duration.

Capture


#10

It is important to remember that just because we leave a room, does not meant that the motion is immediately seen as inactive. It takes time for the sensor to determine that the room is empty. Every device is different, but I have seen timeouts vary somewhere between 6 seconds, and 3 minutes. (with 1 minute being about the “norm”)

What this means is, if motion changes to active and you leave the area, it may take a moment for the sensor to report inactive. It is only after doing so, that it will see your next motion as CHANGING to active. (thereby, restarting the timer)


Pro Tip:

If you want to see your device’s timeout period for inactivity, you can make a simple test piston:

IF Sensor's motion changes to active
Then
    Log to console = "Active"
Else
    Log to console = "Inactive"
END IF

Once the test piston is running, just run past the sensor, and get out of sight as quickly as possible. Your logs will have two time stamps. The space between the two time stamps will show you how long it takes for a cleared room to register as inactive.

This knowledge will help you with future programming.


#11

I have mostly Smartthings motion sensors, I believe 2018/19 models. Using your Pro-Tip, these have come back with a 21 second timeout.

I have an Ikea Tradfri motion sensor (£6), the timeout on that was 3 minutes.

Thanks for your help, I look forward to the journey further into this subject.