Rule example: Engine heater based on temperature

examples
Tags: #<Tag:0x00007fe05daf2950>

(Daniel Malmgren) #5

Doh. Fahrenheit. That’s weird stuff. It just hit me now though that I never mentioned in my post that I was actually talking Celsius. Added that information now :slight_smile:


(Eduardo Alexandre) #7

My simple sample of temperature rule…

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

	rule "temperature"
	when
        	Item temp changed
	then
		if(temp.state >= 25) {
		Fan.sendCommand(ON)
		}
	else if(temp.state < 25) { 
		Fan.sendCommand(OFF)
		}
end

And my PIR sensor rule:

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

var Timer timer = null

rule "Lamp"
when
        Item presence changed from CLOSED to OPEN
then
        Lamp.sendCommand(ON)
	sendMail("myemail@emailserver.com", "Front Door.", "You have guests on front door!", "http://mycamip/snapshot.jpg?user=admin&pwd=")
	sendTelegramPhoto("bot1", "http://mycamip/snapshot.jpg?user=admin&pwd=", null)
end

rule "Lamp OFF"
when
        Item presence changed from OPEN to CLOSED
then
	timer = createTimer(now.plusSeconds(6)) [|	
        	Lamp.sendCommand(OFF)
		timer - null
	]
end

(Todd Evans) #8

Attaching to this topic because it appears to be the most relevant… :slight_smile:
I get the concept of rules… I have a few based on time for turning on light, etc,
However, these rules seem to defy simple logic sometimes… and maybe due to my ignorance… so I apologize for that.
I have a temp sensor in the greenhouse (item=greenhousetemp) Been working and sending data for months)
I have a ZWave Wallplug (item=wallplug2) Also controllable via basic UI and/or iphone and android openhab app with no issue.
The wife wants the greenhouse heater (wallplug2) to turn on when equal or less than 40 and turn off at equal or greater than 45. (Winter tomatoes stuff)
Here is my rule, but does not work (my values are different right now for testing)
Log shows this:
2018-10-10 15:16:55.027 [vent.ItemStateChangedEvent] - greenhousetemp changed from 47.0 to 47.2
No coinciding event to show: Wallplug2 changed from OFF to ON
Here is the rule… where is my logic failing?

rule “greenhousetemperature”
when
Item greenhousetemp changed
then
if (greenhousetemp.state <= 48.0) {
wallplug2.sendCommand(ON)
}
if (greenhousetemp.state > >= 49.0) {
wallplug2.sendCommand(OFF)
}
end
PS I hate asking for help with such stupid, simple stuff :frowning:
Do I need an import of something??
Thanks
Todd


(Todd Evans) #9

I must add that this temperature value is gathered from MQTT on another R-Pi that the sensors reside on. I wouldn’t think that matters, though :slight_smile:


(Crispin) #10

A bit of a hijack :wink:

Fix the syntax and the rule should work. You’ve got an extra >
Add some debug logWarn to see if it enters the rule.


(Rich Koshak) #11

Probably would have been more appropriate to start a new thread.

In the future, when posting code How to use code fences.

I strongly recommend using VSCode to edit .rules files. See https://www.openhab.org/docs/configuration/editors.html#openhab-vs-code-extension. It will point out syntax errors. Failing that, always watch openhab.log when making changes to a .rules file. It will tell you if you have a syntax error.

As Crispin says, you have an extra > in your second if statement which is probably causing OH to reject the whole .rules file as syntactically incorrect.

VScode would have underlined that line in red as soon as you typed it.


(Todd Evans) #12

Thanks,
I Recently installed Visual Studio Code, installed the OpenHab extension but AM TRYING TO FIGURE OUT HOW TO CONNECT TO THE OPENHABIANPI share on my network… not seeing it.
I don’t think the extra > is the issue… that is just a recent typo while adding the <= value

I’ll look this up too: How to use code fences.
Thanks Rich.
Todd


(Todd Evans) #13

One more interesting thing… I always keep my “Tail” opened so I can see if the new edit I make causes an issue.
This rule reports NOTHING when ever I make a change to it… like it is going un-noticed!


(Rich Koshak) #14

Which log file are you tailing? events.log only shows Items change and command events (some Thing events too). To see errors and a log of what OH is doing you need to tail openhab.log. When you edit the .rules file and save you should at least be seeing “Loading model ‘rulesfilename.rules’” in openhab.log. If there are syntax errors you will see the error after that line and it will tell you what and where (row and column).

Be sure to copy and paste code is it is exactly as it appears in your posts. We can’t know whether a stray character is in the original or not.

Add a logInfo as the first line of the Rule to verify whether the Rule is executing. I would guess you have a syntax error here or elsewhere in the same .rules file. But if not this will show whether the rule is executing or not.

Is the Item named “wallplug2” or “Wallplug2”? Case does matter.


(Todd Evans) #15

Here is what I watch:
tail -F /var/log/openhab2/openhab.log /var/log/openhab2/events.log
Here is the Rule again
(greenhouseheater.rule)

logInfo
rule "greenhouseheater"
when 
	Item greenhousetemp changed
then
	if (greenhousetemp.state <= 45.5) {
		wallplug2.sendCommand(ON)
	} 
	if (greenhousetemp.state >= 49.0) {
		wallplug2.sendCommand(OFF)
	} 
end

Let me know what you think, Rich! :slight_smile:THanks
Todd


(Todd Evans) #16

To verify that my item is working, I switched it on then off again…
Here is the log for that:

2018-10-10 17:17:29.214 [vent.ItemStateChangedEvent] - Wallplug2 changed from OFF to ON

2018-10-10 17:17:32.222 [ome.event.ItemCommandEvent] - Item ‘Wallplug2’ received command OFF

2018-10-10 17:17:32.246 [vent.ItemStateChangedEvent] - Wallplug2 changed from ON to OFF

:sunglasses:


(Todd Evans) #17

DOH… maybe it IS a “W” thing
Let me change that :frowning:


(Todd Evans) #18

Well, bummer!
Still no go.

Maybe I’ll make another rule based on some other kind of event to assure that the Wallplug2 will take a command…
No fun! :slight_smile:


(Daniel Malmgren) #19

Yep. Woke up this morning with 11 mails in my mailbox about this thread. Thought someone finally realized the brilliance :smile: Actually I started using my engine heater again last week, starting to get cold in Sweden now.

I don’t know if it’s a copy-paste error, but the first row in the rule (“logInfo”) doesn’t seem to belong there. I think what you need to do though is to put a couple of logInfo() calls in the script to see where it actually executes and not. See https://www.openhab.org/docs/configuration/rules-dsl.html#scripts. And then just tail openhab.log.


(Vincent Regaud) #20

@toddwevans

Try that:

rule "greenhouseheater"
when 
    Item greenhousetemp changed
then
    if (greenousetemperature.state == NULL) return; // If no temp do nothing

    // Calculate states
    val temperature = greenhousetemperature.state as Number
    var wallPlugState
    if (wallplug2.state == NULL) {
        wallplug2.sendCommand(OFF) // Initialise wallplug2 to OFF just in case
        wallPlugState = OFF
    } else { 
        wallPlugState = wallplug2.state
    }

    if (temperature <= 45.5) {
       wallPlugState = ON
    } 
    if (temperature >= 49.0) {
        wallPlugState = OFF
    }

    // Do the command
    wallplug2.sendCommand(wallPlugState)
end

(Daniel Malmgren) #21

Trying to understand… Wouldn’t this mean that wallPlugState is only declared if wallplug2.state isn’t null? Shouldn’t the declaration of the variable be before the if? Also I guess wallplug.state should really be wallplug2.state.


(Vincent Regaud) #22

Yep, typos again
Corrected
Edge case, wallplug2.state != OFF and temp between 45.5 and 49 then variable not declared. You are correct young man


(Todd Evans) #23

Thanks gentlemen,
I will give this a try.
I’ve always been a “lazy” coder… only coding what I feel is pertinent in the script.
IE, I don’t care if the wallswitch is on or off… I just want to send a command when it is at a defined temperature.
If it is Null, then I don’t want to do anything BECAUSE that means ny sensor is broken… I’ll need to troubleshoot that.
Yes, a very simplistic approach and obviously deficient for this application :slight_smile:
But yes… this is what I must have missed:
" val temperature = greenhousetemperature.state as Number
var wallPlugState"
SO… with all of my ramblings “rambled” , I’ll give that entire block of Code a try and let you know.
If this works, I’ll have a template for future Rules :):+1:


(Todd Evans) #24

One note… I changed this:
val temperature = greenhousetemperature.state as Number
to this:
val temperature = greenhousetemp.state as Number
because there is no known item defined for greenhousetemperature.
Testing now :slight_smile:


(Todd Evans) #25

It appears that I will have to elaborate on my “stuff” here in an effort to troubleshoot this issue:
Here is the item for the greenhousetemp derived from the MQTT output of another Raspberry pi. The “other” pi runs a python script every minute to gather the info from the temperature sensor and publishes it to the MQTT server which is actually on the Openahbianpi device. This openhabianpi subscribes to the topic “greenhousetemp” which in turn takes that info and updates the home.sitemap file. This works wonderfully and has done so for a few months now, since I setup Openhab2.
home.items:

Number	 		greenhousetemp		"Greenhouse Temperature [%.1f f]" 		<temperature>		{ mqtt="<[openhabianpi:greenhousetemp:state:REGEX((.*?))]" }

This recieves this data every minute and shows in the openhab2 log:

2018-10-11 16:33:25.590 [vent.ItemStateChangedEvent] - greenhousetemp changed from 54.8 to 54.9

I have other rules running that turn on dimmable switches, etc based on the time of day… very simple. So I KNOW that rules are indeed functioning as expected.
From a ZWave perspective, this is the home.items file that defines the switch:

Switch 			Wallplug2 		"Greenhouse Heater" 			<heating>		(Switches)	{ channel = "zwave:device:512:node7:switch_binary" }

This is ALSO working as expected… I can turn this off and on via the IOS openhab app as well as from the basic or classic web link.
So… why is the rule not working… very strange to me.
I MUST be missing something, right?
Thanks again, guys!
Todd