Problem with while loop condition never exiting


#1

1) Give a description of the problem
I have a piston that is using a while loop based on a contact change condition, which is not working as expected.

2) What is the expected behaviour?
Essentially, this piston is used to remind me to give my son his medication. The piston runs once every day and:

  1. Checks to see if a contact sensor was opened or closed in the last two hours (this is the condition of for the while loop
  2. If the contact sensor was not opened or closed in the last 2 hours, triggers another sensor, which in turn will trigger an Alexa routine telling me that I have not opened his medication.
  3. The piston will sleep for 10 minutes, then check again and trigger the Alexa routine again.

3) What is happening/not happening?
The piston seems to always trigger the Alexa routine every 10 minutes, starting at the time when the piston is initially triggered by the schedule. This happens even though I’ve opened and closed the trigger contact sensor within the expected timeframe.

**4) Post a Green Snapshot of the piston![image|45x37]

5) Attach logs after turning logging level to Full
(PASTE YOUR LOGS HERE THEN HIGHLIGHT ALL OF THE LOGS AND CLICK ON THE </> ICON TO FORMAT THEM CORRECTLY)

REMOVE BELOW AFTER READING
If a solution is found for your question then please mark the post as the solution.


#2

It would be good to show logs of the execution.


#3

I copied yours and modified it a bit for testing and it worked like you expected… on Hubitat though.


#4

Interesting. In my case it did seem to work in the beginning, but the very next day it kept running the condition every 10 minutes. It’s fair to note that ‘Contact Sensor 4’ in my case is a virtual switch. I suspect that what is happening is that the condition is specifically checking for a change in the contact value (open and close) which could actually still be the same after 10 minutes since the sensor is always closed after being opened for a few seconds. Is there a way to change the while loop to check for the last timestamp for when the sensor received any action? That’s really what it should be doing. For example:

while
‘Contact Sensor 5’ is not updated in the last 2 hours

Thanks,

Julio


#5

You can try to use an Expression instead…

If age([SensorName:contact]) > 7200000 is true (verify the 7200000, it should be 2 hours in milliseconds)


#6

Awesome, I will give this a try.


#7

Hey @eibyer, I just tried this and it’s exactly what I needed. In addition, it’s opened up a whole bunch of possibilities for other things I can do with home automation. I wish I knew this before!

Thanks,
Julio


#8

I’m glad that helped. There are other functions in the webcore wiki that might be of use in the future.
https://wiki.webcore.co/Functions


#9

hi Julio,

I happen to found your Code, on the medication reminder that i intend to implement as well for my parents. Can you share with me the code that you have successfully been able to trigger the reminders after 2hours.

Cheers


#10

Hi Julio,

I have figured out the code and got it to work. Have shared code below so that other users may re-use it. :slight_smile:

Cheers