Example: Convert Color Item Values To RGB (With Explanation)

As you defined red, green and blue as global values (i.e. a constant which must not change) it’s not possible redefine the val in a rule.

And what’s the received command?

I would expect the rule to fail at least if dimming or switch ON/OFF. A color Item can receive many different commands :slight_smile: so you have to test before using the command:

rule "get RGB Colors from ColorItem"
 when
    Item Pond_Colour received command

 then
    if(!(receivedCommand instanceof HSBType)) // if command is not of Type HSBType, leave the rule
        return;

    val red = (receivedCommand as HSBType).red 
    val green = (receivedCommand as HSBType).green 
    val blue = (receivedCommand as HSBType).blue

    Pond_Red_X.sendCommand(red)
    Pond_Green_X.sendCommand(green)
    Pond_Blue_X.sendCommand(blue)
end

If you want red, green and blue to be global, use var instead of val and don’t repeat the key word in the rule:

var Number red
var Number green
var Number blue

rule "get RGB Colors from ColorItem"
 when
    Item Pond_Colour received command

 then
    if(!(receivedCommand instanceof HSBType)) // if command is not of Type HSBType, leave the rule
        return;

    red = (receivedCommand as HSBType).red 
    green = (receivedCommand as HSBType).green 
    blue = (receivedCommand as HSBType).blue

    Pond_Red_X.sendCommand(red)
    Pond_Green_X.sendCommand(green)
    Pond_Blue_X.sendCommand(blue)
end

Hey! Thanks for the information!

Maybe a little more background on my use case… I have 3 coloured LEDs attached to 3 dimmers, they are not meant to be RGB, but making my own setup. I’m not bothered by any feedback from the lights etc. The Pond_Colour item should only be receiving input from the color picker in my sitemap. This rule then updates the ###_X number items with values between 0 and 100. This gives me the dimmer % required for each colour.

I found that when using the color picker, it was sending hundreds of commands as i dragged my finger across it etc, this was upsetting the dimmers as they received too many commands in a short space of time. Therefore, the ###_X items are not directly linked to the dimmers.

I have 3 more items that are the physical dimmers and I have another rule that runs 2 seconds after the colour items have been updated (timer cancels if another updated is received). So the dimmers only change every 2 seconds max.

So, the only input into the HSBType is from the colour picker in the sitemap, which is why it was strange to see that error in the log. Like i said, it all works great, just trying to remove the error :slight_smile:

I’ll try your idea above and see what happens!

Cheers!

Hello All,

runtimeInfo:
  version: 3.1.0
  buildString: Release Build
locale: default
systemInfo:
  configFolder: /etc/openhab
  userdataFolder: /var/lib/openhab
  logFolder: /var/log/openhab
  javaVersion: 11.0.12
  javaVendor: Azul Systems, Inc.
  javaVendorVersion: Zulu11.50+19-CA
  osName: Linux
  osVersion: 5.10.60-sunxi
  osArchitecture: arm
  availableProcessors: 4
  freeMemory: 68282840
  totalMemory: 253845504
bindings:
  - serial
clientInfo:
  device:
    ios: false
    android: false
    androidChrome: false
    desktop: true
    iphone: false
    ipod: false
    ipad: false
    edge: false
    ie: false
    firefox: false
    macos: false
    windows: true
    cordova: false
    phonegap: false
    electron: false
    nwjs: false
    webView: false
    webview: false
    standalone: false
    os: windows
    pixelRatio: 0.800000011920929
    prefersColorScheme: light
  isSecureContext: false
  locationbarVisible: true
  menubarVisible: true
  navigator:
    cookieEnabled: true
    deviceMemory: N/A
    hardwareConcurrency: 4
    language: en-US
    languages:
      - en-US
      - en
      - tr
    onLine: true
    platform: Win32
  screen:
    width: 1920
    height: 1080
    colorDepth: 24
  support:
    touch: false
    pointerEvents: true
    observer: true
    passiveListener: true
    gestures: false
    intersectionObserver: true
  themeOptions:
    dark: dark
    filled: true
    pageTransitionAnimation: default
    bars: light
    homeNavbar: default
    homeBackground: standard
    expandableCardAnimation: default
  userAgent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML,
    like Gecko) Chrome/94.0.4606.61 Safari/537.36
timestamp: 2021-09-24T08:15:43.999Z

i am trying the ColorPicker application on OH3.
i am created an RGB thing and a Dimmer channel and an RGB channel (Pictures and Thing Code are below)

RGB THING CHANNEL 1

UID: serial:serialDevice:SB1:RGB1
label: RGB1
thingTypeUID: serial:serialDevice
configuration:
  patternMatch: v
bridgeUID: serial:serialBridge:SB1
channels:
  - id: RGB1_DIMMER
    channelTypeUID: serial:dimmer
    label: DIMMER 1
    description: ""
    configuration:
      onValue: O2_2104*254#%d10
      offValue: O2_2104*0#
      commandFormat: O2_2104*%d#
  - id: RGB1_C1_String
    channelTypeUID: serial:string
    label: RGB1_C1_String
    description: ""
    configuration: {}

Dimmer channel working very well but the RGB don’t.

and RGB Rules is below

var Number red
var Number green
var Number blue
     
rule "RGB"
  when
    Item RGB1_RGB1C1String received command
  then
   // if (RGB1_Power.state === NULL) RGB1_Power.sendCommand(OFF)
  	if (!(receivedCommand instanceof HSBType))
      return
      logInfo("Color Command received", receivedCommand)
      red = (receivedCommand as HSBType).red
	  green = (receivedCommand as HSBType).green
	  blue = (receivedCommand as HSBType).blue
	  	
	  	RGB1_RGB1C1String.sendCommand(red)
	  	RGB1_RGB1C1String.sendCommand(green)
	  	RGB1_RGB1C1String.sendCommand(blue)
  	    logInfo("LED Color changed", "R" + red + "G" + green + "B" + blue)

  	if (receivedCommand == ON){
		 RGB1_RGB1C1String.sendCommand("O2_2111","R"+red+"B"+blue+"G"+green)
  	}
  	else if (receivedCommand == OFF){
		 RGB1_RGB1C1String.sendCommand("O2_2111","R"+0+"B"+0+"G"+0) // Fade-out lights very slowly. Implemented in the device code
  	}
end

and log file

2021-09-24 10:14:30.957 [INFO ] [openhab.event.ItemCommandEvent      ] - Item 'RGB1_RGB1C1String' received command 13,47,89
2021-09-24 10:14:30.962 [INFO ] [penhab.event.ItemStatePredictedEvent] - Item 'RGB1_RGB1C1String' predicted to become 13,47,89
2021-09-24 10:14:30.974 [INFO ] [openhab.event.ItemStateChangedEvent ] - Item 'RGB1_RGB1C1String' changed from 192,47,89 to 13,47,89
2021-09-24 10:14:30.988 [INFO ] [openhab.event.ItemCommandEvent      ] - Item 'RGB1_RGB1C1String' received command 89
==> /var/log/openhab/openhab.log <==
2021-09-24 10:14:30.997 [INFO ] [.core.model.script.LED Color changed] - R89G56.233166668061B47.17
==> /var/log/openhab/events.log <==
2021-09-24 10:14:30.999 [INFO ] [openhab.event.ItemCommandEvent      ] - Item 'RGB1_RGB1C1String' received command 56.233166668061
2021-09-24 10:14:31.003 [INFO ] [penhab.event.ItemStatePredictedEvent] - Item 'RGB1_RGB1C1String' predicted to become 89
==> /var/log/openhab/openhab.log <==
2021-09-24 10:14:31.015 [ERROR] [internal.handler.ScriptActionHandler] - Script execution of rule with UID 'RGB-1' failed: An error occurred during the script execution: Could not invoke method: org.openhab.core.model.script.actions.Log.logInfo(java.lang.String,java.lang.String,java.lang.Object[]) on instance: null in RGB
==> /var/log/openhab/events.log <==
2021-09-24 10:14:31.027 [INFO ] [openhab.event.ItemCommandEvent      ] - Item 'RGB1_RGB1C1String' received command 47.17
==> /var/log/openhab/openhab.log <==
2021-09-24 10:14:31.029 [ERROR] [internal.handler.ScriptActionHandler] - Script execution of rule with UID 'RGB-1' failed: An error occurred during the script execution: Could not invoke method: org.openhab.core.model.script.actions.Log.logInfo(java.lang.String,java.lang.String,java.lang.Object[]) on instance: null in RGB
==> /var/log/openhab/events.log <==
2021-09-24 10:14:31.031 [INFO ] [penhab.event.ItemStatePredictedEvent] - Item 'RGB1_RGB1C1String' predicted to become 56.233166668061
==> /var/log/openhab/openhab.log <==
2021-09-24 10:14:31.042 [ERROR] [internal.handler.ScriptActionHandler] - Script execution of rule with UID 'RGB-1' failed: An error occurred during the script execution: Could not invoke method: org.openhab.core.model.script.actions.Log.logInfo(java.lang.String,java.lang.String,java.lang.Object[]) on instance: null in RGB
==> /var/log/openhab/events.log <==
2021-09-24 10:14:31.051 [INFO ] [penhab.event.ItemStatePredictedEvent] - Item 'RGB1_RGB1C1String' predicted to become 47.17
2021-09-24 10:14:31.062 [INFO ] [openhab.event.ItemStateChangedEvent ] - Item 'RGB1_RGB1C1String' changed from 13,47,89 to 13,47,56.233166668061
2021-09-24 10:14:31.067 [INFO ] [openhab.event.ItemStateChangedEvent ] - Item 'RGB1_RGB1C1String' changed from 13,47,56.233166668061 to 13,47,47.17

btw, I don’t want HUE, SAT, etc. I need just Red Green and Blue data for sending to my physical device by Serial Binding.

where am i doing wrong?

Could you describe what you want to send, exactly?

This will send something like
R89G56.233166668061B47.17

This will send something like
92
35
40
Can your device handle these different message formats? Does it need integer values?

The error occurs because your rule sends these commands to itself, it’s re-triggered and tries to process its own output.

like as

RGB1_RGB1C1String.sendCommand("O2_2111","R"+red 
(0<>255)+"B"+blue(0<>255)+"G"+green(0<>255))

O2_2111RxxxBxxxGxxx

xxx mean= between 0 to 255

Yes, I need integer values, not float. And no need for other data, like HUE, Brightness Saturation, etc.

i didn’t understand what you mean.

Okay, clearly -

	  	RGB1_RGB1C1String.sendCommand(red)
	  	RGB1_RGB1C1String.sendCommand(green)
	  	RGB1_RGB1C1String.sendCommand(blue)

is not going to do anything like that, so you would need to restructure that part your rule to be more like the example you just gave.
You’d need to find out how to get integer values too. .intValue seems likely a useful method here.

rule "RGB"
  when
    Item RGB1_RGB1C1String received command
  then
               // rest of your code
	  	RGB1_RGB1C1String.sendCommand(red)

This rule triggers itself.