[Solved] Multiplication in rules

Hello,

I have a Aqara Temperature/Humidity/Pressure sensor and since it is a “little” to precise (XiaomiAqaraTHPSensorOutside_Pressure changed from 95.23999786376953 kPa to 95.23400115966797 kPa) and in gives the pressure only in kPa, i wanted to use rules and transformation to cast the output to a more suiteable format
but i struggle with the multiplication part

the rule:

rule "habpanel xiaomi HTP outside pressure"
 when
   Item XiaomiAqaraTHPSensorOutside_Pressure received update
 then
   var xiaomi_htp_outside_pressure = transform("JS", "javascript/xiaomi.js", XiaomiAqaraTHPSensorOutside_Pressure.state.toString) * 10
   vdXiaomiHTP_outside_pressure.postUpdate((xiaomi_htp_outside_pressure)) 
end

the transform

(function(i) {
    return parseFloat(i.slice(0, -6)).toFixed(1);
}
)(input)

the error msg

2018-04-25 21:39:10.570 [ERROR] [ntime.internal.engine.RuleEngineImpl] - Rule 'habpanel xiaomi HTP outside pressure': Unknown variable or command '*'; line 22, column 35, length 33

sure i could so it in the transformation, but since i use it also for the temperature and humidity, i would prefer if i could do it in the rule instead. At least i hope this is possible at all.

Thanks for your help
mueslee

The javascript transform returns a String which you can’t multiply by ten
This should work:

rule "habpanel xiaomi HTP outside pressure"
 when
   Item XiaomiAqaraTHPSensorOutside_Pressure received update
 then
   var Number xiaomi_htp_outside_pressure = (transform("JS", "javascript/xiaomi.js", XiaomiAqaraTHPSensorOutside_Pressure.state.toString) as Number) * 10
   vdXiaomiHTP_outside_pressure.postUpdate((xiaomi_htp_outside_pressure)) 
end

sadly doesn’t work

2018-04-25 22:30:26.390 [INFO ] [el.core.internal.ModelRepositoryImpl] - Validation issues found in configuration model 'habpanel_formatting.rules', using it anyway:
Cannot cast from String to Number
2018-04-25 22:30:26.426 [INFO ] [el.core.internal.ModelRepositoryImpl] - Refreshing model 'habpanel_formatting.rules'
2018-04-25 22:30:45.125 [ERROR] [ntime.internal.engine.RuleEngineImpl] - Rule 'habpanel xiaomi HTP outside pressure': Could not cast 95.3 to java.lang.Number; line 21, column 40, length 103

Sorry, I did that from memory
A quick searched turned out:

rule "habpanel xiaomi HTP outside pressure"
 when
   Item XiaomiAqaraTHPSensorOutside_Pressure received update
 then
   var Number xiaomi_htp_outside_pressure = Float.valueOf(transform("JS", "javascript/xiaomi.js", XiaomiAqaraTHPSensorOutside_Pressure.state.toString)) * 10
   vdXiaomiHTP_outside_pressure.postUpdate((xiaomi_htp_outside_pressure)) 
end
1 Like

that worked, thank you very much vzorglub