Previous Posts:
Epidode 1: Forget Me Not: Application
Episode 2: Forget Me Not: The Best Laid Plans ...
Episode 3: Forget Me Not: Setup EnOcean with openHAB
I was notified of a possible delay in shipment from Newark, so I have worked on the Medication Reminder System in the mean time. It took a while to work through the examples on https://github.com/openhab/openhab/wiki/Samples-Rules to learn about setting up rules. My rules system can be summarized as follows:
1. Initialize System - Set The rocker switch states
This part is extremely important. Even though I can load the openHab interface and see the state of the switches are not ON it doesn't mean the states of the switches are OFF. There must be a tri-state option? In demo.rules I have this rule for the system startup.
rule "System Start" //demo.rules when System started then Rocker1B.setState(OFF) Rocker1A.setState(OFF) medicationTakenFlag = 0 //Flag set when medication taken end
Note: In demo.items, my rocker switches are defined as:
Switch Rocker1A {enocean="{id=00:29:E4:66, eep=F6:02:01, channel=A}"} Switch Rocker1B {enocean="{id=00:29:E4:66, eep=F6:02:01, channel=B}"}
2. Set the medicationTakenFlag when I take the medication
If the Rocker Switch is pressed, the medicationTakenFlag gets set.
//Set the medication flag when I take my medication rule "Medication Taken" when Item Rocker1A changed from OFF to ON then medicationTakenFlag = 1 end
3. Attempt to leave the home without taking medication
I have mounted a magnetic contact switch on my front door and named it EnOcean_sensor_CONTACT2. If I attempt to leave the home without having set the medicationTakenFlag (pressing the rocker switch) my Raspberry Pi will notify me with a voice command "Take Medication". This should only occur after 4 AM, because before that would just be annoying.
//Haven't taken medication and leave the apartment rule "Remind Me While I am Leaving" when Item EnOcean_sensor_CONTACT2 changed from CLOSED to OPEN then var Number hour = now.getHourOfDay if ( (Rocker1A.state == OFF) && (hour > 4) ) { //Rocker1B.setState(ON) say("Take Medication") } end
4. Reset the rocker switch everyday at 3:59 AM.
This should be one of the easiest settings, but I had to learn how cron works in Linux to setup the check.Have a look here for the details Quartz Scheduler | Documentation | Tutorial Lesson 06. It's still a bit confusing. The code below should turn off the rocker switch and reset the medicationTakenFlag at 3:59:00 AM everyday.
//Reset medication flag every night at 03:59 rule "Reset me" when Time cron "0 59 03 * * * " //03:59:00 AM everyday then sendCommand(Rocker1A, OFF) medicationTakenFlag = 0; end
5. The last rule is a little trickier. What if it's the weekend or I have the day off? I still need to be reminded if I didn't take the medication.
This last rule reminds me every 15 minutes between 8AM and 11AM if medicationTakenFlag is not set that I should take the medication. I will come up with a better way to do this one eventually because this could get really annoying.
rule "Remind Me and I Haven't Left" when Time cron "* */15 * * * ?" then var Number hour = now.getHourOfDay if ((hour > 8) && (hour < 11) && (medicationTakenFlag == 0)) say("Take Medication") end
That's the medication system so far. Next I might try to get Notify My Android to notify our smart devices at home as well as the voice command to take medication.
Things I learned this week:
- Initialize switch states, holy moly this can cause you a lot of grief!
- The smart phone apps respond a lot quicker than the web interface. I was watching the web interface waiting for a rocker switch state to change but it didn't change until I reloaded the interface. The app responds immediately.
- If you are working in openHab designer and you want to see the autofill options, ie you type Rocker1A. and you want to see the parameter options, hit ctrl-space.