receivedCommand to Integer

Hey there,

first of all it bugs me so much that I can’t get this to work, and I tried everything I found on the forum for like 40 mins now but it it never works :frowning:

Disclamer out of the way: I want to cast receivedCommand to an integer so I can calculate with it.
The goal is simple: I need a number that I can send via postUpdate to an Item and is calculated with 100 - receivedCommand.

I got the receivedCommand to a number but can’t subtract now. Essential this is what I have at the moment

var Number zahl =  receivedCommand.toString()

(Running on OpenHAB 2)

I am sure that there is a super simple solution. Can someone please point me into the right direction. Thanks!


Have you tried:

(receivedCommand as DecimalType).intValue
1 Like

Firstly, there is no receivedCommand on an update to an Item. So the Item needs to be commanded, not updated.

Secondly, what is the type of the Item? Does it have units? Whether or not it does changes how the casting and calculation needs to be done.

The code you have won’t work. receivedCommand.toString will give you a String. A String is not a Number. You can’t do math with a String.

Assuming the Item is just a plain old Number Item,

var Number zahl = receivedCommand

should be sufficient. Alternatively you could use:

var zahl = receivedCommand as Number

Both are basically equivalent.

If you have a number with units though, you need to do the math with “like” units. For example, if you are working with Temperatures in °C you would need to do something like:

100 | °C - receivedCommand

I think Rules DSL will be smart enough and not require you to cast the receivedCommand to a QuantityType.

Yes, but this resulted in the following error

An error occurred during the script execution: Could not invoke method: org.eclipse.smarthome.model.script.actions.LogAction.logInfo(java.lang.String,java.lang.String,java.lang.Object[]) on instance: null

same error with richs suggetions var Number zahl = receivedCommand and var zahl = receivedCommand as Number

Sorry for not specifing where my receivedCommand comes from. It comes from a rollershutter item:

Rollershutter  Verdunklung_Jalousie  "Jalousie[%d %%]" 

which is controlled in the sitemap.

Even a simple logInfo("test", receivedCommand) results in the same error as shown above.

I hoped that the Number in var Number zahl = receivedCommand.toString() would cast it to a number

With this rules I want to invert a rollershutter. It could be solved by a js tranformation however then only the value in the sitemap is updated correctly. I want to use the inverted values in rules too.

It appears that the error

An error occurred during the script execution: Could not invoke method: org.eclipse.smarthome.model.script.actions.LogAction.logInfo(java.lang.String,java.lang.String,java.lang.Object[]) on instance: null

only showed up because I wanted to log a number. Getting the actual number worked all the time^^.

So this is my solution:


Rollershutter    Verdunklung_Jalousie_Proxy    "HILFSITEM"          {channel="amazonechocontrol:smartHomeDevice:69cd07f6:4c3d9c72-4dfb-492c-a27b-54e12c27be83:percentage"}
Rollershutter    Verdunklung_Jalousie          "Jalousie[%d %%]"    	//Needed to invert (100-x)


rule "Jalousie"
    Item Verdunklung_Jalousie received command
    var number = 100 - (receivedCommand as DecimalType).intValue
    logInfo("Rule triggered", Dateiname + ": \"Jalousie\": Trigger: " + + ", Empfangen: " + receivedCommand + ", Gesetzt: " + number.toString())


Thanks to both of you =)

That changes everything. A Rollershutter carries a PercentType, not a DecimalType. That could be causing problems here. That is definitely why Justin’s suggestion failed.

Also, any Item can also carry NULL or UNDEF as a state and neither of those are a Number. Make sure the Item has a state that isn’t NULL or UNDEF.

Further more, receivedCommand is a command, not a state. In some cases it can be both, for example ON and OFF can be either a state or a command. But Rollershutters can accept all sorts of commands that are not states, such as UP, DOWN, STOP. If you send UP to the Rollershutter, receivedCommand will be UP. And UP isn’t a Number. You need to get the Item’s state.

You can only cast something to a type that it already is. DecimalType inherits from Number. In other words DecimalType is a Number. So if you have a DecimalType you can say “I don’t care, I want to use it as a Number” and everything is OH because DecimalType is a Number. There is no transformation going on.

A PercentType inherits from DecimalType. And therefore it inherits from Number so you can cast it to a Number.

String does not inherit from Number. You can’t treat it like a Number because it isn’t one already.

All casting does is say despite what type the Object is, I want to treat it as a Number (or whatever). Therefore if you have a DecimalType and an Integer you can cast them both to Number and treat them the same.

As I mentioned above, that rule will only work if you never send UP/DOWN/STOP commands to the Rollershutter.

A different approach would be to use changed as the trigger and use the Item’s state. Or you will have to make the rule check to see what type of command it is and forward the non-number type commands unchanged.

A final parting note, typically the Item that is not linked to the device is called the “Proxy”. I only bring it up as it might become a source of confusion in future posts.

My bad for not specifying this in my first post^^

Thanks for the hint. However this are non-issues for me as is as the triggering item only receives numbers since it is controlled via a slider in the sitemap so it only can receive 0-100. Maybe I will have to adjust the rule when I want to also be able handel UP/DOWN and STOP-commands

Thanks for clarification!

To conclude: Thank you Rich for your detailed explanation we have come to expect from your posts :stuck_out_tongue: