[solved] Compare a NumberItem in rule - how can it be done?

I want to trigger an event when the battery level of a devices is below a certain value

the item is DevoloMT02648ZWaveNode3_Battery
type is according to PaperUI Number:Dimensionless
for testing i use a Mi Button to activate the rule

with this rule i only see the “rule triggered” output to the log

rule "battery trigger"
when
   Channel "mihome:sensor_switch:158d0001ffff6a:button" triggered
then
    logWarn("myLog", "rule triggered")
    if ( DevoloMT02648ZWaveNode3_Battery.state < 50 ) {
        logWarn("myLog", "<50")
    }
    if ( DevoloMT02648ZWaveNode3_Battery.state > 50 ) {
        logWarn("myLog", ">50")
    } 
end

then i tried

rule "battery trigger"
when
   Channel "mihome:sensor_switch:158d0001ffff6a:button" triggered
then
    logWarn("myLog", "rule triggered")
    if ( (DevoloMT02648ZWaveNode3_Battery.state as DecimalType) < 50 ) {
        logWarn("myLog", "<50")
    }
    if ( (DevoloMT02648ZWaveNode3_Battery.state as DecimalType) > 50 ) {
        logWarn("myLog", ">50")
    } 
end

but that gave me this error:

2018-04-29 17:03:27.122 [ERROR] [ntime.internal.engine.RuleEngineImpl] - Rule 'battery trigger': Could not cast 100.0 % to org.eclipse.smarthome.core.library.types.DecimalType; line 38, column 10, length 59

after this I tried

if ( (Double::parseDouble((DevoloMT02648ZWaveNode3_Battery.state.toString).replace(" %",""))) < 50 ) {

ending in

2018-04-29 17:25:34.698 [ERROR] [ntime.internal.engine.RuleEngineImpl] - Rule 'battery trigger': Conversion = ')'

the question how can i access the number value without the %?

Thank you.

for the sake of completeness

  • openHAB 2.3.0~20180429034940-1 (Build #1265
  • raspberry Pi 3b+ with openHABian
rule "battery trigger"
when
   Channel "mihome:sensor_switch:158d0001ffff6a:button" triggered
then
    logWarn("myLog", "rule triggered")
    logWarn("myLog", DevoloMT02648ZWaveNode3_Battery.state.toString)
    if ( DevoloMT02648ZWaveNode3_Battery.state < 50 ) {
        logWarn("myLog", "<50")
    }
    if ( DevoloMT02648ZWaveNode3_Battery.state > 50 ) {
        logWarn("myLog", ">50")
    } 
end

What is your log?

1 Like

here is the log out

2018-04-29 18:18:45.332 [WARN ] [eclipse.smarthome.model.script.myLog] - rule triggered
2018-04-29 18:18:45.336 [WARN ] [eclipse.smarthome.model.script.myLog] - 100.0 %

Try that:

rule "battery trigger"
when
   Channel "mihome:sensor_switch:158d0001ffff6a:button" triggered
then
    logWarn("myLog", "rule triggered")
    logWarn("myLog", DevoloMT02648ZWaveNode3_Battery.state.toString)
    var battery = DevoloMT02648ZWaveNode3_Battery.state as Number
    logWarn("myLog", battery.toString)
    if ( Battery <= 0.5 ) {
        logWarn("myLog", "<=0.5")
    }
    if ( battery > 0.5 ) {
        logWarn("myLog", ">0.5")
    } 
end
1 Like

thank you so much @vzorglub it works
guess i own you a coffee for all your help :slight_smile:

2018-04-29 20:53:14.621 [WARN ] [eclipse.smarthome.model.script.myLog] - rule triggered
2018-04-29 20:53:14.626 [WARN ] [eclipse.smarthome.model.script.myLog] - 100.0 %
2018-04-29 20:53:14.630 [WARN ] [eclipse.smarthome.model.script.myLog] - 100.0 %
2018-04-29 20:53:14.640 [WARN ] [eclipse.smarthome.model.script.myLog] - >0.5

also works with

if ( (DevoloMT02648ZWaveNode3_Battery.state as Number) <= 0.5 ) {