Rotary Dimmer

Everything about esp8266 and more.
Post Reply
Posts: 4
Joined: Sunday 18 March 2018 16:27
Target OS: -
Domoticz version:

Rotary Dimmer

Post by frankverd » Thursday 04 October 2018 16:42

Running Domotics 4.9700

Wemos D1 firmware v2.0.0-dev12 test image with rotary
with Rotary KY-040 encoder connected to D3 GPIO(0) and D2 GPIO(2)
Ip and at the device tab the rotary switch.


Hi All.
I like to controle my dimmer via a rotary switch KY-040 encoder.
Dimmer is working via the slider via a dummy dimmer via lua script.
So in short terms how can i controle a dimmer via a rotary encoder connected to a Wemos ESP8266.

Code: Select all

commandArray = {}
DomDevice = 'Dimmer-Veranda-110';
IP = '';
PIN = "16";
if devicechanged[DomDevice] then
   if(devicechanged[DomDevice]=='Off') then
     print ("OFF dimm = "..uservariables['dimm']);
     CalcValue = 800;
   else if(devicechanged[DomDevice]=='On') then
        DomValue = uservariables['dimm'];
        print ("ON dimm = "..uservariables['dimm']);
        CalcValue = DomValue;
         DomValue = otherdevices_svalues[DomDevice];
         CalcValue = 900-(DomValue*9);
         commandArray['Variable:dimm'] = tostring(CalcValue);
         print ("dimm Level = "..uservariables['dimm']);
   runcommand = "curl 'http://" .. IP .. "/control?cmd=PWM,"  ..PIN.. "," .. CalcValue .. "'";
print("PWM calculated value= "..CalcValue);
return commandArray
Can any one help how to proceed, i have tried this for weeks now with out any results.

I have seen some articles :
For home assist: ... mqtt/29448

Image of wemos esp8266 where you can see that the rotary encoder is counting up and down.
Schermafbeelding 2018-10-04 om 16.30.08.png
Schermafbeelding 2018-10-04 om 16.30.08.png (25.41 KiB) Viewed 252 times

User avatar
Posts: 938
Joined: Monday 27 June 2016 11:02
Target OS: Raspberry Pi
Domoticz version: latest
Location: Milano, Italy

Re: Rotary Dimmer

Post by emme » Thursday 04 October 2018 17:13

how MQTT is involved in this?

rotary can send via mqtt 2 different payload domoticz already formatted to be stored in a variable...
then you can have a dzVents script to handle the variable change:

let's suppose the dimmer publish on /domoticz/in a payload like:

Dim Up

Code: Select all

   "command": "setuservariable",
   "idx": 1,
   "value": "+"
Dim Down

Code: Select all

   "command": "setuservariable",
   "idx": 1,
   "value": "-"
on dzVents you could a script like:

Code: Select all

return {
	on = {
		variables = { 'myVarIDX1' },

	execute = function(dz, myVar)
	    local dimmer = dz.devices('myDimmerDevice')
	    local dimLevel = dimmer.level
	    local dimStep = 5       -- absolute value to step up or down the dimmer
        if myVar.value == "+" then 
            newDimLevel = dimLevel + dimstep 
            if newDimLevel > 100 then newDimLevel = 100 end 
        elseif myVar.value == "-" then 
            newDimLevel = dimLevel - dimstep 
            if newDimLevel < 0 then newDimLevel = 0 end 
        if newDimLevel == 0 then dimmer.switchOff().checkFirst() 
the script will be activated bu variable chage (it reflects the mqtt telegram)
if it gets a "+ " then it increate the dim value, in "-"... well... it does exactely what it is supposed to do

then I added a level check... assuming 0 and 100 are min and max values....
and more...
if you get 0... then switch off the dimmer device ;)

hope you enjoy it
The most dangerous phrase in any language is:
"We always done this way"

Posts: 4
Joined: Sunday 18 March 2018 16:27
Target OS: -
Domoticz version:

Re: Rotary Dimmer

Post by frankverd » Friday 05 October 2018 9:49

Hi Emme.
Thank you for responding for some more pieces of this puzzel :D
I appreciate your help !!!!!

As i understand is that the ESP8266 is send mqtt message from the rotary swithch to mqtt
But How do i get / see these update in variable "dimmer" i have created in Domoticz.

Dimmer-1 and Rotary are on the same ESP8266
GPIO POrts: Dimmer on GPI0 16 and the rotary on GPIO,0 and 2

Json output: dimer is changing when i turn the rotary.

Build 20000
Unit 11
Uptime 1431
Free RAM 19328
TaskName "dimmer"
dimmer 75

Rg Frank
Schermafbeelding 2018-10-05 om 09.37.15.png
Schermafbeelding 2018-10-05 om 09.37.15.png (29.55 KiB) Viewed 216 times
Schermafbeelding 2018-10-05 om 09.38.04.png
Schermafbeelding 2018-10-05 om 09.38.04.png (10.18 KiB) Viewed 216 times
Schermafbeelding 2018-10-05 om 09.38.22.png
Schermafbeelding 2018-10-05 om 09.38.22.png (56.53 KiB) Viewed 217 times

Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest