Color item TCP binding

Sorry for the bump, but I’ve been struggling with this for a few days now using 1.8.3, and ended up with the following ‘working’ example:

Item:
Color LEDcolor "LEDcolor (RGB Values)" <video> (G1F)

Sitemap:
Frame label=“Hyperion” {
Colorpicker item=LEDcolor icon=“colorwheel”
}

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

rule "Set RGB Value"
when
    Item LEDcolor changed
then    
    // Get RGB Values from hue as Integer - there must be a tidier way of doing this!
    val redValue   = (Math::round((LEDcolor.state as HSBType).red.intValue)*2.5).intValue
    val greenValue = (Math::round((LEDcolor.state as HSBType).green.intValue)*2.5).intValue
    val blueValue  = (Math::round((LEDcolor.state as HSBType).blue.intValue)*2.5).intValue
    
    // If any colour is above zero
    if (redValue + greenValue + blueValue > 0 ) {
        // build the string using above values then send it to our external script
        var String json = String::format("{\"color\":[%1$d,%2$d,%3$d],\"command\":\"color\",\"priority\":100}", redValue, greenValue, blueValue)
        executeCommandLine( "/opt/openhab/scripts/hyperion.sh@@" + json)        
    } else {
        // As the RGB values are unset, we want to clear (switch off static colour)
        var json = "{\"command\":\"clear\",\"priority\":100}"
        executeCommandLine( "/opt/openhab/scripts/hyperion.sh@@" + json)
    }
end

/opt/openhab/scripts/hyperion.sh:
#!/bin/bash

echo $* | nc xbmc 19444
exit 0

This gives me a colour picker with up and down arrows for switching it on and off. It’s not my most elegant piece of code, but it was my first venture into OpenHAB. Controlling xbmc, sonos and an LG tv seems to be much less hassle than hyperion - for me at least.

Hi - someone can help me to adapt the rule below to my needs?
My idea is to use hyperion as a courtesy light. A movement sensor , when house is completely dark, must trigger a 30 seconds static light from hyperion.
It’s easy and i know how to do all that stuff, but my problem is that i’m not able to compose a valid json string to do that :frowning:

Edit: It is working now: the trick was the postamble in tcp.cfg

I have the problem that tcp item seems to be working but on the client side (hyperion) I do not see any reaction, so there must be something wrong with the command thats send out:

String 	hyperionDevice 	"Ambilight Device" 		<television>  	{tcp=">[*:192.168.108.55:19444:default]"}
Color 	hyperion_color	"Ambilight Color"		<television>	(gWohnzimmer)	

The rule seems to work and the command gets send:

import java.awt.Color
import org.openhab.core.library.types.*

var HSBType hsbValue
var int redValue
var int greenValue
var int blueValue
var String RGBvalues

rule "Set RGB value"
when
	Item hyperion_color received command
then

	val command = receivedCommand

	if (command instanceof HSBType) {
		var hsbValue = hyperion_color.state as HSBType

		redValue   = hsbValue.red.intValue
		greenValue = hsbValue.green.intValue
		blueValue  = hsbValue.blue.intValue			

		logInfo("hyperion", "Hyperion red: {}", redValue.toString)
		logInfo("hyperion", "Hyperion green: {}", greenValue.toString)
		logInfo("hyperion", "Hyperion blue: {}", blueValue.toString)

		var String json = '{"color":[' + redValue.toString + ',' + greenValue.toString + ',' + blueValue.toString + '],"command":"color","priority":100}'

		logInfo("hyperion", "Hyperion JSON: {}", json)
		hyperionDevice.sendCommand(json)
		}
end

I played with some settings here - that made the binding itself working:

# all parameters can be applied to both the TCP and UDP binding unless
# specified otherwise

# Port to listen for incoming connections
#port=25001

# Cron-like string to reconnect remote ends, e.g for unstable connection or remote ends
#reconnectcron=0 0 0 * * ?

# Interval between reconnection attempts when recovering from a communication error,
# in seconds
#retryinterval=5

# Queue data whilst recovering from a connection problem (TCP only)
# queue=true

# Maximum buffer size whilst reading incoming data
#buffersize=1024

# Share connections within the Item binding configurations
#itemsharedconnections=true

# Share connections between Item binding configurations
#bindingsharedconnections=true

# Share connections between inbound and outbound connections
#directionssharedconnections=false

# Allow masks in ip:port addressing, e.g. 192.168.0.1:* etc
#addressmask=true

# Pre-amble that will be put in front of data being sent
#preamble=

# Post-amble that will be appended to data being sent
#postamble=\r\n
postamble=\n

# Perform all write/read (send/receive) operations in a blocking mode, e.g. the binding
# will wait for a reply from the remote end after data has been sent
#blocking=false

# timeout, in milliseconds, to wait for a reply when initiating a blocking write/read
#  operation
#timeout=3000

# Update the status of Items using the response received from the remote end (if the
# remote end sends replies to commands)
#updatewithresponse=true
#updatewithresponse=true

# Timeout - or 'refresh interval', in milliseconds, of the worker thread
#refreshinterval=250

# Timeout, in milliseconds, to wait when "Selecting" IO channels ready for communication
#selecttimeout=1000

# Used character set
#charset=ASCII

Sending on command line works:

echo '{"command" : "color", "priority" : 100, "color" : [255,0,0]}' | nc 192.168.108.55 19444

on tcpdump I on the client side I see the command with very small difference
but I cannot get it working correct

from command line:
'7 f(nEi@@Jll74KL 
kh6aTq{"command":"color","priority":100,"color":[255,0,0]}

from the item:
'7'~NEj@@U,l6l7KW'yg
`^{"color":[95,34,97],"command":"color","priority":100}

Hey :slight_smile:

i running hyperion on libreelec am i able to just use Switch ON/OFF and late with amazon echo?

Like "Alexa, turn off “Ambilight” and Alexa, turn on ambilight?

I defined rules that I switch ON or OFF to activate ambilight or the color effects. I do not use Alexa, I do that with SIRI

Hey all,

I tried to send an UDP message after a color picker was updated.
I used the code from the beginning of this topic.
But i cannot send a message, the only thing it generates is errors

I hope someone can help me out with my code.
The code is placed in RGB_TV.rules

import org.openhab.core.library.types.HSBType
import org.openhab.core.library.types.Switch

rule RGB_TV
when
  Item RGB_TV received command or
  Item RGB_TV_Power received command
then  
  HSBType hsb = receivedCommand as HSBType
  String UDP "UDP_Send" {udp=">[0.0.0.0:25001:'REGEX((.*))']"}
  
  if (receivedCommand instanceof HSBType) 
  {
    val String Data = "1,"+"1,"+hsb.red+","+hsb.green+","+hsb.blue
	UDP.sendCommand(Data)
  } 
  else if (receivedCommand instanceof OnOffType) 
  {
	if (receivedCommand == ON ) 
	{
		val String Data = "1,"+"1,"+hsb.red.intValue.toString+","+hsb.green.intValue.toString+","+hsb.blue.intValue.toString
		UDP.sendCommand(Data)
	}
	else 
	{
		val String Data = "1,0,0,0,0"
		UDP.sendCommand(Data)
	}
  }
end

String UDP is also an item.

  • Jordo

Does not belong into a rule. This should be in your items file. If you still get errors, please post them as well.

Hey gersilex,

Thanks for the fast reply
I added UDP.items with content:
String UDP_Bedroom “UDP_Send” {udp=">[192.168.178.28:3333:‘REGEX((.*))’]"}

But the following errors still come on when changing state of the color picker or switch:

When pressing the switch
2017-08-06 19:57:20.604 [ERROR] [.script.engine.ScriptExecutionThread] - Rule 'RGB_TV': org.eclipse.smarthome.core.library.types.HSBType

when changing the color picker
2017-08-06 20:08:09.957 [INFO ] [el.core.internal.ModelRepositoryImpl] - Validation issues found in configuration model 'RGB_TV.rules', using it anyway:
This expression is not allowed in this context, since it doesn't cause any side effects.

2017-08-06 20:18:56.468 [ERROR] [.script.engine.ScriptExecutionThread] - Rule 'RGB_TV': An error occurred during the script execution: Couldn't invoke 'assignValueTo' for feature JvmVoid:  (eProxyURI: RGB_TV.rules#|::0.2.0.2.0.1::0::/1)

Hey all

I changed my code to:
based on a few other posts on de openhab community

var HSBType hsbValue
var String  redValue
var String  greenValue
var String  blueValue

rule RGB_TV
when
  Item RGB changed
then  
  if (RGB_Power.state == ON) 
  {
	hsbValue = RGB.state as HSBType

    redValue   = hsbValue.red.intValue.toString
    greenValue = hsbValue.green.intValue.toString
    blueValue  = hsbValue.blue.intValue.toString

    var Data = redValue+","+greenValue+","+blueValue
	sendCommand(UDP, Data)
  } 
  else
  {
	var Data = "0,0,0"
	sendCommand(UDP, Data)
  }
end

“UDP” in sendCommand refers to UDP.items with content:

String UDP “UDP_Send” {udp=">[0.0.0.0:0000:‘REGEX((.*))’]"}

There are no errors anymore, but there is still no UDP message send.
I hope someone can help me

  • Jordo

Turns out,
There needs to be at least one UDP setting in the config file, otherwise the binding won’t send UDP messages…

Soo, it is sending messages now, but only this:

?REGEX((.*))?

Looks like the last part of my UDP send item…

String UDP “UDP_Send” {udp=">[0.0.0.0:0000:‘REGEX((.*))’]"}

Someone who knows how to fix this problem?

  • Jordo

Update
The problem is solved
Thanks for your help Gersilex

String UDP         "UDP" 		{ udp=">[0.0.0.0:0000:default]" }

It is working now