SOLVED - Greenhouse Rules For Fans and Shutters

I’m working on a fairly large greenhouse project that will require manipulation of some fans and shutters.

I am installing a new OpenHab 2 box. I am using Sonoff TH16 that has been flashed with Tasmota.

I am going to need some help with this.

I will need rules that run and will operate them at a certain temperature and then turn off at a certain temperature. i would like the shutters to open first for say like 30 seconds and then the fans would go on. and then reverse that when they close.

i would like an easy way i can adjust that temperature that they turn on and off at.

i would also like to have a switch that i can control the fans and shutters separately.

i am good with the wiring and setting up openhab and connecting the mqtt etc. i just really dont know how to do the rules. someone willing to help me out?

i have read through some of the posts on this and might be able to cobble something together but i really need to make sure this works correctly. i dont want it to be just my cobbled code.

thanks!

We can’t really help you if you don’t post your code. No one here is going to code the rules for you, but we can all assist you along the way. You can start with these readings:

  1. Concept
  2. How to create rules
  3. How to ask for assistance

Thanks Lucky that helps. I’ll get a start on it and then post what I figure out.

Thanks!

Sounds like you need the basic-most programming skills. As already stated, please don’t expect anyone to jump in.

To get you started:
Create switch items for the fans and number items for the max and min threshold temperatures.
Add them to your sitemap so you can manually switch and change them.
Create rules to trigger on temperature updates, use a timer or the expire binding to auto-switch off fans after a specific period of time.

Thanks Markus, a programmer i am not. Just as you stated, I am going to set them as a switch and turn them on and off in the panel. That I can figure out. Already do that at the house with my lights etc. It will be the rules part that will be tricky for me but i’ll see what I can do.

Thanks!

This is what I came up with for a rule set. Just pretty simple to start with (well simple for some) to see if I can get this to work. This would be a big help right out of the gate. Does this look like its coded correctly?

import org.openhab.model.script.actions.*
import org.openhab.model.script.actions.Timer

	rule "temperature"
	when
        	Item temp changed
	then
		if(temp.state >= 76) {
		Shutter1.sendCommand(ON)
		}
	else if(temp.state < 76) { 
		Shutter1.sendCommand(OFF)
		}
		
		when
        	Item temp changed
	then
		if(temp.state >= 78) {
		Shutter2.sendCommand(ON)
		}
	else if(temp.state < 78) { 
		Shutter2.sendCommand(OFF)
		}
		
		when
        	Item temp changed
	then
		if(temp.state >= 79) {
		LowFan1.sendCommand(ON)
		}
	else if(temp.state < 76) { 
		LowFan1.sendCommand(OFF)
		}
		
		when
        	Item temp changed
	then
		if(temp.state >= 85) {
		LowFan2.sendCommand(ON)
		}
	else if(temp.state < 85) { 
		LowFan2.sendCommand(OFF)
		}
		
		when
        	Item temp changed
	then
		if(temp.state >= 90) {
		HighFan.sendCommand(ON)
		}
	else if(temp.state < 90) { 
		HighFan.sendCommand(OFF)
		}
end

thanks!

You mustn’t repeat the part

  when
            	Item temp changed
    	then

Also, omit the “if (…) { … }” part after “else” because there you check for the opposite of the previous “if” which is exactly what the “else” part already means.

Thanks Markus! so something more like this? would i better off to create rules for each item?

or does that really matter…

import org.openhab.model.script.actions.*
import org.openhab.model.script.actions.Timer

	rule "temperature"
	when
        	Item temp changed
	then
		if(temp.state >= 76) {
		Shutter1.sendCommand(ON)
		}
	else  { 
		Shutter1.sendCommand(OFF)
		}
				
		if(temp.state >= 78) {
		Shutter2.sendCommand(ON)
		}
	else  { 
		Shutter2.sendCommand(OFF)
		}
			
		if(temp.state >= 79) {
		LowFan1.sendCommand(ON)
		}
	else  { 
		LowFan1.sendCommand(OFF)
		}
		
		if(temp.state >= 85) {
		LowFan2.sendCommand(ON)
		}
	else  { 
		LowFan2.sendCommand(OFF)
		}
		
		if(temp.state >= 90) {
		HighFan.sendCommand(ON)
		}
	else  { 
		HighFan.sendCommand(OFF)
		}
end

What do you mean for each item?
This rule is triggered by only one item: temp
Then in the then section you can do as many things as you like

You don’t need the import section

And I have tidied up a bit:

rule "temperature"
    when
        Item temp changed
    then
        if (temp.state >= 76) {
            Shutter1.sendCommand(ON)
        } else  { 
            Shutter1.sendCommand(OFF)
        }
		
        if (temp.state >= 78) {
            Shutter2.sendCommand(ON)
        } else { 
            Shutter2.sendCommand(OFF)
        }
			
        if (temp.state >= 79) {
            LowFan1.sendCommand(ON)
        } else { 
            LowFan1.sendCommand(OFF)
        }
		
        if (temp.state >= 85) {
            LowFan2.sendCommand(ON)
        } else { 
            LowFan2.sendCommand(OFF)
        }
		
        if (temp.state >= 90) {
            HighFan.sendCommand(ON)
        } else { 
            HighFan.sendCommand(OFF)
        }
end

Thanks Vincent! appreciate the help! i’m going to head over to the greenhouse to install this tonight. (vpn on a future project list). when said item it was per item like shutter 1, shutter 2, etc. I think the way this is laid out makes a lot of sense. Not sure what else i need to add unless at some point i just want to open the shutter and run the fan to do a quick circulation. but that i could do with another rule. Not crucial now. If i can just get things on and off with this it will be a great start! i have to use my phone to check now and then and then turn things on and off.

i will do some research too as it would be good to know for awhile if something goes on and off. sure their is a way to do that…

thanks! will folllow up with how it works.

Cool,
Do you understand the need to have a tidy code?
It make it much easier to read not only for yourself but others too.
Also it’s good to get good habit from the beginning.
I used 4 spaces for the indenting. There are different school of thought regarding indentation. Chose one and stick to it.

Thanks Vincent!

I loaded the code. I updated the items to actually match my item names as i did not have those when i was typing this at home. i was wondering if i would run into an issue and it looks like i have. right now its 70 in here so everything should be closed up according to the rule. so now to check my naming in the rule…

for the temp state, this is my item code:

String Indoor_State "[%s]" (startpersist) {mqtt="<[broker:stat/sonoff-00th161/POWER:state:default]"} 
Number Indoor_Temperature "Indoor Temperature [%.1f °F]" <temperature> ["CurrentTemperature"] {mqtt="<[broker:tele/sonoff-00th161/SENSOR:state:JSONPATH($.AM2301.Temperature)]"}

I used Indoor_Temperature for the temp:state in my rule. so Indoor_Temperature.state

this would be the example for the shutter item:

Switch Shutter1  "Shutter 1" <switch> [ "Switchable"]

I used Shutter1 in the rule.

Does that all look correct? Theres nothing you really need to do to get a rule to run other than load it up. I did reboot…

Adding on, for a rule, if i want to override and turn a fan on will the rule keep checking and shut it down based on the temp? Not sure how rules work as far as times they run or dont…

Add a logInfo in you rule to check if you are getting data:

rule "temperature"
    when
        Item Indoor_Temperature changed
    then
        logInfo("Temp Rule",triggeringItem.state.toString)
        if (triggeringItem.state >= 76) {

Then change the temperature and check your logs. Both: events.log AND openhab.log

Thanks Vincent, reposting what have in the rule. In the log I am getting this error:

2018-03-20 18:20:14.902 [INFO ] [el.core.internal.ModelRepositoryImpl] - Loading model 'greenhouse.rules'

2018-03-20 18:20:14.921 [WARN ] [el.core.internal.ModelRepositoryImpl] - Configuration model 'greenhouse.rules' is either empty or cannot be parsed correctly!

2018-03-20 18:20:15.077 [WARN ] [el.core.internal.ModelRepositoryImpl] - Configuration model 'greenhouse.rules' has errors, therefore ignoring it: [9,5]: mismatched input 'when' expecting '}'

[10,14]: no viable alternative at input 'Indoor_Temperature'

this is what my rule looks like:

rule "temperature"
    
	when
        Item Indoor_Temperature changed
    then
        logInfo("Temp Rule",Indoor_Temperature.state.toString)
        if (Indoor_Temperature.state >= 62) {
	
    when
        Item Indoor_Temperature changed
    then
        if (Indoor_Temperature.state >= 76) {
            Shutter1.sendCommand(ON)
        } else  { 
            Shutter1.sendCommand(OFF)
        }
		
        if (Indoor_Temperature.state >= 78) {
            Shutter2.sendCommand(ON)
        } else { 
            Shutter2.sendCommand(OFF)
        }
			
        if (Indoor_Temperature.state >= 79) {
            LowFan.sendCommand(ON)
        } else { 
            LowFan.sendCommand(OFF)
        }
		
        if (Indoor_Temperature.state >= 85) {
            SingleSpeedFan.sendCommand(ON)
        } else { 
            SingleSpeedFan.sendCommand(OFF)
        }    
end

You have two when statements
It should be:

rule "temperature"
when
    Item Indoor_Temperature changed
then
    logInfo("Temp Rule",Indoor_Temperature.state.toString)
    if (Indoor_Temperature.state >= 76) {
        Shutter1.sendCommand(ON)
    } else  { 
        Shutter1.sendCommand(OFF)
    }

    if (Indoor_Temperature.state >= 78) {
        Shutter2.sendCommand(ON)
    } else { 
        Shutter2.sendCommand(OFF)
    }
			
    if (Indoor_Temperature.state >= 79) {
        LowFan.sendCommand(ON)
    } else { 
        LowFan.sendCommand(OFF)
    }
		
    if (Indoor_Temperature.state >= 85) {
        SingleSpeedFan.sendCommand(ON)
    } else { 
        SingleSpeedFan.sendCommand(OFF)
    }    
end

Yes it will, rules run whenever the trigger condition is met that you specify in the when … part. Btw, you can also specify when X or Y or Z, see here for the rules specification.

hey, hey, hey! it works! how cool! literally and figuratively! thanks soooo much for the help! this is excellent!

ok, now to work on some alerting and monitoring!

I would also remove your hard coded values from the body of the code to constants declaration at the top of the code. This way you wont have to change them in so many different places in your code.

Thanks Lucky, that sounds like a great idea but not something I would understand how to do. I can do some research though!