[SOLVED] Could not cast 2079 to java.lang.Number... How to compare MQTT result/state to given threshold?

I’m getting mqtt-results from a soil moisture sensor and want to trigger a rule to activate a water pump based on the sensor readings. Now I’m trying to figure out how to compare these readings to a given threshold.

Error in Log:

Rule 'Irrigation': Could not cast 2079 to java.lang.Number

Rule:

rule "Irrigation"
    when
        Item Feuchtigkeit changed
    then
        if ((Feuchtigkeit.state as Number).intValue > 1000) {
            if (Pumpe_Timer.state == OFF) {
                Pumpe.sendCommand(ON)
                Thread::sleep(2000)
                Pumpe.sendCommand(OFF)
                Pumpe_Timer.sendCommand(ON) //puts pump in refractory state for 10 min
            }
        }
    end

Any idea why the result “2079” can not be cast to java.lang.Number or how I could compare that number to i.e. 1000 so the rule gets triggered?

Thanks!

Post you item, please?
Try this rule

rule "Irrigation"
when
    Item Feuchtigkeit changed
then
    if ((Feuchtigkeit.state as Number) > 1000) {
        if (Pumpe_Timer.state == OFF) {
            Pumpe.sendCommand(ON)
            createTimer(now.plusSeconds(2), [ |
                Pumpe.sendCommand(OFF)
                Pumpe_Timer.sendCommand(ON) //puts pump in refractory state for 10 min
            ])
        }
    }
end

Tried your code @vzorglub, thanks! Unfortunately the error remains:

Rule 'Irrigation': Could not cast 2071 to java.lang.Number

Item:

String Feuchtigkeit "Olivier: [%d]"
{ mqtt="<[broker:esp/erde/olivier/:state:default]" }

I’ve tried sending “Feuchtigkeit.state.toString” which works fine and sends out the correct 4-digit number:

sendTelegram ("…", “Erdfeuchtigkeit: %s”, (Feuchtigkeit.state.toString))

Your item is a String!!
Change it to a Number:

Number Feuchtigkeit "Olivier: [%d]"
{ mqtt="<[broker:esp/erde/olivier/:state:default]" }
1 Like

Haha, of course :smiley: Thank you so much, that did it!

Please tick the solution, thanks

This doesn’t belong here but maybe the answer is just a simple: Why would you avoid “Thread::sleep” in favour of “createTimer”?

Thanks again!

See:

Thanks for the link, great guideline!