Send Color value to MQTT [Solved]

I have a color picker in openhab and I want to convert the values of the Red Green and Blue to PWM values (0-1024). Once I have the color picker values in RGB I want to publish that number as a payload the topics: frontPorch/PWM/12, /frontPorch/PWM/13, and /frontPorch/PWM/15. 12,13 and 15 which are the red, green, and blue LEDs respectively.

As a proof of concept here is a switch which publishes an MQTT message to the right topic and makes the LED about half brightness. Switch RGBred {mqtt=">[mymosquitto:/frontPorch/PWM/15:command:*:500]"}

Here is the color picker I’m using:
.items:
Color themeLights “Theme Lights Color”

I believe I will have to make some rule to do what I want, but I can’t seem to extract the color picker values and place them in the correct part of the MQTT payload.

This rule obviously doesn’t work, but it serves as my notes from the research I conducted. I’m sure the answer is hidden somewhere within this code:

`var String message1="mosquitto_pub -t /frontPorch/PWM/15 -m ABC-123" 
import org.openhab.core.library.types.* 

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

rule "mqtt publish"
when
Item themeLights changed
then
 hsbValue = themeLights.state as HSBType  

	redValue   = hsbValue.red.intValue.toString
	greenValue = hsbValue.green.intValue.toString
	blueValue  = hsbValue.blue.intValue.toString
    
    
var String message1="mosquitto_pub -t /frontPorch/# -m 250" 
executeCommandLine(message1)

PublishItem.sendCommand("250")
    
end`

Would someone be able to guide me with an example to extract the color picker values and place them in the MQTT payload? Thanks.

Ok So I made a lot of progress. This only works once per session of openhab. Basically every time I make a change to the ui, items, or rules and save and a new ‘version’ of openhab opens it will work exactly once. I would love some feedback on why this is the case.

Here is my code
`import org.openhab.core.library.types.*
//val int redValue = 0
//val int greenValue = 0
//val int blueValue = 0
var String redMessage=“mosquitto_pub -t /frontPorch/PWM/15 -m” // contains the publish command to the specific pin on the ESP8266 (12e) Node MCU
var String greenMessage="mosquitto_pub -t /frontPorch/PWM/13 -m"
var String blueMessage=“mosquitto_pub -t /frontPorch/PWM/12 -m”
//val java.util.Random rand = new java.util.Random

//
var HSBType hsbValue

rule "Theme lights"
when
// when in doubt use all the types
Item themeLights changed or
Item themeLights received update or
Item themeLights received command

then

hsbValue = themeLights.state as HSBType // sets hsbValue to some kind of variable (int/float?) 
	
//send the red value
redMessage = redMessage + " " + (10*hsbValue.red.intValue).toString // grabs just the red value out of hsbValue and multiplies it time 10 so that it has a scale of 0-1000 for the PWM
executeCommandLine(redMessage) //send the red message with the PWM value

//send the green value
greenMessage = greenMessage + " " + (10*hsbValue.green.intValue).toString //same as in red
executeCommandLine(greenMessage) //same as in red

//send the blue value
blueMessage = blueMessage + " " + (10*hsbValue.blue.intValue).toString //same as in red
executeCommandLine(blueMessage) //same as in red

end // le fin

Instead of calling mosquitto_pub, why not use either openhab-action-mqtt (which lets you call publish(brokerName,topic,message) from your rule), or just create items like:

String frontPorchPWM12 { mqtt=">[mymosquitto:/frontPorch/PWM/12:state:*:default]" }
String frontPorchPWM13 { mqtt=">[mymosquitto:/frontPorch/PWM/13:state:*:default]" }
String frontPorchPWM15 { mqtt=">[mymosquitto:/frontPorch/PWM/15:state:*:default]" }

and your rules would just make calls like frontPorchPWM12.postUpdate(pwmString).

OMG! That works so much better! Thanks. I’m not sure why sendcommandline doesn’t work as well, but this is awesome. Thank you so much for the help!

hardware:
ESP 12e v1 (Node MCU)
Software running on the board:
ESP Easy: http://www.esp8266.nu/index.php/ESPEasy

Here is the code for anyone in the future struggling.

.items
String frontPorchPWM12 { mqtt=">[mymosquitto:/frontPorch/PWM/12:state:*:default]" } String frontPorchPWM13 { mqtt=">[mymosquitto:/frontPorch/PWM/13:state:*:default]" } String frontPorchPWM15 { mqtt=">[mymosquitto:/frontPorch/PWM/15:state:*:default]" }

.sitemap
Colorpicker item=themeLights

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

var String redpwmString
var String greenpwmString
var String bluepwmString
var HSBType hsbValue

rule "Theme lights"
when

	Item themeLights received update

then
	
	hsbValue = themeLights.state as HSBType // sets hsbValue to some kind of variable (int/float?) 
	
	redpwmString = (10*hsbValue.red.intValue).toString// grabs just the red value out of hsbValue and multiplies it time 10 so that it has a scale of 0-1000 for the PWM
	frontPorchPWM15.postUpdate(redpwmString)// posts the value above in the MQTT message
	
	greenpwmString = (10*hsbValue.green.intValue).toString//same as in red
	frontPorchPWM13.postUpdate(greenpwmString)
	
	bluepwmString = (10*hsbValue.blue.intValue).toString //same as in red
	frontPorchPWM12.postUpdate(bluepwmString)
2 Likes

Anyone did the Rules for WS2812?