Rules to convert HSB to hex RGB – issue at dimming

Hi,
I’ve installed hyperion on my RPi 3 (to control LEDs) and encountered the following problem with my rule.

To describe the aim of the rule – it should convert value from colorpicker to hex-formatted RGB (it has to be RRGGBB, not RGB) and then execute command: hyperion-remote -c RRGGBB.

The problem is, when I try to dim, e.g. green color, it is suddenly converted at particular level to red, not dimmed green.

Rule (partly modified by myself so it may be coded really badly :wink: ):

import org.openhab.core.library.types.*

rule “Set RGB value duzy led”

when

    Item duzy_led_hsb changed

then

    val hsbValue = duzy_led_hsb.state as HSBType
    val brightness = hsbValue.brightness.intValue
    val redValue   = ((((hsbValue.red.intValue * 255) / 100) * brightness) / 100)
    val greenValue = ((((hsbValue.green.intValue * 255) / 100) * brightness) / 100)
    val blueValue  = ((((hsbValue.blue.intValue * 255) / 100) * brightness) / 100)
    var red = Integer::toHexString( redValue )
    var green = Integer::toHexString( greenValue )
    var blue = Integer::toHexString( blueValue )
    val rr = red
    val gg = green
    val bb = blue
    if (red.length() < 2) {
            red = rr + rr
    }
    if (green.length() < 2) {
            green = gg + gg
    }
    if (blue.length() < 2) {
            blue = bb + bb
    }
    val hex = red + green + blue
    val komenda = 'hyperion-remote -c ' + hex
    executeCommandLine( komenda )

end

Logs (to see values being converted and the results):

11:33:05.642 [INFO ] [marthome.event.ItemStateChangedEvent] - duzy_led_hsb changed from 95.019915,96.289492,83.890374 to 95.019915,96.289492,32.820856
11:33:05.689 [INFO ] [lipse.smarthome.io.net.exec.ExecUtil] - executed commandLine ‘hyperion-remote -c bb1900’

Alternatively I can send TCP packet with JSON-formatted string but I had problems with the binding. When the hyperion was started, it didn’t work. However, it worked while listening with netcat, so I guess it is some hyperion – or port – issue, and so I tried another way – the one with command execution.

EDIT: Eventually I solved it after another approach with TCP binding (had to add postamble to make it work).

Hi @PawelGrzembka,

I’m also using hyperion and I’m interested in controlling it with openhab. At the moment I’m only using the hyperion app.
Can you maybe provide a little step by step guide how to connect both systems so hyperion can be controlled via openhab? :slight_smile:

I’ll try. I assume you are on OH2.

You need unbound and bound item, just like that:

Color duzy_led_hsb “LEDs” [ “Lighting” ]
String duzy_led_rgb {tcp=“>[*:localhost:19444:‘REGEX((.*))’]”}

Then, you need to install TCP Binding and then edit tcp.cfg – you need to uncomment lines:

#postamble=\r\n
#charset=ASCII
#queue=true

I’m not sure if the second and third edit is necessary but it works for me, so I guess it will work for you.

Also, you need a rule:

import org.openhab.core.library.types.*
rule “Set RGB value duzy_led”

when

    Item duzy_led_hsb changed

then

    val hsbValue = duzy_led_hsb.state as HSBType
    val brightness = hsbValue.brightness.intValue
    val redValue   = ((((hsbValue.red.intValue * 255) / 100) * brightness) / 100)
    val greenValue = ((((hsbValue.green.intValue * 255) / 100) * brightness) / 100)
    val blueValue  = ((((hsbValue.blue.intValue *255) / 100) * brightness) / 100)
    val String color = String::format("{\"color\":[%1$d,%2$d,%3$d],\"command\":\"color\",\"priority\":100}", redValue, greenValue, blueValue)
    sendCommand( duzy_led_rgb, color )

end

And add this to sitemap:

Colorpicker item=duzy_led_hsb

Of course, you need to change the item names etc. to your setup but you do need two items – unbound HSB one and the TCP bound RGB one.

I hope I escaped all special characters so copy-paste would work.

My hyperion server is running on another raspberry and I replaced the localhost:19444 with the ip:8888 from the other raspberry. Thats the web interface from my hyperion server but now I get these log entries the whole time so I assume it won’t work by simply changing the ip in the item.

13:12:44.093 [ERROR] [ing.tcp.AbstractSocketChannelBinding] - We do not accept outgoing connections for Items that do use address masks
13:12:44.345 [ERROR] [ing.tcp.AbstractSocketChannelBinding] - We do not accept outgoing connections for Items that do use address masks
13:12:44.569 [INFO ] [ing.tcp.AbstractSocketChannelBinding] - Attempting to reconnect the channel for localhost/127.0.0.1:19444
13:12:44.596 [ERROR] [ing.tcp.AbstractSocketChannelBinding] - We do not accept outgoing connections for Items that do use address masks
13:12:44.598 [WARN ] [ing.tcp.AbstractSocketChannelBinding] - The channel java.nio.channels.SocketChannel[closed] has encountered an unknown IO Exception: Connection refused

It looks like a problem with TCP binding as it is, not hyperion. As I’ve written before, I encountered also many issues but not that one.

Nevertheless, I find it suspective that it attempts to connect to localhost:19444 even though – as you said – you’ve changed it to other IP.

Maybe try at first to connect (and send some TCP packages) with netcat – to assure that the TCP connection works properly.
So install netcat on both RPis if you haven’t and then:

nc -l -p 8888 on the listening (hyperion one) machine
nc ip 8888 on the openhab machine

And try to type and send anything from one machine to another. You may need to stop hyperion at first (either with HyperCon or sudo systemctl stop hyperion).

That said, if this doesn’t give any clue, hint, I can’t help you any further. Maybe you should do some research or create a new topic (TCP binding ninjas won’t come here, I guess).

I think I solved the general connection problem. I missed a double dot while editing the ip and port.

But now I have another problem. When I use the colorpicker this appears in the log:

18:12:47.480 [INFO ] [ing.tcp.AbstractSocketChannelBinding] - The channel for /IP:8888 is now connected
18:13:23.419 [INFO ] [smarthome.event.ItemCommandEvent    ] - Item 'duzy_led_hsb' received command 106,39,100
18:13:23.436 [INFO ] [marthome.event.ItemStateChangedEvent] - duzy_led_hsb changed from 295,46,100 to 106,39,100
18:13:23.508 [INFO ] [smarthome.event.ItemCommandEvent    ] - Item 'duzy_led_rgb' received command {"color":[178,255,155],"command":"color","priority":100}
18:13:23.519 [INFO ] [marthome.event.ItemStateChangedEvent] - duzy_led_rgb changed from HTTP/1.1 400 Bad Request
Server: nginx/1.6.2
Date: Sat, 04 Feb 2017 17:12:41 GMT
Content-Type: text/html
Content-Length: 172
Connection: close

<html>
<head><title>400 Bad Request</title></head>
<body bgcolor="white">
<center><h1>400 Bad Request</h1></center>
<hr><center>nginx/1.6.2</center>
</body>
</html> to {"color":[178,255,155],"command":"color","priority":100}
18:13:23.812 [INFO ] [marthome.event.ItemStateChangedEvent] - duzy_led_rgb changed from {"color":[178,255,155],"command":"color","priority":100} to HTTP/1.1 400 Bad Request
Server: nginx/1.6.2
Date: Sat, 04 Feb 2017 17:13:23 GMT
Content-Type: text/html
Content-Length: 172
Connection: close

<html>
<head><title>400 Bad Request</title></head>
<body bgcolor="white">
<center><h1>400 Bad Request</h1></center>
<hr><center>nginx/1.6.2</center>
</body>
</html>

18:13:23.826 [INFO ] [marthome.event.ItemStateChangedEvent] - duzy_led_rgb changed from HTTP/1.1 400 Bad Request
Server: nginx/1.6.2
Date: Sat, 04 Feb 2017 17:13:23 GMT
Content-Type: text/html
Content-Length: 172
Connection: close

<html>
<head><title>400 Bad Request</title></head>
<body bgcolor="white">
<center><h1>400 Bad Request</h1></center>
<hr><center>nginx/1.6.2</center>
</body>
</html>
 to HTTP/1.1 400 Bad Request
Server: nginx/1.6.2
Date: Sat, 04 Feb 2017 17:13:23 GMT
Content-Type: text/html
Content-Length: 172
Connection: close

<html>
<head><title>400 Bad Request</title></head>
<body bgcolor="white">
<center><h1>400 Bad Request</h1></center>
<hr><center>nginx/1.6.2</center>
</body>
</html>
18:13:29.061 [INFO ] [ing.tcp.AbstractSocketChannelBinding] - Attempting to reconnect the channel for /IP:8888
18:13:29.069 [INFO ] [ing.tcp.AbstractSocketChannelBinding] - The channel for /IP:8888 is now connected

I see a lot of “400 bad requests” of the nginx server but I don’t know how to solve them. I tried to make the “large_client_header_buffers” bigger (I read this on different sites) but unfortunately it didn’t help solving this problem yet.

I know nothing about nginx, so definitely can’t help here :frowning: