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

Ok, does this tell you anything?

rule "Loginfo every 1 minute"
when
	Time cron "0 */1 * ? * *"
then
	logInfo("currentTemperature", "< 33° " + WC_PWS_Temperature)
	if(WC_PWS_Temperature.state < 33){OfficeDeskLight.sendCommand(ON)
	logInfo("currentTemperature", "< 33° " + WC_PWS_Temperature)}
end	

Give this on loginfo: [INFO ] [home.model.script.currentTemperature] - < 33° WC_PWS_Temperature (Type=NumberItem, State=2 °F, Label=Temperature, Category=temperature).

rule "Loginfo every 1 minute"
when
	Time cron "0 */1 * ? * *"
then
	logInfo("currentTemperature", "< 33° " + WC_PWS_Temperature)
	if(WC_PWS_Temperature.state < 33|°F){OfficeDeskLight.sendCommand(ON)
	logInfo("currentTemperature", "< 33° " + WC_PWS_Temperature)}
end	

Gives this on openhab.log: [WARN ] [el.core.internal.ModelRepositoryImpl] - Configuration model ‘house.rules’ has errors, therefore ignoring it: [29,35]: no viable alternative at input ‘Â’.

Look a few posts above, make sure the file is in UTF-8. What editor do you use?

This is referring to your degrees symbol ° , which the rules parser does not expect there.
if(WC_PWS_Temperature.state < 33|°F)

I’m not sure that all versions of openHAB deal with UoM in exactly the same way here, but the cure for OH 2.4 is

if(WC_PWS_Temperature.state < 33|"°F")...

This is the method shown in post #4

1 Like

If you have problems with the degree-sign, try this kind of IF-Statement

     if ((WC_PWS_Temperature.state as Number).floatValue < 33) {
         OfficeDeskLight.sendCommand(ON)
         logInfo("currentTemperature", "< 30° = ON " + WC_PWS_Temperature) 

But normally the examples of @rossko57 should bring a result too.

Maybe there are other reasons why your rule brings errors, i.e. Keyboard settings, language settings, wrong file-type, etc.
So as said above it would be interesting what file-type your rule-file has (screen-shot) and which editor you are using.

Rossko57, you mailed it on the head. I put the quotation marks in, and it is working. Thank you so much.

Also, thanks to everybody else for your suggestions. I used a couple of other suggestions, too.

Here is the working code:

rule "Test Temperature Control"
when
	Item WC_PWS_Temperature changed
then
		logInfo("currentTemperature", "item changed" + WC_PWS_Temperature)
	if(WC_PWS_Temperature.state < 25|"°F"){ChickenCoopHeat.sendCommand(ON)
	
		logInfo("currentTemperature", "WC_PWS_Temperature changed and OfficeDeskLight commanded on" + WC_PWS_Temperature)}
	else if(WC_PWS_Temperature.state > 27|"°F"){ChickenCoopHeat.sendCommand(OFF)
		logInfo("currentTemperature", "WC_PWS_Temperature changed and OfficeDeskLight commanded off" + WC_PWS_Temperature)}
end	

BTW, I have no idea where my manners were.

Thanks to:
fibu-freak
pacive
rlkoshak
kriznik
rossko57
opus
Olymp

I believe that was everyone. I spent three weeks reading and I could not get it. After several suggestions, I got it. I really appreciate it.

2 Likes

One more hint!
After setting up my new RPi 4 from the scratch I found a similar error, when starting my test-rule

rule "Loginfo every 10 minutes"
when
    Item Dummy4 changed to ON
then
var Number WC_PWS_Temperature = 30 |"°F"
var Number currentTemperature = 29 |"°F"
    
            if(currentTemperature > 33 | "°F"){
//              OfficeDeskLight.sendCommand(OFF)
                
            logInfo("currentTemperature", "> 33° = OFF " + WC_PWS_Temperature)
            }

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

            
end

Error-Message:

Fehlermeldung::::
2019-11-01 23:39:07.057 [WARN ] [el.core.internal.ModelRepositoryImpl] - Configuration model 'test_PWS.rules' has errors, therefore ignoring it: [15,41]: no viable alternative at input '?'
[15,43]: extraneous input 'F' expecting ')'		 

After a bit searching I found the reason. My language settings (environment variables) where not stetted up correctly. (I don’t know if I made a mistake on startup or is it a bug in “openhabian-config”).

To look for correct setting just log in via Putty and type in:

env | grep -i lang

to get a result like this:
image

If your result looks like this:

LANG=de_DE.UTF-8 LC_ALL=de_DE.UTF-8 LC_CTYPE=de_DE.UTF-8 LANGUAGE=de_DE.UTF-8

it could be that there’s something wrong with your environment-settings.

Does that mean that form (no quotes) now works for you, with new language settings?
Might explain why it seems to work for some and not others.

Doesn’t work for me in Windows / OH2.4, I need 30|"°F"

Just a moment, I will test it again (not to say something wrong)

The Rule:

rule "Loginfo every 10 minutes"
when
    Item Dummy4 changed to ON
then
var Number WC_PWS_Temperature = 30 |"°F"
var Number currentTemperature = 29 |"°F"
    
            if(currentTemperature > 33 | "°F"){
//              OfficeDeskLight.sendCommand(OFF)
                
            logInfo("currentTemperature", "> 33° = OFF " + WC_PWS_Temperature)
            }

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

            
end

The result in the logger:

2019-11-02 11:17:32.153 [INFO ] [el.core.internal.ModelRepositoryImpl] - Refreshing model 'test_PWS.rules'
2019-11-02 11:36:51.318 [INFO ] [ipse.smarthome.model.script.mqtt OFF] -  vString1 192.168.178.20 enthält: {"MqttHost":"openHABPi"}
2019-11-02 11:36:51.324 [INFO ] [home.model.script.currentTemperature] - < 30° = ON 30 °F

My environment:

[11:15:44] openhabian@openHABPi:~$ env | grep -i -e lang -e lc_
LANGUAGE=de_DE.UTF-8
LANG=de_DE.UTF-8
LC_CTYPE=de_DE.UTF-8
LC_ALL=de_DE.UTF-8

…it worked for me.

Thanks. That explains it. That is why I followed some with no results, but then I added the " and it worked.

I don’t seem to have any of those on a Windows / Oracle java host. Nearest
user.country GB
user.language en
Something subtle going on here :crazy_face:

I suppose the conclusion is that safest to quote UoM

Thx for reply at first. You are right that to quote measurements as the safest way to compare and to avoid problems. I have to excuse, but my English is not the best. (learned 50 years ago)

I think the setup on Windows-Machine differs a bit from my openhabian-hassle free version.
I came to openHAB and Linux and that stuff a year ago or bit more using a RPi 3b. On this machine I’m now on 2.5.0.M4.
I never had problems with transformations of Units, either with or without Quotation Marks or with transforming it, as I learned a lot in this time from you and others (Rich, Udo, Scott, Jürgen, Bruce,etc.).
But yesterday I got my new RPi 4b and I made a complete new install of OH with the latest distro (which includes the workaround of the java problem) based on OH 2.4 stable.

At the end of the install-procedure I got the information that’s something wrong with the language settings. (I think it was a perl-script within the initial startup-procedure, which told me that something’s wrong with the environment variables LANG and LANGUAGE).
I tried to solve it with “openhabian-config” and setting it manually with the options of topic “30” | System Settings but it doesn’t work, and I saw that on my new system my test-rule produced the same error as @dsspindler described it.
When comparing my Environment-Setings from my “old RPI3b-Machine” and the new one , I found the environtment-variables LANG and LANGUAGE where different and so I changed it from

to

LANGUAGE=de_DE.UTF-8
LANG=de_DE.UTF-8

using the following commands on the shell:

export LANGUAGE=de_DE.UTF-8
export LANG=de_DE.UTF-8

I hope this can help others, to look on those variables when a similar problem comes up.

Cheers,
Peter

BTW: Thx for this snippet

//  Temperature
//  ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//  version of rossko57 for better error-handling
//  The idea is to toughen this expression against surprises
//  by doing it longhand
//  +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//  get a sorted but unfiltered list
    var myTempItemList = gTemp.members.sortBy[state]
//		   logInfo(rulename,"2: sorted but unfiltered - myTempItemList: " + myTempItemList)

    var Number sumTemp = 0
    var avgTemp = 0
    var countTemp = 0
    var minTemp = 100.0
    var maxTemp = -100.0
    myTempItemList.forEach[ g |
       // do the filter now
       val thisNamePart = g.name.split("_").get(1)  // I'm not sure if this might error or not
//	   logInfo(rulename,"3: g.name:" + thisNamePart)
       if (thisNamePart !== null) {
            // should be safe to parse now
            if (Integer.parseInt(thisNamePart) > daymin && Integer.parseInt(thisNamePart) <= daymax) {
                 // now process
                 var thisValue = (g.state as Number ).floatValue  
//				 logInfo(rulename,"4: thisValue: {} thisNamePart {} ", thisValue, Integer.parseInt(thisNamePart))
                 sumTemp = sumTemp + thisValue
                 if (thisValue > maxTemp) { maxTemp = thisValue }
                 if (thisValue < minTemp) { minTemp = thisValue }
                 countTemp = countTemp + 1  // bump counter
//				 logInfo(rulename,"5: countTemp:" + countTemp)
            }  // else filter this one out
        } else {
           logInfo("test", "6: gTemp name unexpected - " + g.name)
       }
    ]  // end of foreach
//    logInfo(rulename,"7: avgTemp: {} countTemp: {}  sumTemp: {} " , avgTemp, countTemp, sumTemp)

:wink: :+1:

Doesn’t the above only change it for the specific shell where you type the commands? I think you need a way to set this for the entire system every time it boots. Does it go in an openHAB startup file? Or I misunderstand it?

Hi John,
I searched a bit in the web for this command and if I understand it correct the variables will be set permanently until it is unset. i.e see here. These environment-variables are set system-wide and can be used from all programs.

Just type in “env” on the Shell and you will see all your variables or for special variables something like

I don’t see how that could work, but maybe I missed something. I created a sub shell, changed the environment, exited, and the change was lost. I think when you exit the shell and login again you won’t see these set the same (unless they are getting set somewhere else too).

$ /bin/bash
$ ps
  PID TTY          TIME CMD
  344 pts/0    00:00:00 bash
14550 pts/0    00:00:00 bash
14558 pts/0    00:00:00 ps
$ export LANG=foobar
$ env | grep LANG
LANG=foobar (yes, set here)
$ exit
exit
$ env | grep LANG
LANG=en_US.UTF-8 (now it's gone)
$

Sorry, I’m not that specialist, I only can say what I have done. This was my environment-variable printed out after I made a new install of openHAB … and got an warning-message from the install-routine at the end.

First I tried to correct it with openhabian-config ( no.32)


But the error still occurs.
After that I tried it the other way and the magic happened. I didn’t do that in a subshell, I just typed it in the command line.

BTW: I just read that post once again 'til the end. Now I’m wondering what happend :thinking: and understand what you mean.
I only can say as it it now.

$ env | grep -i -e lang -e lc_
LANGUAGE=de_DE.UTF-8
LANG=de_DE.UTF-8
LC_CTYPE=de_DE.UTF-8
LC_ALL=de_DE.UTF-8
$

I can stop OH, shutdown. After the restart the variables are still set. (weird)

I wonder if the “Set System Locale” actually did the permanent change, but only after the reboot. I.e., your command line change worked for current session, but the openhabian-config made it good for the future.