Help with rule for temp sensor

I’m using several temperature sensors which are powered by Li-Ion batteries. I want to be notified when a battery dies so I made this:
Created an expiry on my temp items:
//items
Number temp_vh_eg “temp [%.1f]” {mqtt="<[mosquitto:sensor/EG/VH/Temp:state:default]", expire=“5h,100”}

and a rule which checks when the temp is 100 and notifies me:
//rule

rule “Temperatur Sensoren Check”
when
Item temp_wz changed or
Item temp_bad_e changed or
Item temp_vh_eg changed or
Item temp_aussen changed or
Item temp_pool changed
then
Thread::sleep(100) // give persistence time to catch up
val sensor = triggeringItem as Number
if(sensor.state as Number = 100) {
sendBroadcastNotification(triggeringItem.name + " hat seit 5 Stunden kein Update erhalten")
}
end

But I get this Error in log:
2018-06-06 18:30:42.699 [ERROR] [ntime.internal.engine.RuleEngineImpl] - Rule ‘Temperatur Sensoren Check’: Could not cast temp_vh_eg (Type=NumberItem, State=24.44, Label=temp, Category=null) to java.lang.Number; line 10, column 18, length 24

What am I doing wrong?

Please post the complete rule.

Please use the code fences when publishing code or logs

rule “Temperatur Sensoren Check”
when
    Item temp_wz changed or
    Item temp_bad_e changed or
    Item temp_vh_eg changed or
    Item temp_aussen changed or
    Item temp_pool changed
then
    Thread::sleep(100) // give persistence time to catch up
    if((triggeringItem.state as Number) == 100) {
        sendBroadcastNotification(triggeringItem.name.toString + " hat seit 5 Stunden kein Update erhalten")
    }
end

3 errors there
val sensor = triggeringItem as Number
triggeringItem is an item and can’t be converted to a Number
It could have been: val sensor = triggeringItem as NumberItem but you don’t need it

When doing an equal test you need to use ==. A single = assigns a value.

triggeringItem.name to triggeringItem.name.toString

thank you - will try that

You can also put your items in a group eg: gTempSensors and do this:

rule “Temperatur Sensoren Check”
when
    Member of gTempSensors changed
then
    Thread::sleep(100) // give persistence time to catch up
    if((triggeringItem.state as Number) == 100) {
        sendBroadcastNotification(triggeringItem.name.toString + " hat seit 5 Stunden kein Update erhalten")
    }
end

And when you use triggeringItem I think you don’t need to wait for persistence to catch up.

Could be one liner then:

rule “Temperatur Sensoren Check”
when
    Member of gTempSensors changed
then
    if((triggeringItem.state as Number) == 100) sendBroadcastNotification(triggeringItem.name.toString + " hat seit 5 Stunden kein Update erhalten")
end

Even better:

rule “Temperatur Sensoren Check”
when
    Member of gTempSensors changed to 100
then
    sendBroadcastNotification(triggeringItem.name.toString + " hat seit 5 Stunden kein Update erhalten")
end
rule “Temperatur Sensoren Check”
when
    Member of gTempSensors changed to 100
then
    sendBroadcastNotification("Fehler: kein Update seit 5 Stunden für "+ triggeringItem.name )
end

:smile:

Sorry me no speak german.
What did your change?

Moved the item at the end of the sentence and removed toString as it will work without it then.

Just to show one thing which offen confuses. When to string is automatically used.

To provide some context to what Josar posted, .name already returns a String so there is no need to call toString on it.

Beyond that, most of the time you don’t have to call toString on an Object when constructing a String (e.g. "First part " + myObject + " second part"). As long as myObject is indeed an Object, the language will be smart enough to call toString for you.

Where this usually breaks down is when a method call expects a String (e.g. the sendCommand and postUpdate Actions). Sometimes, for no reason I can tell, it just can’t figure out that it needs to call toString on the Objects.

Call toString doesn’t hurt anything in this context, but it is not needed.

To just add more confusion, I think it is also needed when the object is at the beginning of an concatenated string.
I think I remember falling into such a trap and thus avoid objects at the beginning of message strings.
I just wanted to show @andy_k that he might see such calls without toString and raise awareness of this.

Thanks for all your help - rule is loading but it seems that my expire is already not working. Is there something wrong with expire and number items?
UPDATE: ok it’s working - somehow the item I tested with was still NULL and so it didn’t expire
Thanks for you help!!