UoM numbers in rules

Trying to get to grips with using Units of Measurement in rules.

In the case of an Item like -

Number:Temperature WTemp "Forecast" {some weather binding}

we get an Item like say 7 °C , where the “temperature flavour” is a part of the value and we can convert to °F etc. conveniently. OK so far.

If we want to do arithmetic or comparisons with that in rules, we’ve either got to cast everything else as temperature types or (more often) extract the numeric part only from the Item.

This is one way to get “the number”

var Number myvalue = (WTemp.state as QuantityType<Number>).doubleValue

but it seems a bit clumsy to me. Am I missing something?

======

Something else I accidentally fell into, until puzzled out (because I was doing the QT<Number> thing on its own.
An “unflavoured” Item can adopt a UoM on the fly. And later, unadopt it as well. Maybe that’s by design but it came as surprise to me.

ITEM
Number nm_test "test simple number Item"

rule "uom demo"
when
	Item test_switch changed
then
	logInfo("test", "AA " + nm_test.state.toString)
	nm_test.postUpdate(99)
	Thread::sleep(500)
	logInfo("test", "BB " + nm_test.state.toString)
	nm_test.postUpdate((WTemp.state as QuantityType<Number>))
	Thread::sleep(500)
	logInfo("test", "CC " + nm_test.state.toString)
	nm_test.postUpdate(77)
	Thread::sleep(500)
	logInfo("test", "DD " + nm_test.state.toString)
	nm_test.postUpdate((WTemp.state as QuantityType<Number>).doubleValue)
	Thread::sleep(500)
	logInfo("test", "EE " + nm_test.state.toString)
	nm_test.postUpdate(WTemp.state)
	Thread::sleep(500)
	logInfo("test", "FF " + nm_test.state.toString)
	nm_test.sendCommand(88)
	Thread::sleep(500)
	logInfo("test", "GG " + nm_test.state.toString)
	nm_test.sendCommand(88|"°C")
	Thread::sleep(500)
	logInfo("test", "HH " + nm_test.state.toString)
end
[.eclipse.smarthome.model.script.test] - AA NULL
[.eclipse.smarthome.model.script.test] - BB 99
[.eclipse.smarthome.model.script.test] - CC 6.87 °C
[.eclipse.smarthome.model.script.test] - DD 77
[.eclipse.smarthome.model.script.test] - EE 6.87
[.eclipse.smarthome.model.script.test] - FF 6.87 °C
[.eclipse.smarthome.model.script.test] - GG 88
[.eclipse.smarthome.model.script.test] - HH 88 °C

I suppose I was thinking of e.g. temperature as a property of the Item, whereas it acts like its the property of a state or value.

Using a test Item of type Number:Dimensionless forces ignores of update or commands with a °C property. I guess I was mistakenly expecting Number to be Dimensionless by default. In reality it seems AnyDimension :slight_smile:

EDIT - to clarify, Number:Dimensionless represents a proportion e.g. 75% rather than any arbitrary number.

4 Likes

this is how i’m doing it, too. i’m not in love with it. seemed easier to do before…

mmm, before UoM it seemed best to have a policy of “always use Number in rules - don’t mess with Decimal type, float etc.”
But Number now comes with units baggage.
I feel there ought to be a “toSimpleNumber” method