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
Yep. Woke up this morning with 11 mails in my mailbox about this thread. Thought someone finally realized the brilliance 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.
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.
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
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 :)
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
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:
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
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
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