[SOLVED] Trying to set up If Then with Outdoor Temperature

  • Platform information: Windows 10 Prof
    • Hardware: CPUArchitecture/RAM/storage i686/16GB
    • OS: what OS is used and which version Windows 10 Prof
    • openHAB version: 2.4
  • Issue of the topic: please be detailed explaining your issue

Ok, I know I am a newbie, but after three weeks of studying, it would look like I could figure it out. But, no, I was wrong. It probably is simple, but it eludes me.

What I am trying to do, is to turn a light on whenever the temperature goes below 30° and off again when the temperature goes above 33°. I believe I have the temperature set right, but I can only get it to work sometimes, and then only one way.

I think I have used the code fences correctly, but again, I could have messed it up. Any help would be much appreciated.

  • Items configuration related to the issue
Number:Temperature          WC_PWS_Temperature
                            "Temperature [%.0f %unit%]"
                            <temperature>
                            { channel="weathercompany:weather:local:pwsObservations#currentTemperature" }
  • Rules code related to the issue
var Number WC_PWS_Temperature
var Number currentTemperature

rule "Loginfo every 10 minutes"
when
    Time cron "0 */10 * ? * *"
then
	
			if(currentTemperature > 33){
				OfficeDeskLight.sendCommand(OFF)
				}
			logInfo("currentTemperature", "> 33° = OFF " + WC_PWS_Temperature)	

			if(currentTemperature < 30){
				OfficeDeskLight.sendCommand(ON)
				}
			
			logInfo("currentTemperature", "> 30° " + WC_PWS_Temperature) 
			logInfo("Minute", "Another 10 minutes has passed.")

end
  • Services configuration related to the issue
  • If logs where generated please post these here using code fences:
2019-10-22 20:00:00.004 [INFO ] [home.model.script.currentTemperature] - > 33° = OFF WC_PWS_Temperature (Type=NumberItem, State=27 °F, Label=Temperature, Category=temperature)
2019-10-22 20:10:00.389 [INFO ] [home.model.script.currentTemperature] - > 30° WC_PWS_Temperature (Type=NumberItem, State=27 °F, Label=Temperature, Category=temperature)
2019-10-22 20:10:00.389 [INFO ] [g.insteonplm.InsteonPLMActiveBinding] - Item: OfficeDeskLight got command ON
rule “Loginfo every 10 minutes”
when
Time cron “0 */10 * ? * *”
then
    if(WC_PWS_Temperature.state > 33) {
        OfficeDeskLight.sendCommand(OFF)
        logInfo("currentTemperature", "> 33° = OFF " + WC_PWS_Temperature.state)	
    }
    if(WC_PWS_Temperature.state < 30) {
        OfficeDeskLight.sendCommand(ON)
        logInfo("currentTemperature", "> 30° " + WC_PWS_Temperature.state) 
    }
    logInfo("Minute", "Another 10 minutes has passed.")
}

I did not check, but I hope the idea will be clear

PS Perhaps so:
var Number currentTemperature= WC_PWS_Temperature.state as DecimalType
if(currentTemperature > 33) {

Why are you using a cron trigger?
By using a changed trigger on the tenperatue item the rule would run in each temperature change.

wondering what for is turning light on/off at certain temperature… there are some really weird implementation out there :smiley:

Unfortunately the latter. See How to use code fences. It looks like you are trying to use

'``

You need to use

```

Do not name variables the same as your Item name.

Why not trigger when WC_PWS_Temperature changes?

You never set currentTemperature to anything. Where is it supposed to get it’s value? Presumably what you want is the state of WC_PWS_Temperature. But you’ve overridden that name in the Rule by redefining it as a variable.

Finally, WC_PWS_Temperature is a Number:Temperature meaning it has a Unit of Measurement so using it in a Rule is a little different. See Rules | openHAB and scroll down to the QuantityTypes section.

rule "Control lamp by temp"
when
    Item WC_PWS_Temperature changed
then
    if(WC_PWS_Temperature.state > 33|°F) {
        OfficeDeskLight.sendCommand(OFF)
    }
    else if (WC_PWS_Temperature.state < 30|°F) {
        OfficeDeskLight.sendCommand(ON)
    }
end

Assuming my guess is correct and OP is using °F, this Rule will turn on a light when the temperature is below freezing which could be a handy indicator if you need to do something special in that situation. Though I really expect this is the equivalent of “hello world”; something simple to figure out how to write Rules.

1 Like

Thanks for all the help. I imagine I misused much in my programming. I will try out what was said tomorrow, as this has been an awful long day.

Rich, I may have used the wrong punctuation marks. Sorry, I will check better in the future.

There have been enough suggestions that I will have to try them out tomorrow. BTW, I was aware that using a temperature change to initiate my rule, would work, although I was not able to get it to. I switched to cron because I know cron better. I will switch it back, tomorrow.

Again, thanks!

Thanks, Rich. I made the needed changes in my code fences.

kriznik, I was just using a light so I could see when it came on and went off. I am planning on setting up a heater out in the loafing shed, about 200 feet west of here.

Rich, yes, I am trying to turn on heat, by temperature. Your example, which I have no doubt works, does not work on my system, so I apparently have something else missing, or wrong. I receive a squiggly line under else, with a statement saying, “mismatched input ‘else’ expecting ‘end’.”

BTW, I am using cron every minute to speed up the process of testing out my temperature.

Again, I apologize, because I thought I knew a little programming, but it is apparent I do not know nearly enough.

This is what I have, now.
House.items

Number:Temperature PwsObservationsCurrentTemperature "Current temperature" {channel="weathercompany:weather:local:pwsObservations#currentTemperature"}

house.rules

rule "Loginfo every 1 minutes"
when
   Time cron "0 */1 * ? * *"
then
	
			if(WC_PWS_Temperature.state < 33|°F)
				{
				OfficeDeskLight.sendCommand(OFF)
				}
			logInfo("PwsObservationsCurrentTemperature", "> 33° = OFF " + WC_PWS_Temperature)	

		    else if (WC_PWS_Temperature.state < 30|°F) 
				{
       			OfficeDeskLight.sendCommand(ON)
    			}
			logInfo("currentTemperature", "> 30° " + WC_PWS_Temperature) 
			logInfo("Minute", "Another 1 minute has passed.")

end

I am sure I do not have it right, yet.

I really appreciate the help. It makes me feel even more like newbie, but…

Also, Rich, I receive this log entry: [WARN ] [el.core.internal.ModelRepositoryImpl] - Configuration model ‘house.rules’ has errors, therefore ignoring it: [9,37]: no viable alternative at input ‘Â’.

It means nothing to me.

The loginfo statement is after the if-statement, in which case the parser thinks that the if-clause is finished. The else need to be right after the closing } so the parser knows that they belong together. Put the loginfo between the curly braces ({}) as was suggested above.

			if(WC_PWS_Temperature.state < 33|°F)
				{
				OfficeDeskLight.sendCommand(OFF)
                
  logInfo("PwsObservationsCurrentTemperature", "> 33° = OFF " + WC_PWS_Temperature)
                  }	
		    else if (WC_PWS_Temperature.state < 30|°F) 
				{
       			OfficeDeskLight.sendCommand(ON)
			logInfo("currentTemperature", "> 30° " + WC_PWS_Temperature)
                                }
2 Likes

Ok, pacive, I have done that, and I no longer have the squiggly lines on my else. But I still have this error in the openhab.log:

2019-10-25 05:25:54.055 [WARN ] [el.core.internal.ModelRepositoryImpl] - Configuration model 'house.rules' has errors, therefore ignoring it: [11,37]: no viable alternative at input 'Â'
[17,46]: no viable alternative at input 'Â'

11,37 happens after the WC_PWS_Temperature.st (right here) ate.

I have read this link, but it is not making any sense to me.

You’d better show us your current rule. Talk of no longer having squiggly lines does not sit well with the suggestion to move (not remove) curly brackets.

Might be an encoding error from copy-pasteing or something like that, try to erase the line and retype it manually and see if it goes away.

The error is on line 9 column 37. It seems to be seeing something stage at that position in the file. Try deleting and typing that line.

But make sure to fix the curly brackets first. You cannot have any extra lines of code between an if statement and it’s else. If you are using { } the else must immediately follow the }.

As demonstrated in my example alone and in the many examples at that link, when you have a Number that had a UoM, you must provide the units when comparing the state to a number. 33|°F for example to compare a Number:Temperature to 33°F. If you want to compare to
0 °C you would use 0|°C.

Ok, here is my house.rules file:

var Number PwsObservationsCurrentTemperature 

rule "Loginfo every 1 minute"
when


Time cron "0 */1 * ? * *"

then
			if(WC_PWS_Temperature.state > 33|°F){OfficeDeskLight.sendCommand(OFF)
					logInfo("PwsObservationsCurrentTemperature", "> 33° = OFF " + PwsObservationsCurrentTemperature)}
		    else if (WC_PWS_Temperature.state < 30|°F){OfficeDeskLight.sendCommand(ON)    			
					logInfo("currentTemperature", "> 30° " + WC_PWS_Temperature)}
end

My house.items file:

Number:Temperature PwsObservationsCurrentTemperature "Current temperature" {channel="weathercompany:weather:local:pwsObservations#currentTemperature"}

I tried removing the offending lines and retyping it, no good.
Now, where the error shows up in in the middle of the workd state on both lines. IE, “if(WC_PWS_Temperature.st**(Right Here)**ate > 33|°F)”
I believe I have all the curly brackets and parenthesis, correct.

BTW, what does, “no viable alternative at input ‘Â’.” mean? It is in my openhab.log file every time I save the house.rules.

Each ‘block’ of an if-else should have its own curly brackets. You can’t keep some and miss others out.

if (conditions) {
    // some stuff
} else if (condition) {
   // other stuff 
}

The curly brackets tell the rule interpreter, "this bunch of code belongs with the ‘if’ . that bunch of stuff belongs with the ‘else’ "

Ok, rossko57, I have that curly brackets correct. It still does not work.

The questions I have are:
Do I need any java imports, or anything like that?
Which variables should have “var and the conditions” at the beginning of the house .rules file?
I have the |°F in place now, but even before the loginfo showed the temperature, in °F.

I think I have over thought it, partially, but then again, this is new to me, and I apparently have missed the part that needs to be correct.

I am turning on my chicken heat manually, since it is going down to 5°F, tonight.

I would really like to figure out (or some one to figure it out) what is wrong. If I get it working once, I will make notes and have it to help me in the future.

Thanks for all of the help. I really need it.

I would try the syntax for Units of Measurement as posted by @rossko57 ( “°F”, I. E. with the " around !). Your column count might be wrong.

David,
there are several ways to compare items with UoM, as there is a Unit in the item-State. You can see this in your logInfo “State=27 °F

So if you want to compare it with some other value, you have to bring either the left side or the right sight of the comparison to the others format.

Here is an example how to deal with comparison of UoM-Items in three different ways:

rule "Rain warning"

when
    Item Dummy4 changed

then
    if(localHourlyForecast3RainVolume.state<0.1 | "mm") {
       logInfo("RainVolume","RainVolume with Pattern: " + localHourlyForecast3RainVolume)
     } // works
     if( (localHourlyForecast3RainVolume.state as Number).floatValue < 0.01) {
       logInfo("RainVolume","RainVolume as Float for comparison: " + localHourlyForecast3RainVolume)
     } // works
     var vRainVol = (localHourlyForecast3RainVolume.state as Number).floatValue
     if( vRainVol < 4) {
       logInfo("RainVolume","RainVolume with changed Variable: " + vRainVol)
     } // works
end

For testing your rule you can use a simple “Dummy-Switch” as a trigger:

Switch      Dummy4   "Testschalter Dummy4 [%s]"       (gPower)

Another question is, has your rule file the correct file format (UTF-8). Normally you can see this in your Editor:
grafik

Ok, I tried your example, and I cannot run the house.rules because of an error. It shows:
[WARN ] [el.core.internal.ModelRepositoryImpl] - Configuration model 'house.rules' has errors, therefore ignoring it: [17,35]: no viable alternative at input 'Â'

This is my house.rules, and I tried it both with spaces and without spaces, around the >33. It looks as if I am dead in the water until I can figure out what the error is. I do not understand: no viable alternative at input 'Â'.


rule "Loginfo every 1 minute"
when


Time cron "0 */1 * ? * *"

			if(WC_PWS_Temperature.state > 33|°F){OfficeDeskLight.sendCommand(OFF)logInfo("PwsObservationsCurrentTemperature", "> 33° = OFF " + WC_PWS_Temperature)}
		  
end


Line 17 is the one with the if statement.