Type Conversions

Edit: It seems to work now.

val Number Waschmaschine_MODE_OFF = 0
val Number Waschmaschine_MODE_STANDBY = 1
val Number Waschmaschine_MODE_ACTIVE = 2
val Number Waschmaschine_MODE_FINISHED = 3
val Number Waschmaschine_MODE_CREASE_PROTECTION = 4

var Number Waschmaschine_Counter = 0

//#########################################################################//
//Waschmaschine Status anhand des aktuellen Stromverbrauches setzen                               //
//Wenn Status Fertig, dann Telegram senden                                                                            //
//#########################################################################//

rule "Waschmaschine Status"
when
    Item Steckdose_Waschmaschine_Strom changed
then
  logInfo("WaMa Strom Durchschnitt 30 min", Steckdose_Waschmaschine_Strom.averageSince(now.minusMinutes(30)).toString)
  if (Steckdose_Waschmaschine_Strom.state < 1) Waschmaschine_OpState.postUpdate(Waschmaschine_MODE_OFF)
  else if (Steckdose_Waschmaschine_Strom.state > 100  && Waschmaschine_OpState.state !== (Waschmaschine_MODE_CREASE_PROTECTION) || Steckdose_Waschmaschine_Strom.state > 500){
    Waschmaschine_OpState.postUpdate(Waschmaschine_MODE_ACTIVE)
  }
  if (Waschmaschine_OpState.state == (Waschmaschine_MODE_ACTIVE) && Steckdose_Waschmaschine_Strom.state < 2){
    Waschmaschine_Counter = Waschmaschine_Counter + 1
  }
  if (Steckdose_Waschmaschine_Strom.state > 1 && (Steckdose_Waschmaschine_Strom.averageSince(now.minusMinutes(2)) as Number) < 50) {
    if (Waschmaschine_OpState.state == Waschmaschine_MODE_OFF) Waschmaschine_OpState.postUpdate(Waschmaschine_MODE_STANDBY)
  }
  if (Waschmaschine_Counter > 6){
    if (Waschmaschine_OpState.state == Waschmaschine_MODE_ACTIVE){
      Waschmaschine_Counter = 0
      Waschmaschine_OpState.postUpdate(Waschmaschine_MODE_CREASE_PROTECTION)
    }
  }
  if ((Steckdose_Waschmaschine_Strom.averageSince(now.minusMinutes(30)) as Number) < 150){
    if (Waschmaschine_OpState.state == (Waschmaschine_MODE_CREASE_PROTECTION)) Waschmaschine_OpState.postUpdate(Waschmaschine_MODE_FINISHED)
  }
 end  

Thanks a lot for your help and patience !

Hm, seems the problem is only solved partially…this doesn’t look to me like an average being calculated?

2018-03-18 17:07:07.447 [INFO ] [cript.WaMa Strom Durchschnitt 30 min] - 280.96
2018-03-18 17:07:12.612 [INFO ] [cript.WaMa Strom Durchschnitt 30 min] - 0.0
2018-03-18 17:07:14.311 [INFO ] [cript.WaMa Strom Durchschnitt 30 min] - 786.56000000000
2018-03-18 17:07:15.984 [INFO ] [cript.WaMa Strom Durchschnitt 30 min] - 505.92
2018-03-18 17:07:27.923 [INFO ] [cript.WaMa Strom Durchschnitt 30 min] - 0.0
2018-03-18 17:07:29.589 [INFO ] [cript.WaMa Strom Durchschnitt 30 min] - 421.76
2018-03-18 17:07:41.401 [INFO ] [cript.WaMa Strom Durchschnitt 30 min] - 0.0
2018-03-18 17:07:44.780 [INFO ] [cript.WaMa Strom Durchschnitt 30 min] - 561.92
2018-03-18 17:07:56.701 [INFO ] [cript.WaMa Strom Durchschnitt 30 min] - 0.0
2018-03-18 17:07:58.381 [INFO ] [cript.WaMa Strom Durchschnitt 30 min] - 646.72
2018-03-18 17:08:00.065 [INFO ] [cript.WaMa Strom Durchschnitt 30 min] - 392.960000000000
2018-03-18 17:08:10.277 [INFO ] [cript.WaMa Strom Durchschnitt 30 min] - 0.0
2018-03-18 17:08:13.642 [INFO ] [cript.WaMa Strom Durchschnitt 30 min] - 421.76
2018-03-18 17:08:25.525 [INFO ] [cript.WaMa Strom Durchschnitt 30 min] - 0.0
2018-03-18 17:08:27.212 [INFO ] [cript.WaMa Strom Durchschnitt 30 min] - 843.52
2018-03-18 17:08:28.890 [INFO ] [cript.WaMa Strom Durchschnitt 30 min] - 449.92
2018-03-18 17:08:40.781 [INFO ] [cript.WaMa Strom Durchschnitt 30 min] - 0.0
2018-03-18 17:08:42.481 [INFO ] [cript.WaMa Strom Durchschnitt 30 min] - 533.76
2018-03-18 17:08:54.383 [INFO ] [cript.WaMa Strom Durchschnitt 30 min] - 0.0
2018-03-18 17:08:56.063 [INFO ] [cript.WaMa Strom Durchschnitt 30 min] - 252.96
2018-03-18 17:08:57.762 [INFO ] [cript.WaMa Strom Durchschnitt 30 min] - 561.92
2018-03-18 17:09:09.670 [INFO ] [cript.WaMa Strom Durchschnitt 30 min] - 0.0
2018-03-18 17:09:11.329 [INFO ] [cript.WaMa Strom Durchschnitt 30 min] - 674.5600000000001
2018-03-18 17:09:13.035 [INFO ] [cript.WaMa Strom Durchschnitt 30 min] - 392.960000000000
2018-03-18 17:09:23.240 [INFO ] [cript.WaMa Strom Durchschnitt 30 min] - 0.0

Seems the averageSince is not really processed…instead it returns the current value which is pointless :frowning:

(ok, so this isn’t about the casting anymore)

If you can check the database to see what is being saved. The problem could be a bunch of places so we need to start narrowing it down.

Hm, as the Graph is being drawn, I’d would need a hint where and how to check the rr4dj files please?

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.

@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

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

Thanks

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)

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

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

@NCO
See:

Thanks - got it:

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