Hi guys,
here is my implementation with MQTT v2 binding. This is the 40 animations included version, unfortunately I’ve forgot where I found the code itself. If you use an another version of the code, don’t forget replace the mappings on the sitemap and the transformation map file’s contents. As an extra, I’ve configured direct switches for the four base colors (R,G,B,W), and I’ve made a basic logic for the reatining the previous settings if you choose a solid one. I switch on a solid color red light with an astro binding based rule in the night, and turn it off when the sun rises. In this case, the ledstrip switching back to the previous states via the “prev_” prefixed dummy items. Since the OH2.5 snapshot fixed the early MQTT based issues, it works fine without any issue. I’m a rookie yet, so I’m sure you can simplify my rules, but it do what I’ve planned. If somebody wants migrate this project to MQTT v2, it’s an opportunity.
broker.things:
Bridge mqtt:broker:sentinel "MQTT Broker: Mosquitto server" [
host="localhost",
port=1883,
clientid="sentinel_openhab",
secure="false",
username="openhabian",
password="mqtt"
]
mqtt.things:
Thing mqtt:topic:sentinel:t-hLED "Hifi LED" (mqtt:broker:sentinel) @ "MQTT2" {
Channels:
Type string : hLED "HiFi LED" [ stateTopic="hifi_led", commandTopic="hifi_led/set" ]
Type switch : hLEDPower "HiFi LED Power" [ stateTopic="hifi_led", commandTopic="hifi_led/set", transformationPattern="JSONPATH:$.state"]
Type string : hLEDFX "HiFi LED FX" [ stateTopic="hifi_led", commandTopic="hifi_led/set", transformationPattern="JSONPATH:$.effect"]
Type string : hLEDIntensity "HiFi LED Intensity" [ stateTopic="hifi_led", commandTopic="hifi_led/set", transformationPattern="JSONPATH:$.brightness"]
Type string : hLEDColorred "HiFi LED color r" [ stateTopic="hifi_led", commandTopic="hifi_led/set", transformationPattern="JSONPATH:$.color.r"]
Type string : hLEDColorgreen "HiFi LED color g" [ stateTopic="hifi_led", commandTopic="hifi_led/set", transformationPattern="JSONPATH:$.color.g"]
Type string : hLEDColorblue "HiFi LED color b" [ stateTopic="hifi_led", commandTopic="hifi_led/set", transformationPattern="JSONPATH:$.color.b"]
}
led items:
Group:Switch HifiColors "Solid color buttons"
Switch HifiRed "Solid Red" (HifiColors) [ "Switchable" ]
Switch HifiBlue "Solid Blue" (HifiColors) [ "Switchable" ]
Switch HifiWhite "Solid White" (HifiColors) [ "Switchable" ]
Switch HifiGreen "Solid Green" (HifiColors) [ "Switchable" ]
Switch hLED_Power "Ledstrip Power [%s]" [ "Switchable" ] {channel="mqtt:topic:sentinel:t-hLED:hLEDPower"}
String hLED_FX "Ledstrip FX [MAP(LEDStripEffectJSON_2.map)%s]"
String hLED "Ledstrip overall state [%s]" {channel="mqtt:topic:sentinel:t-hLED:hLED"}
Dimmer hLED_Speed "Ledstrip Speed"
Dimmer hLED_Intensity "Ledstrip Intensity"
Color hLED_Color "Ledstrip Color" <rgb> [ "Switchable" ]
String hLED_Color_red "Ledstrip red color [%s]" {channel="mqtt:topic:sentinel:t-hLED:hLEDColorred"}
String hLED_Color_green "Ledstrip green color [%s]" {channel="mqtt:topic:sentinel:t-hLED:hLEDColorgreen"}
String hLED_Color_blue "Ledstrip blue color [%s]" {channel="mqtt:topic:sentinel:t-hLED:hLEDColorblue"}
Switch prev_hLED_Power
String prev_hLED_FX
Dimmer prev_hLED_Speed
Dimmer prev_hLED_Intensity
Color prev_hLED_Color
led sitemap:
sitemap hifi_led label="Ledstrip Control" icon="colorpicker" {
Frame label="Solid color direct buttons" icon="lightbulb" {
Switch item=HifiWhite
mappings=[OFF="Turn off"]
labelcolor=[OFF="red",NULL="red",ON="blue"]
valuecolor=[OFF="red",NULL="red",ON="blue"]
visibility=[HifiWhite==ON]
icon="light"
Switch item=HifiWhite
mappings=[ON="Turn on"]
labelcolor=[OFF="red",NULL="red",ON="blue"]
valuecolor=[OFF="red",NULL="red",ON="blue"]
visibility=[HifiWhite==OFF,HifiWhite=="NULL"]
icon="light"
Switch item=HifiRed
mappings=[OFF="Turn off"]
labelcolor=[OFF="red",NULL="red",ON="blue"]
valuecolor=[OFF="red",NULL="red",ON="blue"]
visibility=[HifiRed==ON]
icon="light"
Switch item=HifiRed
mappings=[ON="Turn on"]
labelcolor=[OFF="red",NULL="red",ON="blue"]
valuecolor=[OFF="red",NULL="red",ON="blue"]
visibility=[HifiRed==OFF,HifiRed=="NULL"]
icon="light"
Switch item=HifiBlue
mappings=[OFF="Turn off"]
labelcolor=[OFF="red",NULL="red",ON="blue"]
valuecolor=[OFF="red",NULL="red",ON="blue"]
visibility=[HifiBlue==ON]
icon="light"
Switch item=HifiBlue
mappings=[ON="Turn on"]
labelcolor=[OFF="red",NULL="red",ON="blue"]
valuecolor=[OFF="red",NULL="red",ON="blue"]
visibility=[HifiBlue==OFF,HifiBlue=="NULL"]
icon="light"
Switch item=HifiGreen
mappings=[OFF="Turn off"]
labelcolor=[OFF="red",NULL="red",ON="blue"]
valuecolor=[OFF="red",NULL="red",ON="blue"]
visibility=[HifiGreen==ON]
icon="light"
Switch item=HifiGreen
mappings=[ON="Turn on"]
labelcolor=[OFF="red",NULL="red",ON="blue"]
valuecolor=[OFF="red",NULL="red",ON="blue"]
visibility=[HifiGreen==OFF,HifiGreen=="NULL"]
icon="light"
}
Frame label="Animation, color, speed, intensity" icon="smiley" {
Slider item=hLED_Intensity label="Ledstrip intensity [%d %%]" icon="chart"
Switch item=hLED_Power label="Ledstrip Power [%s]"
Selection item=hLED_FX mappings=[1="BPM",2="candy cane",3="animations",4="template",5="blendwave",6="beatwave",7="blur",8="confetti",9="confetti 2",10="confetti pal",11="dotbeat",12="easing",13="every n example",14="fill grad",15="inoise8 mover",16="inoise8 pal",17="noise 16 1",18="noise 16 2",19="noise 16 3",20="one sine pal",21="palette cross fade",22="rainbow march",23="rainbow beat",24="ripple pal",25="sinelon",26="cyclon rainbow",27="dots",28="fire",29="glitter",30="juggle",31="lightning",32="noise",33="police all",34="police one",35="rainbow",36="rainbow with glitter",37="ripple",38="sinelon",39="solid",40="twinkle"] icon="smiley"
Slider item=hLED_Speed label="Ledstrip speed [%d %%]" icon="motion"
Colorpicker item=hLED_Color icon="colorwheel"
Text item=hLED_Color_red icon="colorpicker"
Text item=hLED_Color_green icon="colorpicker"
Text item=hLED_Color_blue icon="colorpicker"
//Text item=hLED label="HLED"
}
}
led rules:
rule "Hifi color switch"
when Member of HifiColors received command
then
if(receivedCommand == ON){
prev_hLED_Intensity.postUpdate(hLED_Intensity.state)
prev_hLED_Speed.postUpdate(hLED_Speed.state)
prev_hLED_Color.postUpdate(hLED_Color.state)
prev_hLED_Power.postUpdate(hLED_Power.state)
prev_hLED_FX.postUpdate(hLED_FX.state)
if (triggeringItem == HifiWhite) {
hLED_FX.sendCommand(39)
hLED_Speed.sendCommand(60)
hLED_Intensity.sendCommand(60)
val actions = getActions("mqtt", "mqtt:broker:sentinel")
actions.publishMQTT("hifi_led/set", "{\"state\":\"ON\",\"transition\":170,\"color\":{\"r\":255,\"g\":255,\"b\":255}}")
hLED_Power.postUpdate(ON)
HifiRed.postUpdate(OFF)
HifiGreen.postUpdate(OFF)
HifiBlue.postUpdate(OFF)
logInfo("Test", "Ledstrip White command executed")
}
else if(triggeringItem == HifiRed) {
hLED_FX.sendCommand(39)
hLED_Speed.sendCommand(60)
hLED_Intensity.sendCommand(60)
val actions = getActions("mqtt", "mqtt:broker:sentinel")
actions.publishMQTT("hifi_led/set", "{\"state\":\"ON\",\"transition\":170,\"color\":{\"r\":255,\"g\":0,\"b\":0}}")
hLED_Power.postUpdate(ON)
HifiWhite.postUpdate(OFF)
HifiGreen.postUpdate(OFF)
HifiBlue.postUpdate(OFF)
logInfo("Test", "Ledstrip Red command executed")
}
else if (triggeringItem == HifiBlue) {
hLED_FX.sendCommand(39)
hLED_Speed.sendCommand(60)
hLED_Intensity.sendCommand(60)
val actions = getActions("mqtt", "mqtt:broker:sentinel")
actions.publishMQTT("hifi_led/set", "{\"state\":\"ON\",\"transition\":170,\"color\":{\"r\":0,\"g\":0,\"b\":255}}")
hLED_Power.postUpdate(ON)
HifiRed.postUpdate(OFF)
HifiGreen.postUpdate(OFF)
HifiWhite.postUpdate(OFF)
logInfo("Test", "Ledstrip Blue command executed")
}
else if (triggeringItem == HifiGreen) {
hLED_FX.sendCommand(39)
hLED_Speed.sendCommand(60)
hLED_Intensity.sendCommand(60)
val actions = getActions("mqtt", "mqtt:broker:sentinel")
actions.publishMQTT("hifi_led/set", "{\"state\":\"ON\",\"transition\":170,\"color\":{\"r\":0,\"g\":255,\"b\":0}}")
hLED_Power.postUpdate(ON)
HifiRed.postUpdate(OFF)
HifiWhite.postUpdate(OFF)
HifiBlue.postUpdate(OFF)
logInfo("Test", "Ledstrip Green command executed")
}
}
else {
val actions = getActions("mqtt", "mqtt:broker:sentinel")
actions.publishMQTT("hifi_led/set", '{"state" : "OFF"}')
postUpdate(HifiRed, OFF)
postUpdate(HifiWhite, OFF)
postUpdate(HifiBlue, OFF)
postUpdate(HifiGreen, OFF)
sendCommand(hLED_Power, prev_hLED_Power.state.toString)
Thread::sleep(100)
hLED_Color.sendCommand(prev_hLED_Color.state.toString)
Thread::sleep(100)
hLED_FX.sendCommand(prev_hLED_FX.state.toString)
Thread::sleep(100)
hLED_Intensity.sendCommand(prev_hLED_Intensity.state as Number)
Thread::sleep(100)
hLED_Speed.sendCommand(prev_hLED_Speed.state as Number)
}
end
rule "LED Strip Hifi JSON Intensity"
when Item hLED_Intensity received command
then
var intensity = Math::round(((receivedCommand as Number) * 255 / 100.0).intValue)
val actions = getActions("mqtt", "mqtt:broker:sentinel")
actions.publishMQTT("hifi_led/set", "{\"brightness\":" + intensity + "}")
logInfo("Test", "Ledstrip intensity command executed, new value : " + hLED_Intensity.state + "%")
end
rule "hLED Strip JSON Speed"
when Item hLED_Speed received command
then
var speed = Math::round(((receivedCommand as Number) * 255 / 100.0).intValue)
val actions = getActions("mqtt", "mqtt:broker:sentinel")
actions.publishMQTT("hifi_led/set", "{\"transition\":" + speed + "}")
logInfo("Test", "Ledstrip Speed command executed, new value : " + hLED_Speed.state + "%")
end
rule "hLED MQTT JSON effect rule"
when Item hLED_FX received command
then
val hledFX=transform("MAP","LEDStripEffectJSON_2.map", receivedCommand.toString)
val actions = getActions("mqtt", "mqtt:broker:sentinel")
actions.publishMQTT("hifi_led/set", hledFX)
logInfo("Test", "Led FX command executed: " + hledFX)
end
rule "hLED MQTT JSON power on off"
when Item hLED_Power received command
then
if(receivedCommand == ON){
val actions = getActions("mqtt", "mqtt:broker:sentinel")
actions.publishMQTT("hifi_led/set", '{"state" : "ON"}')
}
else {
val actions = getActions("mqtt", "mqtt:broker:sentinel")
actions.publishMQTT("hifi_led/set", '{"state" : "OFF"}')
}
end
rule "hLED MQTT JSON Strip Color"
when
Item
hLED_Color received command
then
val actions = getActions("mqtt", "mqtt:broker:sentinel")
actions.publishMQTT("hifi_led/set", "{\"color\": {" +
"\"r\": " + ((hLED_Color.state as HSBType).red * 2.55).intValue + "," +
"\"g\": " + ((hLED_Color.state as HSBType).green * 2.55).intValue + "," +
"\"b\": " + ((hLED_Color.state as HSBType).blue * 2.55).intValue + "}}")
Thread::sleep(100)
logInfo("Test", "Ledstrip color command executed: Red :" + ((hLED_Color.state as HSBType).red * 2.55).intValue +
", Green: " + ((hLED_Color.state as HSBType).green * 2.55).intValue +
", Blue: " + ((hLED_Color.state as HSBType).blue * 2.55).intValue )
end
LEDStripEffectJSON_2.map:
1={"effect":"bpm"}
2={"effect":"candy cane"}
3={"effect":"animations"}
4={"effect":"template"}
5={"effect":"blendwave"}
6={"effect":"beatwave"}
7={"effect":"blur"}
8={"effect":"confetti"}
9={"effect":"confetti 2"}
10={"effect":"confetti pal"}
11={"effect":"dotbeat"}
12={"effect":"easing"}
13={"effect":"every n example"}
14={"effect":"fill grad"}
15={"effect":"inoise8 mover"}
16={"effect":"inoise8 pal"}
17={"effect":"noise 16 1"}
18={"effect":"noise 16 2"}
19={"effect":"noise 16 3"}
20={"effect":"one sine pal"}
21={"effect":"palette cross fade"}
22={"effect":"rainbow march"}
23={"effect":"rainbow beat"}
24={"effect":"ripple pal"}
25={"effect":"sinelon"}
26={"effect":"cyclon rainbow"}
27={"effect":"dots"}
28={"effect":"fire"}
29={"effect":"glitter"}
30={"effect":"juggle"}
31={"effect":"lightning"}
32={"effect":"noise"}
33={"effect":"police all"}
34={"effect":"police one"}
35={"effect":"rainbow"}
36={"effect":"rainbow with glitter"}
37={"effect":"ripple"}
38={"effect":"sinelon"}
39={"effect":"solid"}
40={"effect":"twinkle"}
ON={"state":"ON"}
OFF={"state":"OFF"}
hifi_led.ino.txt (59.2 KB)