Hello,
can somebody give me a hand about converting and working with float?
It’s probably super simple, but … yeah for now I don’t know
I do have mqtt which reports float 23.42 which is kWh (and no i can’t change source for this situation to report it Wh)
So I need to convert value 23.42 into 23420 to work with it somehow
in rules I know how to work with numbers like
(myitem.state as Number).intValue
but issue is that when I use that, it will convert my 23.42 into 23 only. So is there a way how to multiply my input by 1000 before using it as number.intvalue ?
or can i do math operations directly with float?
Thanks
basically I have changing input
consumption = 23.42 , 23.1, 22.4 etc. defined as Number in items
and then I need to populate min/max (which I know how ) and calculate range inbetween, which I dont know how without intValue
rule "power consumption min/max/range"
when
Item Pump_Consumption changed
then
val max = Pump_Consumption.maximumSince(now.withTimeAtStartOfDay)
val min = Pump_Consumption.minimumSince(now.withTimeAtStartOfDay)
if (max !== null && min !== null) {
val range = (Pump_Consumption_Max.state as Number).intValue - (Pump_Consumption_Min.state as Number).intValue
postUpdate(Pump_Consumption_Max, max.state)
postUpdate(Pump_Consumption_Min, min.state)
postUpdate(Pump_Consumption_Range, range)
}
if there is a better way, please kick me to the right direction
I doubt it makes much difference really. The types to avoid in rules where possible are the java primitives, which bring some kind of compiling overhead. Not always possible to avoid.
The closest thing to a “native” type for rules is the Number, but of course that can come with units of measurement baggage.
BigDecimal is BigDecimal. It’s not int, short, long, double, or float. When you call *value there is a conversion that takes place. BigDecimal stores numbers in decimal format meaning it can exactly represent most numbers. double represents numbers in IEEE format which has some gaps in the numbers that it can represent.
Use floatValue when you want to convert the BigDecimal to a 32 bit IEEE formatted floating point value. Use doubleValue when you want to convert BigDecimal to a 64 bit IEEE formatted floating point value. But in both cases a conversion is taking place.
Almost always float is sufficient. Just be sure to use whatever is required by the functions you may be calling.