I have a simple rule running for a humidity sensor. What happens - is that if the sensor drops below the threshohold i start to receive messages almost every minute.
I know that a simple solution would be to change “received update” to cron rule checking only once in 10 minutes.
But really want to try a better solution. So how to introduce a timer or something to send messages only once in 10 minutes?
`
rule "Humidity"
when
Item sense_child_humi received update or
Item sense_bedro_humi received update
then
if (iam_home.state==ON)
{
if (sense_child_humi.state < 40)
{
sendTelegram("Somebot","Low humidity in kids room %s", sense_child_humi.state.toString)
}
if (sense_bedro_humi.state < 30)
{
sendTelegram("Somebot","Low humidity in bedroom %s", sense_bedro_humi.state.toString)
}
}
else {}
end
Here is another way you can do it without a timer.
var lastSent = null
rule "Humidity"
when
Item sense_child_humi received update or
Item sense_bedro_humi received update
then
if (iam_home.state==ON)
{
if(lastSent == null || lastSent.before(now.minusMinutes(10))) {
if (sense_child_humi.state < 40)
{
sendTelegram("Somebot","Low humidity in kids room %s", sense_child_humi.state.toString)
lastSent = now
}
if (sense_bedro_humi.state < 30)
{
sendTelegram("Somebot","Low humidity in bedroom %s", sense_bedro_humi.state.toString)
lastSent = now
}
}
}
else {}
end
NOTE: this might not work as typed but the concept is sound
The declaration of lastSent needs to be given a type. Because I initialize it to null, the Rules Engine has no idea what type should be so it defaults to Object.
var DateTime lastSent = null
Like I said, I just typed it in, there was bound to be a mistake or two.
2019-06-03 22:16:29.270 [ERROR] [ntime.internal.engine.RuleEngineImpl] - Rule ‘System Load Logging’: An error occurred during the script execution: Could not invoke method: org.eclipse.xtext.xbase.lib.BooleanExtensions.operator_or(boolean,boolean) on instance: null
from these rules
var DateTime lastSentU2C = NULL
var DateTime lastSentU2M = NULL
var DateTime lastSentU2T = NULL
rule "System Load Logging"
when
// move to a trigger group
Item CPUloadSetpoint changed or
Item systeminfo_computer_u2_cpu_load changed or
Item CPUmemSetpoint changed or
Item systeminfo_computer_u2_memory_used changed or
Item CPUtempSetpoint changed or
Item systeminfo_computer_u2_sensors_cpuTemp changed
then
//5 min flood limit
if (( lastSentU2C == NULL ) || (lastSentU2C(now.minusMinutes(5))) ) {
// prepare package of OH load information
//logInfo("system load.rules", "cpu load changed " +
systeminfo_computer_u2_cpu_load.state.toString )
if ((systeminfo_computer_u2_cpu_load.state as DecimalType) >
(CPUloadSetpoint.state as DecimalType)) {
//main log
if (U2CModeSW.state ==1 || U2CModeSW.state ==3) {
postUpdate(MainLog, "CPU Load : " +
systeminfo_computer_u2_cpu_load.state.toString) } //$Log
if (U2CModeSW.state ==2 || U2CModeSW.state ==3) {
SendTelegram.postUpdate("CPU Load : " +
systeminfo_computer_u2_cpu_load.state.toString) } //VTelegram
lastSentU2C = now
}
}
I have been experimenting, but getting more intresting errors
2019-06-03 22:16:29.270 [ERROR] [ntime.internal.engine.RuleEngineImpl] - Rule ‘System Load Logging’: An error occurred during the script execution: Could not invoke method: org.eclipse.xtext.xbase.lib.BooleanExtensions.operator_or(boolean,boolean) on instance: null
I think i understand the error but as yet found a solution on the forum
What is your understanding of the error? It might not be what you think.
This is a pretty text book example of a Type error in Rules DSL. Somewhere you have a line where you are using the or (||) operator. This operator expects two Booleans but one of operands is not a Boolean in this case so it reverts to null.
It’s a really unhelpful error really. But believe it or not, it’s a better error than we used to get.
Now, looking at the code you posted nine days ago, I do see a couple of things that stand out.
Initialize variables with null not NULL. NULL is a special state that Items can carry to indicate that the Item is uninitialized. null is a special primitive type meaning “no value”. Given that NULL is of type State and not of type DateTime, I’m surprised that OH allows the declaration of the global variables at all.
lastSentU2C(now.minusMinutes(5)) is effectively meaningless. What are you trying to do here? This is the cause of your problem. Again, I’m surprised that OH isn’t just rejecting this out of hand and actually lets you run it. I suspect what you want is to see if lastSentU2C is more than five minutes ago? If yes, then use lastSentU2C.isBefore(now.minusMinutes(5)).
Many thanks for your, as usual very through explanation. I am still gaining knowledge of this intresting rules system
Could the error system comment regarding the || operator or does this error cover a multitude of things
In my logs regarding null I am getting
2019-06-13 21:33:28.854 [INFO ] [el.core.internal.ModelRepositoryImpl] -
Validation issues found in configuration model 'system.rules', using it anyway:
The operator '==' should be replaced by '===' when null is one of the
arguments.
I maybe wrongly understood === to be more strict(probably wrong phrase) so used ==
As the warning says, when null is one of the operands, you need to use === not ==. The explanation is long and goes deep into how the language works. But you can use the rule of thumb. If null appears on either side use ===. In all other cases use ==.