Sunday, December 20, 2015

openHAB and Fireplace Rules Part 5

Now that everything is humming along we can make some fancy rules. This includes creating a flexible timer and auto mode based on room temperature. I went through a lot of iterations but ended up with some nice rules that work well. I will post the current version here but any updates will be maintained on Github.





Timer

Items
 
Number  tempFire_TimerValue     "Fireplace Timer [%d min(s)]"    (gHeating)
Switch  tempFire_TimerSwitch    "Start Timer"     (gHeating)
DateTime tempFire_TimerStart    "Timer stated at [%1$tr]"        (gHeating)
Number tempFire_TimerElapsed    "Time elapsed [%d min(s)]"       (gHeating)
Number tempFire_TimerLeft       "Time remaining [%d min(s)]"     (gHeating)
 


Sitemap
 
 Setpoint item=tempFire_TimerValue minValue=5 maxValue=60 step=5
 Switch  item=tempFire_TimerSwitch
 Text item=tempFire_TimerStart   visibility=[tempFire_TimerSwitch==ON]
 Text item=tempFire_TimerElapsed visibility=[tempFire_TimerSwitch==ON]
 Text item=tempFire_TimerLeft    visibility=[tempFire_TimerSwitch==ON]

 


Rules
 
rule "Fireplace Timer"
when
        Item tempFire_TimerSwitch changed from OFF to ON
then
        val fireMin = (tempFire_TimerValue.state as DecimalType).intValue
        val counter = 0
        sendCommand(tempFire_Web, ON)
        postUpdate(tempFire_TimerStart, new DateTimeType())
        while(counter < fireMin){
                Thread::sleep(60000)
                counter = counter + 1
                fireMin = (tempFire_TimerValue.state as DecimalType).intValue
        }
        sendCommand(tempFire_TimerSwitch, OFF)
        sendCommand(tempFire_Web, OFF)
        //FireplaceTimer = createTimer(now.plusMinutes(fireMin))[
        //      sendCommand(tempFire_TimerSwitch, OFF)
        //      sendCommand(tempFire_Web, OFF)
        //]

end
rule "Stop timer"
when
        Item tempFire_TimerSwitch changed from ON to OFF
then
        sendCommand(tempFire_Web, OFF)
end

rule "Time left"
when
        Item tempFire_TimerSwitch changed from OFF to ON
then
        var start = now.millis
        while(tempFire_TimerSwitch.state==ON){
                val timerMin = (tempFire_TimerValue.state as DecimalType).intValue
                val dt1 = ((now.millis-start)/1000/60)
                postUpdate(tempFire_TimerElapsed, dt1)
                val dt2 = timerMin - dt1
                postUpdate(tempFire_TimerLeft, dt2)
                Thread::sleep(60000)

        }
end
  
 

I switched from using the timer function to writing my own so that I can update the time left if someone wants to add time to the timer. Beforehand, if you added time it wouldn't do anything. Now it checks every iteration. Much better.

Auto Mode

This mode allows you to just set a temperature and forget about it. It's very basic but it will try to maintain the room temperature using the senor node from the living room node

Items
 
Number  tempFire_Setpoint "Set Temperature [%.1f °F]"      (gHeating)
Switch  tempFire_AutoSwitch     "Auto Mode"       (gHeating)
 

Sitemap

Switch item=tempFire_AutoSwitch
Setpoint item=tempFire_Setpoint minValue=60 maxValue=75 step=1
Here you can limit how hot someone could set it.

Rules

rule "Fireplace Auto Control"
when
        Time cron "0 0/5 * 1/1 * ? *" OR
        Item temp_livingroom received update
then
        if(tempFire_AutoSwitch.state == ON){
                if(tempFire_Web.state==ON){
                        if(temp_livingroom.state >= tempFire_Setpoint.state){
                                tempFire_Web.sendCommand(OFF)
                        }
                }
                else if(temp_livingroom.state < tempFire_Setpoint.state){
                                tempFire_Web.sendCommand(ON)
                }
        }
        else {
        }

end

rule "Start auto mode"
when
        Item tempFire_AutoSwitch changed from OFF to ON
then
        tempFire_Web.sendCommand(ON)
end

rule "Stop auto mode"
when
        Item tempFire_AutoSwitch changed from ON to OFF
then
        if(tempFire_TimerSwitch.state == OFF){
                tempFire_Web.sendCommand(OFF)
        }
end

I might rewrite this so that it starts a while loop when you set the Auto Control from Off to On instead of a CRON that is always checking. I also should add a max run time. Just in case you forget it and leave the house or go to bed. Ooo, I could use a NFC tag on my bedside to start bed mode.

Hopefully that will get you started so you can adapt it to your own set-up. Please comment below if it helped you out.

No comments:

Post a Comment