Type Conversions

The graph is a good indication that things are working. You can user the OH REST API to do some a queries against the db directly.

Coming from a C# background, I have an expectation that invalid comparison operations will fail in at compile time most often, and maybe throw an exception at runtime under special cases. My troubleshooting in OH rules is usually complicated by the fact that everything just appears to work until you add a logInfo after every line!

Could you give me some guidance on the Number:Temperature type? I only came across this item type when I started using the Wunderground binding. Part of the problem is that I don’t really understand what types with colons really mean, and I can’t find any documentation about it. I presumed it’s like a subclass which simply overrides the the ToString() method (or whatever it’s called), but have found that comparisons to numbers always return false (without an exception) and casting to a Number (as Number) seems to have no effect. Consider this rule snippet:

var float _maxOutsideTemp = 23.0
if ((Weather_MaxTemp_0.state as Number) < _maxOutsideTemp)
    logInfo("my.rules", "max temp (" + (Weather_MaxTemp_0.state as Number) + ") < " + _maxOutsideTemp)

This gives the non-intuitive result:
max temp (20 ℃) >= 23.0

That makes me wonder whether the left side of the comparison is being treated as string, but surely that should cause an exception or, ideally, a compilation error.

Is there an incantation to allow a useful comparison of a Number:Temperature item with a rule-defined variable? Comparison between two items of the Number:Temperature type seems to be OK.

That is not a valid expectation for weakly typed languages like the Rules DSL. There is no way for the compiler to have enough information to know if there will be an hour or not.

Unfortunately no I can’t. I’ve no experience with it and it is a new feature of OH 2.3. My understanding is that types like that allows one to set a flag somewhere to tell OH what units you prefer. Then it will automatically convert what ever the binding returns to your desired units.

Assuming I understand it correctly, you shouldn’t have to do anything special to use these items on your sitemap or rules.

I’m pretty positive it is far more involved than that.

They both implement Comparator so you wouldn’t get a compile time exception.

I suggest posting this as a new thread so more people see it. I can help unfortunately.

Thank you for your reply, Rich. I started a new thread here.


Hi Rich,

I can see youve listed a Type conversion for a dimmer.

My current volume control of a thing is expressed as a Dimmer.

Items File:

Dimmer      Zone_1_Volume               "Volume [%.1f %%]"          <soundvolume                                                                                                     >

I am trying to create a Widget that does not use a Dimmer Type for Volume, I’m hoping to use commands to change the Volume like this:

    <div class="widget" ng-class="{true: 'disabled'}[itemValue('Main_Zone_Power')=='PowerOff']">
			<div class="icon off"><svg viewBox="0 0 48 48"><use xlink:href="/static/matrix-theme/squidink.svg#volume"></use></svg></div>
			<div class="name">Volume<div class="value">{{itemValue('Zone_1_Volume')}}</div></div>
			<div class="controlGroup">
				<div class="control" ng-click="sendCmd('Zone_1_Volume', 'Up')"><svg viewBox="0 0 80 80"><use xlink:href="/static/matrix-theme/squidink.svg#top-arrow-2"></use></svg></div>
				<div class="control" ng-click="sendCmd('Zone_1_Volume', 'Down')"><svg viewBox="0 0 80 80"><use xlink:href="/static/matrix-theme/squidink.svg#down-arrow-2"></use></svg></div>

Am I right in saying I can use your conversion to convert the dimmer to a Number item? How would I then reference that new number state in a Widget? Do I create an item for it?

//to get state as Number
val dimVal = Zone_1_Volume.state as Number

if(dimVal > 50)...

val newDimVal = dimVal + 10

val int dimAsInt = dimVal.intValue

val float dimAsFloat = dimVal.floatValue

New items file:

Number dimVal

I don’t know anything about HABPanel so you might be better off posting a new thread in the HABpanel section of the forum.

That would be a requirement to convert a Dimmer item too a Number Item. Bed aware that the conversion code above is in Rules DSL, not JavaScript which is what HABPanel widgets after written in.

Hi Rich,

Forgetting habpanel, assuming a Basic UI sitemap how would I achieve this?

Just this piece of code? in a rule? im a little confused and new to OH2


A DimmerType is a child of NumberType so you should be able to do all the same things to it.

Why don’t you reference the dimmer item directly? Does that not work?

If not then you will need another Number item and do this:

newNumber.postUpdate(Zone_1_Volume.state as Number)

@vzorglub HI Vincent, ill have already tried a direct reference to it with the sendCommand(Dimmer_Item, ‘UP’)

But I dont know what the dimmer item will respond to for the command. Ie, Up/Down, -/+ dont work.

A Dimmer item will accept the following commands (Number, Increase/Decrease, ON/OFF)


1 Like

Thank you :slight_smile: Ill try Increase/Decrease before I look at the Type Conversion.

Just like the ON/OFF, it’s INCREASE/DECREASE… No quotes necessary in the rules. I don’t know about html stuff.

Doesnt work :frowning:

17:37:44.013 [WARN ] [.rest.core.internal.item.ItemResource] - Received HTTP POS T request at ‘items/Zone_1_Volume’ with an invalid status value ‘Decrease’.

but with Capitals it does :smiley: kinda!

17:39:36.376 [INFO ] [smarthome.event.ItemStateChangedEvent] - Zone_1_Volume changed from 52 to 58

17:40:11.514 [INFO ] [smarthome.event.ItemCommandEvent     ] - Item 'Zone_1_Volume' received command DECREASE
17:40:11.533 [INFO ] [smarthome.event.ItemStateChangedEvent] - Zone_1_Volume changed from 64 to 60
17:40:12.201 [INFO ] [smarthome.event.ItemCommandEvent     ] - Item 'Zone_1_Volume' received command DECREASE
17:40:12.217 [INFO ] [smarthome.event.ItemStateChangedEvent] - Zone_1_Volume changed from 60 to 63
17:40:13.069 [INFO ] [smarthome.event.ItemCommandEvent     ] - Item 'Zone_1_Volume' received command DECREASE
17:40:13.086 [INFO ] [smarthome.event.ItemStateChangedEvent] - Zone_1_Volume changed from 63 to 59
17:40:13.828 [INFO ] [smarthome.event.ItemCommandEvent     ] - Item 'Zone_1_Volume' received command DECREASE
17:40:13.844 [INFO ] [smarthome.event.ItemStateChangedEvent] - Zone_1_Volume changed from 59 to 61
17:40:14.742 [INFO ] [smarthome.event.ItemCommandEvent     ] - Item 'Zone_1_Volume' received command DECREASE
17:40:14.760 [INFO ] [smarthome.event.ItemStateChangedEvent] - Zone_1_Volume changed from 61 to 58

I keep using DOWN in this scenario but it goes up a bit… lol!

EDIT: The AVR shows it going up and down fine, problem solved :smiley: Thanks Vincent

Thanks for your DP threads - I almost always find a better way here than my existing ways of doing stuff.

HOwever, that does not work anymore:

The method getCalendar() from the type DateTimeType is deprecated

I just wanted to extract the hour of my DateTime Items (Sunrise / Sunset).
Thanks in advance


Thanks - got it:

val String rise = Weather_Sunrise_Time.state.format("%1$tH")
val Number hour_rise = Integer::parseInt(rise)