Type Conversions

rules
datatypes
Tags: #<Tag:0x00007fd3126914b8> #<Tag:0x00007fd312691378>

(Rich Koshak) #41

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.


(Alphie) #42

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.


(Rich Koshak) #43

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.


(Alphie) #44

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


(Kris K) #45

@rlkoshak

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

(Rich Koshak) #46

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.


(Kris K) #47

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

Thanks


(Vincent Regaud) #48

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)

(Kris K) #49

@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.


(Vincent Regaud) #50

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

See:https://www.openhab.org/docs/configuration/items.html#type


(Kris K) #51

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


(Vincent Regaud) #52

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


(Kris K) #53

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