Rule example: Engine heater based on temperature

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.

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

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:

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

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:

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 Rules | openHAB. And then just tail openhab.log.

@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

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.

1 Like

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

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:

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:

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

Updated rule,
Ttry this one
I added some logging

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

    // Calculate states
    val temperature = greenhousetemp.state as Number
    var wallPlugState = OFF
    logInfo("greenhousetemp: ", temperature.toString)
    logInfo("wallPlugState", wallPlugState.toString)
    
    // We need to do this in case the temperature is between 45.5 and 49
    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
1 Like

Thanks for the assist

I’ll give this a try :slight_smile:

@vzorglub

Following your logic and example I am proposing to use this, to control my attic fan power

Look OK?




rule "Turn on Attic Fan when over 38 degrees at Night"
when
        Item AtticTemp changed
then
if (AtticTemp.state == NULL) return; // If Temp is errored, Do nothing

// Calculate states
val Temp = AtticTemp.state as Number
var AtticPower = AtticPower = OFF
logInfo("Attic Fan: ", Temp.toString)
logInfo("Attic Power", AtticPowerState.toString)


if (AtticPower.state == NULL) {
     AtticPower.sendCommand(OFF)  // Initialise Fan Power to OFF just in case
      AtticPowerState = OFF
} else {
     AtticPowerState = AtticPower.state
}
//Check the Attic Temp
     if (Temp.state => 35) {
                        AtticPower = ON
                        logInfo("Attic Fan", "Switching On due to High Temps and Sun down")
        }
     if (Temp.state <= 35) {
                        AtticPower = OFF
            logInfo("Attic Fan", "Switching off the Fan as its either day, or under 35 degrees")
        }
     AtticPower.sendCommand(AtticPower)

end


What is AtticPower?

It turns on and off the Attic Fan

You can’t name a variable the same as an item
What happens when the temperature is exactly 35?

OK, ill cahnge AtticPower to Power

I mean an item or a variable?