New to rules - allready hit a wall

I’m trying to create a rule which should be run once a day, saving some temperatures to a MySQL database, for further use in other apps.

Rule is as follows:

rule "Temperatur test"
when
	Time cron "0 0 21 * * ?"
then
	NightEconomy?.members.forEach(tStat |
		logInfo("NightEconomy", "Setting " + tStat)
		executeCommandLine("/opt/openhab/scripts/SaveTemp.php name=" + tStat)
	)
end

I get this error:

java.lang.RuntimeException: The name 'tStat' cannot be resolved to an item or type.

If i comment out either logInfo or executeCommandLine everything is fine.
I’m pretty new to Java programming (I’m more into PHP and VB.Net languages)

Anyone to point me in the right direction?

I have not used this construct myself, so I am not sure about this, but it seems that all other examples of “.members.foreach”-constructs that I have seen around the forum is using square-brackets, like this:

	NightEconomy?.members.forEach[tStat |
		logInfo("NightEconomy", "Setting " + tStat)
		executeCommandLine("/opt/openhab/scripts/SaveTemp.php name=" + tStat)
	]

It’s worth a try at least.

Have you looked at the MySQL Persistence binding? You don’t need a rule to save the value once a day using that, though if you need the table to look a certain way then it wouldn’t work.

Question, what type of Items are in NightEconomy?

Regardless I think you want your rule to look as follows:

rule "Temperatur test"
when
	Time cron "0 0 21 * * ?"
then
	NightEconomy.members.forEach[tStat |
		logInfo("NightEconomy", "Setting " + tStat.state.toString)
		executeCommandLine("/opt/openhab/scripts/SaveTemp.php name=" + tStat.state.toString)
	]
end

The changes:

  • I got rid of the “?”. That causes the rule to skip over the line if NightEconomy is null. Personally I would rather get the error than have it silently skip the line.
  • forEach takes a lambda (a function). Anytime you see “|” it is indicating a lambda. the stuff before the “|” is the argument and the stuff after is the code. I always put lambdas in “” rather than “()”. The openHAB Rules Domain Specific Language (DSL) is often pretty forgiving in letting you use “” and “()” interchangeably but not always so make it a habit to always use square brackets for lambdas.
  • If you want the “value” of an Item you must reference the Item’s state. An Item is an Object with a whole bunch of methods and data associated with it, only one of which is the state.

Unfortunately errors in the Rules DSL do not always point directly at the actual error. I believe the error is either caused by not using “” in this context or because you didn’t use tStat.state.toString to get the value as a String.

This is a pet peeve of mine. The Rule’s programming language is a custom Domain Specific Language (i.e. a programming language created specifically for openHAB) based on the Xbase programming language and bearing the most resemblance to the Xtend programming language. It is NOT Java and bears very little resemblance to Java.

It seems like the Square brackets did the trick.

I’ve only looked a Little into persistence, and will investigate it even more.
Right now I just need to get this up and running - maybe it’s a “dirty” way of achieval - but at least it’s something I’m familiar with :slightly_smiling:
Then I can do it the right way when I get the idea behind persistence usage :slight_smile: