Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save maxela/64af0e0471aa59c1550b05d8c0d79802 to your computer and use it in GitHub Desktop.
Save maxela/64af0e0471aa59c1550b05d8c0d79802 to your computer and use it in GitHub Desktop.
Tasmota Script for WiFi-2Channel-Dimmer QS-WiFi-D02-2C or MS-105B-220
;WiFi-2CH-Dimmer v1.10
;QS-WiFi-D02-2C
;MS-105B
>D
b1=0
b2=0
n1=0
n2=0
t1=0
t2=0
h1=0
h2=0
pl=3
pu=10
dd1=0
dd2=0
mp=2.2
sp=8
sm=4
ll=8
ul=100
dv1=0
dv2=0
d1=70
d2=70
f1=0
f2=0
p1=0
p2=0
s1=0
s2=0
di=""
c=0
tv=0
i=0
dt=0
>B
=>Counter1 0
=>Counter2 0
=>Baudrate 9600
=#sd1
=#sd2
>F
n1=pc[1]
n2=pc[2]
b1=0
if chg[n1]>0 {
b1=1
}
b2=0
if chg[n2]>0 {
b2=1
}
t1+=1
t2+=1
if b1==0
and t1>pl
and t1<pu {
f1^=1
dd1=0
}
if b2==0
and t2>pl
and t2<pu {
f2^=1
dd2=0
}
if b1>0 {
if t1>pu
and h1==0 {
h1=1
dd1^=1
}
if h1>0 {
if dd1>0 {
d1+=sm*f1
if d1>ul {
d1=ul
}
} else {
d1-=sm*f1
if d1<ll {
d1=ll
}
}
}
} else {
t1=0
h1=0
}
if b2>0 {
if t2>pu
and h2==0 {
h2=1
dd2^=1
}
if h2>0 {
if dd2>0 {
d2+=sm*f2
if d2>ul {
d2=ul
}
} else {
d2-=sm*f2
if d2<ll {
d2=ll
}
}
}
} else {
t2=0
h2=0
}
if f1>0 {
if dv1!=d1 {
=#ud1(d1)
}
} else {
if dv1>0 {
=#ud1(0)
}
}
if f2>0 {
if dv2!=d2 {
=#ud2(d2)
}
} else {
if dv2>0 {
=#ud2(0)
}
}
>E
f1=pwr[1]
f2=pwr[2]
s1=Channel1
if f1>0
and s1>0 {
if s1>=ll {
if s1>ul {
s1=ul
}
d1=s1
} else {
f1=0
s1=0
}
}
s2=Channel2
if f2>0
and s2>0 {
if s2>=ll {
if s2>ul {
s2=ul
}
d2=s2
} else {
f2=0
s2=0
}
}
#sd1
di="FF5501"+hn(dv1*mp)+"0000000A"
=>SerialSend5 %di%
#ch1
=>Channel1 %dv1%
#sd2
di="FF550200"+hn(dv2*mp)+"00000A"
=>SerialSend5 %di%
#ch2
=>Channel2 %dv2%
#ud1(tv)
if dv1<tv {
dt=tv-dv1
i=sp
} else {
dt=dv1-tv
i=-sp
}
if dt>sp {
for c sp dt sp
dv1+=i
=#sd1
next
}
dv1=tv
=#sd1
=#ch1
#ud2(tv)
if dv2<tv {
dt=tv-dv2
i=sp
} else {
dt=dv2-tv
i=-sp
}
if dt>sp {
for c sp dt sp
dv2+=i
=#sd2
next
}
dv2=tv
=#sd2
=#ch2
#
@maxela
Copy link
Author

maxela commented Mar 1, 2020

v1.8: optimized channel control via konsole/mqtt

@maxela
Copy link
Author

maxela commented Mar 1, 2020

v1.9: optimized channel result messages

@maxela
Copy link
Author

maxela commented Mar 7, 2020

v1.10: Fixed bug, which allowed to change the dimming value by holding the buttons while lights were off. This invisibly changed the dimm value for the next powering on.

@aldii-hassio
Copy link

aldii-hassio commented Mar 23, 2020

v1.10: Naprawiono błąd, który umożliwiał zmianę wartości ściemniania poprzez przytrzymanie przycisków, gdy światła były wyłączone. Niewidocznie zmieniło to wartość przyciemnienia przy następnym włączeniu.

Switch 1 does not work. Is there a working script?

@maxela
Copy link
Author

maxela commented May 10, 2020

@aldii-hassio

Switch 1 does not work. Is there a working script?

You first have to set the right template and set SetOption68 1.
See parent gist by thxthx0 for details.

@yuraivan
Copy link

yuraivan commented Jul 7, 2020

Hello everyone!!!! I did everything according instruction.I have entered the script, the template and activated "Setoption68 1". In web interface I see all correct. Toggle 1 and 2 are working but my lamps not light(((((( What is the problem???

@beolink
Copy link

beolink commented Dec 11, 2020

Hi,
Thanks for all your work.
Is there an update on this?
And @yuraivan: Did you manage to get your dimmer to work?
I´m having the same issue with my d02 dimmers.
Thanks in advance!

@maxela
Copy link
Author

maxela commented Dec 11, 2020

@beolink which version of Tasmota are you using? I developed the script on 8.1.0.5 and it is still running perfectly on that version.

@beolink
Copy link

beolink commented Dec 13, 2020

@maxela, I tried the 8.5 release and also the latest 9.1 releases. Couldn´t find any other compiled scripting versions.
Would you happen to have a link to the 8.1.0.5 version you were using?

Correction!!!:
I now tried your latest script "1.10" (I tried an older version by misstake before). The 1.10 version of the script works flawlessly with Tasmota 9.1. (ofcourse I used the setoption and template as well in conjunction with the script).
Thanks for providing a great solution!

@maxela
Copy link
Author

maxela commented Dec 15, 2020

@beolink great! I’m glad to read that :)

@maxela
Copy link
Author

maxela commented Dec 15, 2020

@beolink btw in case you are using physical wall push buttons to switch on/off the lights, you can make them more or less sensitive by using higher or lower values for pl. In version 1.10 in line 14 pl is set to 3:
pl=3
In my opinion this leads to a sightly too long push duration that is required to toggle the lights. Actually I had to use this value because sometimes both lights switched on/off when I wanted to switch on/off only one of them. Maybe a capacitor would also help to prevent the signal to affect both lines. But I chose to solve it this way.
If you have the same feeling when pushing the push buttons you can try with
pl=2
If then both lights toggle, go back to 3.

@beolink
Copy link

beolink commented Dec 26, 2020

@maxela Thanks! Everything worked perfect until I added the second channel. Now both channels seem to be unstable. The same happens wether I use physical buttons or the sliders in the tasmota web gui. Would it be possible to get a copy of your 8.x scripting binary so I can test with that?
Thanks in advance!

@maxela
Copy link
Author

maxela commented Dec 27, 2020

@beolink I will check but probably I had compiled it by myself.
What means „unstable“? With physical switches it could be happening that the signal affects both channels. But that can not happen when you switch by using mqtt commands or the Tasmota interface.
Which device are you using?

@beolink
Copy link

beolink commented Dec 27, 2020

@maxela Unstable: When I move one slider it immediately affects the correct channel but within a second or two the other channel lights up and starts dimming up until it reaches maximum. The slider I´m referring to is either in the tasmota webinterface or in the HomeAssistant. Same effect. The script I´m using is 1.10 and Tasmota 9.1

@beolink
Copy link

beolink commented Dec 27, 2020

Device I´m using: MS-105B

@maxela
Copy link
Author

maxela commented Dec 27, 2020

@beolink Okay I'm using the same device.
I usually don't use sliders. But I just tested them from the Tasmota web ui and they work fine. One important detail in this context is how often commands are actually sent to the device while sliding. When I use the Tasmota web ui sliders the lights start to act when I release the slider because there is only one command sent when I release it. When I use the sliders of my home automation ui (openHAB in my case) this is a bit different. But anyway it still works quite good. I don't experience the effects you describe.
I put my 8.1.0.5 version with scripting activated on my Google drive. This is probably the firmware I use on my MS-105B. You can try if things work better with this firmware. However there's no guarantee. Maybe the reason is something else.
tasmota-DE-8.1-scripting.bin

@silverStSt
Copy link

Hi. First of all, thank you very much for your work, the script works very well and is more stable than previous versions. I have tried it with version 8.5.1 and it becomes unstable, but with the firmware version you shared it works quite well. I just have a problem with the physical switch. It changes the intensity well and turns off the light, but I can't turn it on from the physical switch. I have tried to analyze the script but the truth is that I am new and I get lost, what could I try? Thanks a lot!

@silverStSt
Copy link

Hello again, I have continued doing tests and I see that the light bulb is turned on with the switch but the power status of the device does not change, so I do not know its status. to turn off the bulb if it changes the state from power to off

@silverStSt
Copy link

sorry for all the messages but it is already solved, following another tutorial I had erroneously modified an option. It works great. thanks.

@maxela
Copy link
Author

maxela commented Jan 23, 2021

@silverStSt great to read that you were able to solve the issue 😊

@JDyszczynski
Copy link

JDyszczynski commented Dec 15, 2021

Hello
Fixed BUGS:

  • Buttons Toggle1 and Toggle2 didn't worked (added two variables of last pwr[1] and pwr[2] state and compare them to current values in function ">P"
  • Max hex value for brightnes never been after calulating as a maximum 255dec = Hex FF (parameter mp changed to 2.55)
  • When current channel will be change to ON, script automaticaly will return back last state of brightnes for this channel.

Corrected Script:

>D
b1=0
b2=0
n1=0
n2=0
t1=0
t2=0
h1=0
h2=0
pl=3
pu=10
dd1=0
dd2=0
mp=2.55
sp=8
sm=4
ll=8
ul=100
dv1=0
dv2=0
d1=70
d2=70
f1=0
f2=0
p1=0
p2=0
s1=0
s2=0
di=""
c=0
tv=0
i=0
dt=0
f1l=0
f2l=0
>B
=>Counter1 0
=>Counter2 0
=>Baudrate 9600
=#sd1
=#sd2

>F
n1=pc[1]
n2=pc[2]
b1=0
if chg[n1]>0 {
b1=1
}
b2=0
if chg[n2]>0 {
b2=1
}
t1+=1
t2+=1
if b1==0
and t1>pl
and t1<pu {
f1^=1
dd1=0
}
if b2==0
and t2>pl
and t2<pu {
f2^=1
dd2=0
}
if b1>0 {
if t1>pu
and h1==0 {
h1=1
dd1^=1
}
if h1>0 {
if dd1>0 {
d1+=sm*f1
if d1>ul {
d1=ul
}
} else {
d1-=sm*f1
if d1<ll {
d1=ll
}
}
}
} else {
t1=0
h1=0
}
if b2>0 {
if t2>pu 
and h2==0 {
h2=1
dd2^=1
}
if h2>0 {
if dd2>0 {
d2+=sm*f2
if d2>ul {
d2=ul
}
} else {
d2-=sm*f2
if d2<ll {
d2=ll
}
}
}
} else {
t2=0
h2=0
}
if f1>0 {
if dv1!=d1 {
=#ud1(d1)
}
} else {
if dv1>0 {
=#ud1(0)
}
}
if f2>0 {
if dv2!=d2 {
=#ud2(d2)
}
} else {
if dv2>0 {
=#ud2(0)
}
}

>E
f1=pwr[1]
f2=pwr[2]
s1=Channel1
if f1>0
and s1>0 {
if s1>=ll {
if s1>ul {
s1=ul
}
d1=s1
} else {
f1=0
s1=0
}
}
s2=Channel2
if f2>0
and s2>0 {
if s2>=ll {
if s2>ul {
s2=ul
}
d2=s2
} else {
f2=0
s2=0
}
}

#sd1
di="FF5501"+hn(dv1*mp)+"0000000A"
=>SerialSend5 %di%
#ch1
=>Channel1 %dv1%
#sd2
di="FF550200"+hn(dv2*mp)+"00000A"
=>SerialSend5 %di%
#ch2
=>Channel2 %dv2%
#ud1(tv)
if dv1<tv {
dt=tv-dv1
i=sp
} else {
dt=dv1-tv
i=-sp
}
if dt>sp {
for c sp dt sp
dv1+=i
=#sd1
next
}
dv1=tv
=#sd1
=#ch1
#ud2(tv)
if dv2<tv {
dt=tv-dv2
i=sp
} else {
dt=dv2-tv
i=-sp
}
if dt>sp {
for c sp dt sp
dv2+=i
=#sd2
next
}
dv2=tv
=#sd2
=#ch2
>P
f1=pwr[1]
f2=pwr[2]
if upd(f1)>0
then
=>channel1 on
else
if f1<>f1l
then
=>channel1 off
endif
endif
if upd(f2)>0
then
=>channel2 on
else
if f2<>f2l
then
=>channel2 off
endif
endif
#

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment