NodeMCU MQTT LED Strip Controller Build & Config How-To Videos

If you use this code, the all white solid command is the following:

sendCommand(YOUR_LED_STRIP_ITEM, "{\"state\": \"ON\",\"color\":{\"r\":255,\"g\":255,\"b\":255},\"brightness\":255,\"effect\":\"solid\"}")

Or, with mqtt2.x you could use the previously mentioned mqtt action from the rule directly:

    val actions = getActions("mqtt","mqtt:broker:YOUR_BROKER_NAME")
   `actions.publishMQTT("YOUR_LED_SET_TOPIC", "{\"state\": \"ON\",\"color\":{\"r\":255,\"g\":255,\"b\":255},\"brightness\":255,\"effect\":\"solid\"}")`
1 Like

Hi. This is e very nice project.

I used the code from the OP and it works for my first strip.

But one problem: the brightness for each color only set to 100.

Is there a chance tu set it to the full range of 255 to each color?

@Daniel3 - are you sure you used the latest version from the Github and the rules I posted in post #1? I have updated these a few times since the original post, including some fixes to the rules, to use Sliders instead of Setpoints for the Speed/Intensity items (but there may have been some changes to the Color items too over time).

Yes. It´s the code from today.

Can you post what the rule is actually sending to the Logs? It’s strange that I don’t have the same problem with my LED strips…

The value of HSBType.red (also green and blue) contain the percentage of the color. If you want the value you have to multiply it by 2.55. So you have to change the rule to:

var ColorHSB = LEDStrip_Test_Color.state as HSBType
val red = (ColorHSB.red * 2.55).intValue
val green = (ColorHSB.green * 2.55).intValue
val blue = (ColorHSB.blue * 2.55).intValue
LEDStrip_Test.sendCommand("{\"color\": {" + 
	"\"r\": " + red + "," +
	"\"g\": " + green + "," +
	"\"b\": " + blue + "}}")

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

can anyone implement the ” * 255 ” in the bk hobby rule please

Just copy the code from my example and replace it in the rule. This is what you get:

rule "LED Strip Group 1 (Kitchen) JSON Color Single Strip"
when Member of gLEDStrip_Kitchen_Color received update
then 
 	val source = triggeringItem.name.split("_Color").get(0)
	val item = gLEDStrip_Kitchen.members.findFirst[ l | l.name == source ] as StringItem
	var ColorHSB = triggeringItem.state as HSBType
	val red = (ColorHSB.red * 2.55).intValue
	val green = (ColorHSB.green * 2.55).intValue
	val blue = (ColorHSB.blue * 2.55).intValue
	item.sendCommand("{\"color\": {" + 
		"\"r\": " + red + "," +
		"\"g\": " + green + "," +
		"\"b\": " + blue + "}}")
end
2 Likes

I guess I never noticed how unsaturated the solid colors were :smiley: (I do mostly use these strips for effects, though).

Thanks @Daniel3 for finding the bug, and @Dave_K for fixing it! :+1:

Hello

Is there a way i can change the start up brightness to 100% white ,becurse
im building my kitchen light to led and i wood like it just to turn on when it is powered on so it the mqtt is not working i will get som light ? or will the D1 mini not start up if is not connecet to mqtt ?

Simon

Hi,

You could use a simple switch on your D1, but in this case you have to modify your code, when you turn it on, send full white values into the main loop. IMO it’s a hard way.
Much easier, if you use and trust in MQTT itself, and use a smart button for OH. I use Xiaomi zigbee smart switch taped to the kitchen furniture, when I push, a rule triggers, and OH sends a full white ON command to the ledstrip. It’s simple and works well. This button knows the simple and double click action, so if you want to turn off the ledstrip, you can use the double click action for that. More funny feature, if you use Xiaomi’s magic cube for this :slight_smile:

1 Like

First, thanks BK for all of your videos and tutorials, I think if it weren’t for that I would have abandoned openhab.

Down to business, I copy pasted the postings and loaded an arduino with code. It connects to wifi and MQTT.

However, openhab, when I press the buttons in the UI put a message in the log

2019-05-07 15:58:07.986 [vent.ItemStateChangedEvent] - LEDStrip_FF_Kitchen_Cabinet_1_Speed changed from 25 to 30

2019-05-07 15:58:08.008 [ome.event.ItemCommandEvent] - Item ‘LEDStrip_FF_Kitchen_Cabinet_1’ received command {“transition”: “30”}

2019-05-07 15:58:08.016 [vent.ItemStateChangedEvent] - LEDStrip_FF_Kitchen_Cabinet_1 changed from {“transition”: “25”} to {“transition”: “30”}

2019-05-07 15:58:09.355 [ome.event.ItemCommandEvent] - Item ‘LEDStrip_FF_Kitchen_Cabinet_1_FX’ received command 5

But no mqtt message are actually sent.

I know MQTT is working because in the paper ui i setup broker and created a thing, with topic matching the set command of the arduino and Output set to {“state”:"%s"}
When a switch is linked to that item it works (turning on/off)

But I would prefer to use .items file as I don’t mind doing for one item via ui, but there are quite a few.

1 Like

I solved the issue. Just thought I would post in case this happens to other…Was using MQTT Binding for 2.x Installed 1.x binding in addition, set the mqtt.cfg file and it works :slight_smile:

@DarthMops - just saw your original post, but I see you already solved your own problem, excellent! :slight_smile:

@bartus - Thanks as always for your help!

just as a follow up, for me the following patterns don’t work.
26=USA, 21=Christmas,22=St Patty,23=Valentine,24=Turkey, 28=Halloween, 29=Go Lions

I have tested almost all. Just curious if it is just me or if these work for other people?

@DarthMops - as those are the “newer” effects (as of late 2018), I’d just make sure that you’re using the latest .ino file from my repository, and the latest MAP transformation file (copy mine).

hi, does this work on the latest versions of opennhab and MQTT binding? ive been trying for a few days and cant seem to get my MQTT broker to talk to my NodeMCU. nothing shows in the serial minitor after it says its connected to MQTT

edit, so i got this to work by downgrading the MQTT to the legacy version and playing around with the mqtt.cfg file. however im using a neopixel strip with chipset SK6812RGBW and the colours arnt solid when using the colour picker each led is sperate colour as per photo

@byronendleton - This should work with the MQTT v2 binding in openHAB as well - just need to define the MQTT things/channels (I haven’t done so myself yet, still using MQTT v1 for these).

As for your LED strips, the fact you’re using RGBW is the problem - FastLED (the library that supports all the effects within the program) doesn’t support RGBW, only RGB LED strips. That’s why you’re seeing the weird color issues. My goto LED strips have been WS2812 RGB strips, in case you want to try those.

im only new to this and tried with v2 for a long time and couldnt work it out. seems to be working ok with v1 till someone has a proven method it works.

hmm thought that might be the case. is there a work around for it that you know of? ive already for the RGBW strips installed

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)

1 Like