[Solved] Rules not working

It’s me again. Now I’m working on some rules and they don’t appear to be running. I have one rule that I’m trying to use to combine two values as a single text string to combine Temp/Humidity on a single line. I have another rule that is supposed to send mail if the temperature in my house gets above a certain temperature. This is what they look like:

`import org.openhab.core.library.types.*
import org.openhab.core.persistence.*
import org.openhab.model.script.actions.*
import org.java.math.*
import org.joda.time.*

/* ===================== Update Temp/Humidity as one variable to display on web ===================== */

rule "Combine temp humidity for display"
when
Item HVAC_Temperature updated or
Item Humidity_Living updated
then
HVAC_Combined_Temp_Hum.postUpdate(HVAC_Temperature.state.toString + " / " + Humidity_Living.state.toString + “%”)
end

/* ===================== Send warning if temp is at or above 82F ===================== */

rule “Send warning if temp is at or above 82F"
when
Item HVAC_Temperature received update
then
if (HVAC_Temperature > 76)
sendMail("myemail@somewhere.com”,“Test e-mail”,“Test e-mail”)
end`

Note that I temporarily set the temperature trigger to 76 just so that I could test and will change it to 82 once I know it is working.

The items look like this:

Number HVAC_Temperature "Thermostat Temperature [%.1f &deg;F]" <temperature> (Living,HVAC,Temperature) { zwave="9:command=sensor_multilevel,sensor_type=1,refresh_interval=60" } Number Humidity_Living "Humidity [%.1f %%]" <water> (Living,Humidity,HVAC) { mysensors="1;0;V_HUM" } String HVAC_Combined_Temp_Hum "Combined temp [%s]" (Living,HVAC)

I’m probably doing something silly with the syntax, but I just can’t figure out what I’m doing wrong.

Designer is your friend when it comes to syntax. I strongly suggest using it to help with that sort of problem.

What do you see in the logs? Errors in openhab.log? Does events.log show HVAC_Temperature or Humidity_Living received updates?

Add logInfo calls to your rules so you can see in openhab.log when they execute.

I have it installed and I loaded the rules file but I can’t see anything happening. Is it supposed to highlight any errors or something?

I don’t see anything at all in the logs pertaining to the rules and if they’ve run. Yes, both are showing up in the events.log file.

I changed the code to read:

`/* ===================== Update Temp/Humidity as one variable to display on web ===================== */

rule "Combine temp humidity for display"
when
Item HVAC_Temperature updated or
Item Humidity_Living updated
then
logInfo(“rules”, “Combined activated”)
HVAC_Combined_Temp_Hum.postUpdate(HVAC_Temperature.state.toString + " / " + Humidity_Living.state.toString + “%”)
end

/* ===================== Send warning if temp is at or above 82F ===================== */

rule “Send warning if temp is at or above 82F"
when
Item HVAC_Temperature received update
then
logInfo(“rules”, “Temp change activated”)
if ((HVAC_Temperature.state as DecimalType) > 76)
logInfo(“rules”, “Temp > 76 activated”)
sendMail("user@host.com”,“Test e-mail”,“Test e-mail”)
end
`

I don’t see anything in the logs nor do I see a rules.log file being created. Seems I’ve really messed all of this up!

Syntax errors will be underlined in red. No red, everything is syntactically correct.

There isn’t a rules log file. Those log statements go to openhab.log.

When you start OH, do you see a line in openhab.log saying your rules file has been loaded?

Oh. I thought it made a separate file.

Yes, they are being loaded:

2016-06-10 12:12:13.814 [INFO ] [c.internal.ModelRepositoryImpl] - Loading model 'hvac.rules'

and here’s an update from the events.log that I think should have caused the max temp rule to be fired:

2016-06-10 12:13:01 - HVAC_Temperature state updated to 78

I’ve tried changing the value to (HVAC_Temperature.state as DecimalType) but that doesn’t seem to have any effect.

So I copied this over to it’s own rules file and widdled the imports down and it works:

import org.openhab.core.library.types.*
import org.openhab.model.script.actions.*

/* ===================== Update Temp/Humidity as one variable to display on web ===================== */

rule "Combine temp humidity for display"
when
    Item HVAC_Temperature updated or
    Item Humidity_Living updated  
then
	logInfo("rules", "Combined activated")
    HVAC_Combined_Temp_Hum.postUpdate(HVAC_Temperature.state.toString + " / " + Humidity_Living.state.toString + "%")
end

/* ===================== Send warning if temp is at or above 82F ===================== */

rule "Send warning if temp is at or above 82F"
when
	Item HVAC_Temperature received update
then
	logInfo("rules", "Temp change activated")
	if ((HVAC_Temperature.state as DecimalType) > 76)
	logInfo("rules", "Temp > 76 activated")
	sendMail("user@hostl.com","Test e-mail","Test e-mail") 
end

I had a similar problem with a different rules file. Can you or anyone tell me why the includes I listed above are
causing problems? As a new user I’m just copying imports from scripts I’m trying so I don’t really know which are needed and which aren’t. I’d really like to understand what needs to be imported rather than just using this hit-or-miss method.

Figuring out the imports is a bit tricky in OH 1. In OH 2 you barely have to import anything any longer so it is much easier.

So here is short list of some standard imports.

  • org.openhab.model.script.actions.* : to use Timers
  • org.openhab.core.library.types.* : use when you need to cast to a specific type of state (e.g. use “as DecimalType”)
  • org.openhab.core.library.item.* : use when you need to cast to a specific type of Item (e.g. use “as NumberItem”)
  • org.joda.time.* : use when you need to create DateTime objects
  • org.eclipse.xtext.xbase.lib.* : to use lambdas
  • Java Classes : when you need to work with a class from the core Java language (e.g. java.util.Map)

I can’t answer why those imports would be causing problems. But I like to use the bare minimum number of imports. You can easily see if you actually require an import by opening the rules file in Designer, comment out the import and see if it highlights anything in red. If it does, you need the import. If it does not you can delete it.

2 Likes

Thanks for replying to this and my other threads. I really appreciate your help.

To further test your patience, you say that I can comment out the imports in Designer and it will mark problems due to not having the imports. I tried this with my rules file by commenting out all imports and I didn’t see anything marked red. The file I have has a timer in it so I thought it would certainly.

I do see one error and it’s a complaint about “missing EOF at ‘val’” in the following line:

val String PATTERN = "HH:mm "

Not sure what EOF is as I’m accustom to that meaning end of file and this line is certainly not at the end of the file. I’d love to use Designer but I’m not sure exactly how to use it’s features. I’ve done some searching and haven’t really found a guide of help file, but admittedly I’m not the best at searching from time-to-time.

That error indicates that it is expecting there to be an end of file at that point instead of a val.

There can be two sources of this error.

  1. You are trying to define the global val PATTERN after you have defined a rule. As documented on the wiki page the format of a rules file is

[Imports]

[Variable Declarations]

[Rules]

You cannot mix variable declarations and rules.

Or there is something really weird going on in that file. This may sound tedious but the best way I’ve found to get past these sorts of errors is to create a new blank file and copy over the text, or even better retype the rules.

Depending on the speed of your machine, RAM, weather, phase of the moon, whether Mercury is in retrograde, … Designer can take a surprising amount of time to catch up. Sometimes you can force it to reparse the file by just adding a space somewhere in the file.

Also, if you are using OH 2 you no longer need to make these imports at all.

Is OH2 ready for prime time in regards to bindings? I’m using:

I’m hesitant to just in on OH2 as a beginner as I wouldn’t be certain if the potential problems I’d have would be incompatibility or of my own creation. Can I install OH2 in a separate directory (win7) and test without having to worry about disturbing my OH1.8.3 install?

As a beginner I wouldn’t recommend OH 2 just yet primarily because of the lack of complete documentation. Great strides are being made on that but it still isn’t to a place where it is complete.

The bindings should be fine. There is a compatibility layer that lets OH 2 run any OH 1 addon. The only real distinction between the “officially” supported OH 1 addons for OH 2 and the rest are the “official” ones have been thoroughly tested.

You can have both installed in separate directories but you cannot run them both at the same time.

So you can either run the officially supported OH1 addons OR run the ones I am using now? I’m not sure if I’m following you correctly. At any rate, I’ll probably play with it once I manage to get things working well with OH1.8.3 and once I get a little more experience under my belt.

All OH 1.x addons should work with OH 2. OH 2 lists several OH 1 addons that are known to work because they have been tested.

1 Like