Rule example: Engine heater based on temperature

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?

Name your variables using camel case. Look it up.

Like so?



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 power = 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) {
                        power = ON
                        logInfo("Attic Fan", "Switching On due to High Temps and Sun down")
        }
     if (temp.state <= 35) {
                        power = OFF
            logInfo("Attic Fan", "Switching off the Fan as its either day, or under 35 degrees")
        }
     AtticPower.sendCommand(power)

end

var OnOffType power = OFF


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 OnOffType power = 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) {
                        power = ON
                        logInfo("Attic Fan", "Switching On due to High Temps and Sun down")
        }
     if (temp.state <= 35) {
                        power = OFF
            logInfo("Attic Fan", "Switching off the Fan as its either day, or under 35 degrees")
        }
     AtticPower.sendCommand(power)

end


What is AtticPowerState?

AtticPower is the item to turn the sonoff switch, it turns the fan on/off

AtticPowerState

Replace => 35 with >= 36
And <= 35 with <= 34