[SOLVED] Thermostat Rule Help (gpio, dht22, exec binding, things, items, rules)

Alright… I got this to work, but was still getting undesired results. This whole experience has been tough being that I previously knew very little about programming. More of the hands on kind of guy.

That said, I’d like to thank the OH Community for the help I’ve received… without the community this adventure would have come to a halt quickly. Again, thanks for being patient, pointing me in the right direction, and forcing me to think!

My solution was to remove the temptimer and replaced with the expire binding. Thanks @vzorglub for suggesting I use the expire binding for the lockout… it’s what led me to use it for other purposes. Works great!

Now… I would like to keep track of the hours that the AC is on for the day, week, and month. I found this thread…Generator hours counter, but noticed it’s pretty old. I get that I would need to have some variables to keep track of when the ac turned on and when it turned off. What I’m not sure of is whether or not I’d have to use any of the imports??

The only import you may still need is: import java.lang.Math
The rest has now been implicit with OH 2.x

Use the code in post 2441th

Got it. What’s up with the lock.lock stuff and do I need it?

Probably not
It’s used to prevent the same rule running at the same time when there is a rule that take a while to execute and a trigger may happen when the rule is still running.
In your case your shouldn’t need it. In the other post he was using a rule to start a generator or something that took a while to get up to speed and so had to prevent the rule being run twice.

Understood. Thanks for the explanation. I’ll see what I can do… appreciate it.

Thanks Vincent for pointing me in the right direction!

Update… I got the “switch item on/off tracking” to work.

At first I adjusted this Generator hours counter to fit my needs. After thinking about it for a few days I concluded that it was overly complex for my situation and I didn’t like the cron jobs constantly checking the states.

So I took this Calculated daily run time of a switch and modified to accomplish my needs. Note… this part >> var Number execDuration = Seconds::secondsBetween(new DateTime(prevOnState.calendar.timeInMillis), now ).getSeconds() doesn’t work in the newest version of OH. Needs to change the bolded part to this >> toInstant.toEpochMilli

Also, no imports are needed for it to work.

@vzorglub Do you know if the expire binding will work for a duration of a year?

@bobby_wayne
I don’t think so. Anyway your system will probably restart more than one in that period so it wouldn’t matter annyway
What are you trying to do?

Get a notification / email a year after a reed switch changes from closed to open.

I think the best way would be to put the time the switch changed to open in an item.
For example:

rule "Entry Doors Update"
when
    reedSwitch changed from CLOSED to OPEN
then
    Thread::sleep(100) // give persistence time to catch up
    if (previousState == NULL) return;
    reedSwitch_LastOpen.postUpdate(now.toString)
end

Your new item will be:

DateTime reedSwitch_LastOpen

And it needs to be persisted and set to restoreOnStartUp to survive a reboot

And then a rule that fires everyday to check if the time now is more than one year after the saved time and fire out an email. I don’t have that code yet. Maybe you have a go and we’ll find out together

@vzorglub

Here is what I’d like to do… I have a contact attached to a filter base. When the filter base is opened, a date time item gets set. I also have a number item used for a selection… 2=2 months, 3=3 months, 6=6 months, 12=12 months. I’d like to have OH check if now is after date time months plus number item (selection on sitemap). If not, check the following Friday. If yes, then send me an email saying that the filter needs to be changed.

Here’s what I have thus far…

.items

Contact Filter_Contact  // Reed switch
DateTime Changed_DateTime  // Gets DateTime that Filter_Contact changed from Closed to Open
Number Change_Period  // Displayed on sitemap using selection.  Gives ability to adjust the duration.. for instance 2=2 months 3=3months 6=6 months 12=12 months (using transformation map)

.rules
// This is just for testing. Am I right to assume that this will also work for now.minusDays and now.minusYears??

rule "Show Filter_Changed switch"
when
        Time cron "0 0/1 * 1/1 * ? *"
then
        val DateTime filterChanged=Changed_DateTime.state
        logInfo("Filter", "Val filterChanged has " + filterChanged + " state")
        val Number filterPeriod=Change_Period.state as Number
        logInfo("Filter", "Val filterMinutes has " + filterPeriod + " state")
        if(now.minusMinutes(filterPeriod).isAfter(filterChanged))
                {
                logInfo("Filter", "It works...you can sendMail now!!!")
                }
end

log

Error during the execution of rule 'Filter Change' An error occurred during the script execution: Could not invoke method: org.joda.time.DateTime.minusMinutes(int) on instance: 2018-08-27T22:10:00.036-05:00

I’ve searched the forum and google for a solution, but I’m not sure if I’m even searching the right topic?!?

if(now.minusMinutes(filterPeriod.intValue).isAfter(filterChanged))

Still no joy…

.rules

Time cron "0 0/1 * 1/1 * ? *"
then
        
        val DateTime filterChanged=Filter_DateTime.state
        logInfo("Filter", "Val filterChanged has " + filterChanged + " state")
       
        val Number filterPeriod=Filter_Period.state as Number
        logInfo("Filter", "Val filterMinutes has " + filterPeriod + " state")
        
        if(now.minusMinutes(filterPeriod.intValue).isAfter(filterChanged)) {
                logInfo("Filter", "It works...")
                }
end

logged error…

2018-08-28 11:15:00.034 [INFO ] [clipse.smarthome.model.script.Filter] - Val filterChanged has 2018-08-28T00:47:49.987-0500 state
2018-08-28 11:15:00.044 [INFO ] [clipse.smarthome.model.script.Filter] - Val filterMinutes has 6 state
2018-08-28 11:15:00.058 [ERROR] [ntime.internal.engine.ExecuteRuleJob] - Error during the execution of rule 'Show Filter_Changed switch': An error occurred during the script execution: Could not invoke method: org.joda.time.base.AbstractInstant.isAfter(org.joda.time.ReadableInstant) on instance: 2018-08-28T11:09:00.049-05:00

@vzorglub After hours of searching and trial / error… I finally found a solution! Here it is…

Now I am curious… let’s say that I change the now.minusMinutes to now.minusMonths and the period is set to 3 months. I change the filter November 1st. So the next due date would be February 1st. Would this solution still work?

 rule "Test | Unhide Filter Changed switch"
when
        Time cron "0 0/1 * 1/1 * ? *"
then

        val filterChanged=new DateTime((Filter_DateTime.state as DateTimeType).zonedDateTime.toInstant().toEpochMilli)
        logInfo("Filter", "Val filterChanged has " + filterChanged + " state")

        val Number filterPeriod=Filter_Period.state as Number
        logInfo("Filter", "Val filterMinutes has " + filterPeriod + " state")

        if(now.minusMinutes(filterPeriod.intValue).isAfter(filterChanged)) {
                logInfo("Filter", "It works...showing Change Filter switch")
                postUpdate(vFilter_Change_Due,ON)
                }else{
                logInfo("Filter", "Time isn't up...")
                }

end

log

2018-08-28 14:52:17.161 [INFO ] [clipse.smarthome.model.script.Filter] - Contact opened... posting DateTime
2018-08-28 14:53:00.024 [INFO ] [clipse.smarthome.model.script.Filter] - Val filterChanged has 2018-08-28T14:52:17.165-05:00 state
2018-08-28 14:53:00.040 [INFO ] [clipse.smarthome.model.script.Filter] - Val filterMinutes has 12 state
2018-08-28 14:53:00.052 [INFO ] [clipse.smarthome.model.script.Filter] - Time isn't up...
2018-08-28 14:54:00.021 [INFO ] [clipse.smarthome.model.script.Filter] - Val filterChanged has 2018-08-28T14:52:17.165-05:00 state
2018-08-28 14:54:00.046 [INFO ] [clipse.smarthome.model.script.Filter] - Val filterMinutes has 12 state
2018-08-28 14:54:00.056 [INFO ] [clipse.smarthome.model.script.Filter] - Time isn't up...
2018-08-28 14:55:00.039 [INFO ] [clipse.smarthome.model.script.Filter] - Val filterChanged has 2018-08-28T14:52:17.165-05:00 state
2018-08-28 14:55:00.052 [INFO ] [clipse.smarthome.model.script.Filter] - Val filterMinutes has 12 state
2018-08-28 14:55:00.059 [INFO ] [clipse.smarthome.model.script.Filter] - Time isn't up...
2018-08-28 14:56:00.022 [INFO ] [clipse.smarthome.model.script.Filter] - Val filterChanged has 2018-08-28T14:52:17.165-05:00 state
2018-08-28 14:56:00.034 [INFO ] [clipse.smarthome.model.script.Filter] - Val filterMinutes has 12 state
2018-08-28 14:56:00.044 [INFO ] [clipse.smarthome.model.script.Filter] - Time isn't up...
2018-08-28 14:57:00.028 [INFO ] [clipse.smarthome.model.script.Filter] - Val filterChanged has 2018-08-28T14:52:17.165-05:00 state
2018-08-28 14:57:00.048 [INFO ] [clipse.smarthome.model.script.Filter] - Val filterMinutes has 12 state
2018-08-28 14:57:00.059 [INFO ] [clipse.smarthome.model.script.Filter] - Time isn't up...
2018-08-28 14:58:00.023 [INFO ] [clipse.smarthome.model.script.Filter] - Val filterChanged has 2018-08-28T14:52:17.165-05:00 state
2018-08-28 14:58:00.034 [INFO ] [clipse.smarthome.model.script.Filter] - Val filterMinutes has 12 state
2018-08-28 14:58:00.043 [INFO ] [clipse.smarthome.model.script.Filter] - Time isn't up...
2018-08-28 14:59:00.023 [INFO ] [clipse.smarthome.model.script.Filter] - Val filterChanged has 2018-08-28T14:52:17.165-05:00 state
2018-08-28 14:59:00.035 [INFO ] [clipse.smarthome.model.script.Filter] - Val filterMinutes has 12 state
2018-08-28 14:59:00.046 [INFO ] [clipse.smarthome.model.script.Filter] - Time isn't up...
2018-08-28 15:00:00.033 [INFO ] [clipse.smarthome.model.script.Filter] - Val filterChanged has 2018-08-28T14:52:17.165-05:00 state
2018-08-28 15:00:00.044 [INFO ] [clipse.smarthome.model.script.Filter] - Val filterMinutes has 12 state
2018-08-28 15:00:00.062 [INFO ] [clipse.smarthome.model.script.Filter] - Time isn't up...
2018-08-28 15:01:00.030 [INFO ] [clipse.smarthome.model.script.Filter] - Val filterChanged has 2018-08-28T14:52:17.165-05:00 state
2018-08-28 15:01:00.042 [INFO ] [clipse.smarthome.model.script.Filter] - Val filterMinutes has 12 state
2018-08-28 15:01:00.051 [INFO ] [clipse.smarthome.model.script.Filter] - Time isn't up...
2018-08-28 15:02:00.043 [INFO ] [clipse.smarthome.model.script.Filter] - Val filterChanged has 2018-08-28T14:52:17.165-05:00 state
2018-08-28 15:02:00.057 [INFO ] [clipse.smarthome.model.script.Filter] - Val filterMinutes has 12 state
2018-08-28 15:02:00.066 [INFO ] [clipse.smarthome.model.script.Filter] - Time isn't up...
2018-08-28 15:03:00.023 [INFO ] [clipse.smarthome.model.script.Filter] - Val filterChanged has 2018-08-28T14:52:17.165-05:00 state
2018-08-28 15:03:00.038 [INFO ] [clipse.smarthome.model.script.Filter] - Val filterMinutes has 12 state
2018-08-28 15:03:00.049 [INFO ] [clipse.smarthome.model.script.Filter] - Time isn't up...
2018-08-28 15:04:00.032 [INFO ] [clipse.smarthome.model.script.Filter] - Val filterChanged has 2018-08-28T14:52:17.165-05:00 state
2018-08-28 15:04:00.046 [INFO ] [clipse.smarthome.model.script.Filter] - Val filterMinutes has 12 state
2018-08-28 15:04:00.056 [INFO ] [clipse.smarthome.model.script.Filter] - Time isn't up...
2018-08-28 15:05:00.028 [INFO ] [clipse.smarthome.model.script.Filter] - Val filterChanged has 2018-08-28T14:52:17.165-05:00 state
2018-08-28 15:05:00.042 [INFO ] [clipse.smarthome.model.script.Filter] - Val filterMinutes has 12 state
2018-08-28 15:05:00.054 [INFO ] [clipse.smarthome.model.script.Filter] - It works...showing Change Filter switch

If it works with plusMinutes I see no reason why it shouldn’t work with plusMonths

Got ya.

More specifically though, would it work if the year goes to the next? I ask because I’m not sure if the “method” accounts for the change in year. For example… I set the period to 3 months and change the filter on 10/1/2018. So the next change would be 1/1/2019.

Does the plusMonths method look at the DateTime as a whole? Knowing that 1/1/2019 is 3 months after 10/1/2018. Please excuse my ignorance!

Yes it would work the dates are absolute

Ok. I thought it would, but wasn’t sure. Thanks for clarifying and your help, Vincent!

Hey,

Thanks for posting your code! I’m working on doing something similar but using it to build a PID controller. I can’t seem to get my sitemap to work properly. Do you mind sharing your sitemap?

Thanks!

So I got my Sitemap up and going but I cannot seem to figure out how to get it to display the temp and humidity correctly. I get the following error…

I’m running the latest Rasbian and Openhab. I also installed the exec binding and GPIO.

>      File "/etc/openhab2/scripts/hum.py", line 10, in <module>
>         humidity, temperature = Adafruit_DHT.read_retry(sensor, pin)
>       File "/usr/local/lib/python2.7/dist-packages/Adafruit_DHT-1.4.0-py2.7-linux-armv6l.egg/Adafruit_DHT/common.py", line 94, in read_retry
>       File "/usr/local/lib/python2.7/dist-packages/Adafruit_DHT-1.4.0-py2.7-linux-armv6l.egg/Adafruit_DHT/common.py", line 81, in read
>       File "/usr/local/lib/python2.7/dist-packages/Adafruit_DHT-1.4.0-py2.7-linux-armv6l.egg/Adafruit_DHT/Raspberry_Pi_2.py", line 34, in read
>     RuntimeError: Error accessing GPIO.

Thanks!