Jython, Color item, HSBType & updates

Hi there,

I recently started working on translating my old DSL rules to Jython. So far, I have managed to get most things working through trial and error and the documentation (the community has put in a huge amount of work).

Some things are missing though (or I couldn’t find the right pages).

I have a Color item (let’s say test_color) and a String item (let’s say test_string).

When the item test_string receives a command like “240,105,80”, it used to be split into an array and then an HSBType object was created and used as state for a postUpdate command to test_color.

Let’s try it with Jython:

#get the command
command = str(event.itemCommand).split(",")
#get RGB values
redValue = int(command[0])
greenValue = int(command[1])
blueValue = int(command[2])

HSBvalue = HSBType().fromRGB(redValue,greenValue,blueValue)
events.postUpdate(“test_color”, HSBvalue)

The postUpdate command fails, saying that it can’t coerce the 1st argument to item or string (I have also tried events.postUpdate(items[“test_color”], HSBvalue) but I get the same problem.

test_color is an HSBType. How can I update it (or send a command to it)?

What?! Where?!

It looks like you missed this one. Specifically, the link to ‘More details’.

https://openhab-scripters.github.io/openhab-helper-libraries/Guides/But%20How%20Do%20I.html#send-an-update-to-an-item

If the first argument for events.postUpdate is a string, then the second one needs to be a string too.

    #get the command
    command = event.itemCommand.toString().split(",")
    #get RGB values
    redValue = int(command[0])
    greenValue = int(command[1])
    blueValue = int(command[2])

    HSBvalue = HSBType().fromRGB(redValue, greenValue, blueValue)
    events.postUpdate(“test_color”, HSBvalue.toString())

First of all, thanks! That did it. I It makes no sense to me (converting a complex object to string), but it works, so…
I was betting on the angle of events.postUpdate(Item, State), where State would be an HSBType object.

I found a lot of info on jrs223, like a very detailed installation guide and some simple examples, as well as a “shallow” reference of the objects on the page you sent. There’s also a helpful guide by @rlkoshak. Granted, much more is needed, but I wanted to acknowledge the work that was already put in.

Thanks again!

But what you used was events.postUpdate(string, ... and if the first argument is a string, then postUpdate expects the second to be a string as well. It’s just the way postUpdate works.

I also tried:

which also failed :slight_smile:

Given that the incoming command is a String of the format “H,S,B” and the toString() of an HSBType is a String of the format “H,S,B” and events.postUpdate() requires the second argument to be a String, you don’t even need to parse the String and create the HSBType. The whole Rule could be:

events.postUpdate("test_color", str(event.itemCommand))

items["test_color"] gives you the State of the Item test_color, not the Item itself. If you want the Item itself you need to pull it from the Item Registry.

ir.getItem("test_color")

so the command would need to be

events.postUpdate(ir.getItem("test_color"), HSBvalue)

I’m happy you found anything I’ve worked on helpful!

Actually, I don’t think that would work in this case. The command is an “RGB” string and I need to send an “HSB” string. Also, I’m normalising the values, because of the dirt-cheap LEDs that I have around the house :blush:

Thanks for clearing up the difference between item[…] and ir.getItem[…]. I was confused about that, too.