MQTT LED Dimmer

Hello, today I’m going to show you how I made MQTT dimmer which sends values to ESP-EASY PWM and that’s it. It’s pretty straight forward.

my .rules file


import java.lang.Math.*
import org.openhab.core.library.types.DecimalType

rule "LEDLIGHT_IN"
when
  Item MQTTLED received update
then
  var Number outNum = MQTTLED.state as DecimalType
  MQTTLEDS.sendCommand(outNum*10)
end


my .items file

Dimmer MQTTLED
Number MQTTLEDS {mqtt=">[broker:/led/PWM/5:command:*:default]"}

and my .sitemap file

Slider item=MQTTLED label="LED Brightness"

that’s all there is for it. Feel free to ask any questions about this code.

P.S I believe that there was something similar poster, but I couldn’t find it, so here it’s.

:slight_smile:

2 Likes

Hey, this is nice, but it’s much easier!

Instead of your rule you can activate JS Transform add-in. Then create the
JS-File “x100.js” in the transforms folder with content:

(function(i) {
    return i*100;
})(input)

You only need one item, note the JS transform in the end:

Dimmer TischLED_FadeTime {mqtt=">[macxsmqtt:wohnzimmer/tisch/fadetime/set:command:*:JS(x100.js)]"}

You can do this vice versa with incoming status (but don’t name the file “/100.js” :wink: )

Bye! Marco