# Getting Percentage from items with UoM

I’m trying to make a simple rule/script using DSL:

I have to items both are of type Number:DataAmount (measuring in MiB)

I am trying to make them give me a percentage and writing that to a Number:Dimensionless item.
But the only result I am getting is 0 %

From the countless forum threads I’ve read I’ve tried numerous things, but currently it looks like this:

``````var item1 = (OH_RPi_Memory_Free.state as Number).intValue / (OH_RPi_Memory_Total.state as Number).intValue
logInfo("MemTest","value of command is: {}", item1)
``````

I have tried replacing Number with QuantityType, not using intValue, and a few more attempts, incluing specifying “item1” variable as Number, all results in 0 as output, and when only trying to log a single item, I get “2.0 E-8 MiB”
Because I can not get any values, I’ve tried splitting the problem into 2, first is getting the ratio - only then will I start tackling the percentage issue, with getting the ratio converted into a percentage.

Clearly there are some concepts I am not understanding!
Somebody please help, as I feel this should be a 5 min tasks that has now gone on for 3 hours… Did you try doubleValue or multiplying by 1.0 (.0 is important!)?

If you divide an int by an int you’ll end up with an int, so 4/2=2, 3/2=1, 2/2=1, 1/2=0y

doubleValue yes I did try.

But not multiplying by 1.0…which gives me some actual values - progress! Now I have an item that is to hold the result, which now gives a value “0.34…” The item is of type Number:Dimensionless.
I’ve given it a state description pattern “%d %%” - some forum threads say this should result in OH turning this into a “real” percentage on its own, so in my case from 0.34 to 34%. This does not happen.
Am I doing something wrong?
I do realise I can easily fix this by multiplying my result by 100 in my script, but if the Number:Dimensionless is supposed to behave differently, I’d much rather learn how it is really supposed to work.

In what way do you expect OH to change it? In what way are you looking for this change?

In addition to the int division problem @J-N-K identifies, how are you updating the Item? If your Item is `item1` than that’s not how you update an Item. If you are expecting `item1` to magically car units, that’s not how `int`, `float`, etc. work.

If you have a `Number:Dimensionless` named `MyPercentItem`, to update that Item with units you first of all need to call `postUpdate` and second of all you need to pass the units.

Note: I would divide using `floatVaue` instead of `intValue` and then you can avoid the whole multiply by 1.0 to force it to a float.

``````val percent = ((OH_RPi_Memory_Free.state as Number).floatValue / (OH_RPi_Memory_Total.state as Number_.floatValue) * 100
MyPercentItem.postUpdate(percent + ' %')
``````

I have exactly what you describe here configured.
And you answered the question I was having regarding the conversion from what I tried to describe as a decimal representation, ie. 0.34, to a percentage, in your example, by multiplying with 100, and then adding the units within the item update statement.
It is now as neat as I would expect, and like it to be.

Thank you gentlemen!