luckily @Josar was able to point me into the right direction concerning the setup of a notification rule, which is
import org.openhab.core.library.types.*
import org.openhab.core.persistence.*
import org.openhab.model.script.actions.*
import org.joda.time.*
// Global Variables
var lastSent = null
rule "Feuchte Kellergang"
when
Item zwave_device_5e813456_node4_sensor_relhumidity received update
then
{
if(lastSent == null || lastSent.before(now.minusMinutes(10))) {
if (zwave_device_5e813456_node4_sensor_relhumidity.state > 50)
{
sendTelegram("bot1", "Luftfeuchtigkeit im Kellergang > 70%%, aktuell bei %s%%", zwave_device_5e813456_node4_sensor_relhumidity.state.toString)
lastSent = now
}
}
}
end
Strangely, this rule is working for the first time, but if there are multiple events that would cause a notification, any further notification causes the following error in the log
Rule 'Feuchte Kellergang': An error occurred during the script execution: The name '<XFeatureCallImplCustom>.before(<XMemberFeatureCallImplCustom>)' cannot be resolved to an item or type.
So what is OpenHab trying to tell me - or what did I do wrong here?
import org.openhab.core.library.types.*
import org.openhab.core.persistence.*
import org.openhab.model.script.actions.*
import org.joda.time.*
val Number lastSent = null
rule "Feuchte Kellergang"
when
Item zwave_device_5e813456_node4_sensor_relhumidity received update
then
if(lastSent == null || (now.millis-lastSent)> 600000 ) {
// Get epoch from Joda DateTime
lastSent = now.millis
}
val Number test = (now.millis-lastSent)
logInfo("TimeTest", "Epoch in milli seconds:"+ lastSent.toString() + " difference " + test.toString() )
end
The first time worked always because the first condition lastSent == null is fullfilled. But the next time the rule is invoked the var lastSent is not declared as aItem Type to the system, i think.
@Josar: Sorry for taking so long to answer, but this week was crazy so far.
According to your support, I updated the rules like this:
// Imports
import org.openhab.core.library.types.*
import org.openhab.core.persistence.*
import org.openhab.model.script.actions.*
import org.joda.time.*
// Global Variables
val Number lastSent = null
rule "maximale Feuchte Kellergang erreicht"
when
Item FeuchteSensor_Kellergang received update
then
if(lastSent == null || (now.millis-lastSent)> 600000 ) {
if (FeuchteSensor_Kellergang.state > 70)
{
sendTelegram("bot1", "Luftfeuchtigkeit im Kellergang > 70%%, aktuell bei %s%%", FeuchteSensor_Kellergang.state.toString)
// Get epoch from Joda DateTime
lastSent = now.millis
}
}
val Number test = (now.millis-lastSent)
logInfo("TimeTest", "Epoch in milli seconds:"+ lastSent.toString() + " difference " + test.toString() )
end
This looks good so far, but in my log, I have several entries that read like this:
2017-11-12 21:09:36.515 [ERROR] [.script.engine.ScriptExecutionThread] - Rule 'maximale Feuchte Kellergang erreicht': cannot invoke method public java.lang.String java.lang.Object.toString() on null
And the notification is not sent.
Any idea what might be the problem?
@Connor i think the error comes from the LogInfo(…) remove it or make sure all variables are initialized before using. At the first run lastSent is not initialized and therefore you see this message.
I did not test it but it should work, initialize lastSend with a time at the beginning. Then you can also omit the first check lastSent == null ||