Skip to content

Instantly share code, notes, and snippets.

@thxthx0
Last active November 18, 2022 12:24
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save thxthx0/c22555f6ec25c3367264729a0ff74dce to your computer and use it in GitHub Desktop.
Save thxthx0/c22555f6ec25c3367264729a0ff74dce to your computer and use it in GitHub Desktop.
Tasmota Script Ver. a for QS-WiFi-D01-TRIAC 150W 1CH-Dimmer
>D
sw=0
tmp=0
cnt=0
tmr=0
hold=0
pwrt=0
slid=0
dim=""
doff="FF550005DC0A"
splo=2 ;short press lo limit
spup=10 ;short press up limit
ddir=0 ;dim dir 0/1
dmul=2.2 ;dim multipl 1..2.55
dstp=2 ;dim step/speed 1..5
dlo=15 ;dim lolimit min.0
dup=95 ;dim uplimit max.100
dval=50 ;start dim level 0..100
>B
print > Wifi-1CH-Dimmer-Script v2.0a <
print >> QS-WiFi-D01 150W Dimmer <<
;hide cnt webui
->WebSensor1 0
;reset cnt@teleperiod
->SetOption79 1
->Baudrate 9600
;boot sequ
=#snddim(dval)
delay(1000)
->SerialSend5 %doff%
=>Power1 0
>F
cnt=pc[1]
if chg[cnt]>0
;sw press
then sw=1
else sw=0
;sw not pressed
endif
; 100ms timer
tmr+=1
; short press
if sw==0
and tmr>splo
and tmr<spup
then
pwrt^=1
;change on/off
if pwrt>0
then
=#snddim(dval)
=>Power1 1
else
->SerialSend5 %doff%
=>Power1 0
endif
endif
; long press
if sw>0
then
if tmr>spup
then
hold=1
;off then on
if pwrt==0
then
pwrt=1
endif
;dim if on&hold
if pwrt>0
then
if ddir>0
then
;incr dimlevel
dval+=dstp
if dval>dup
then
;upper limit
dval=dup
endif
else
;decr dimlevel
dval-=dstp
if dval<dlo
then
;lower limit
dval=dlo
endif
endif
endif
endif
else
tmr=0
if hold>0
then
hold=0
;change dim dir
ddir^=1
endif
endif
if chg[dval]>0
then
=#snddim(dval)
=>Dimmer %dval%
endif
>E
; slider
slid=Dimmer
if chg[slid]>0
then
; dim per slider
if slid>0
then
dval=slid
else
pwrt=0
endif
endif
>P
; on/off webui
if pwr[1]>0
then
pwrt=1
dim="FF55"+hn(dval*dmul)+"05DC0A"
->SerialSend5 %dim%
else
pwrt=0
->SerialSend5 %doff%
endif
; subroutine
#snddim(tmp)
dim="FF55"+hn(tmp*dmul)+"05DC0A"
->SerialSend5 %dim%
#
@sqw534
Copy link

sqw534 commented Jan 16, 2020

@DpunktS Hej, sorry can‘t help you as I do not have an RGB Dimmer. regs Sven

@OlfrygtACAB
Copy link

Hallo an alle,

ich habe ein kleines Problem! ich habe alles, wie in der Anleitung beschrieben, gemacht. wenn ich eine LED 230V dort anschließe funktioniert alles, wenn ich eine Halogen anschließe geht auch alles, wenn ich aber 9 LEDs 230V anschließe geht es über den Taster S aber über MQTT geht es nicht mehr.

Hello everyone,

I have a small problem! I did everything as described in the instructions. If I display a LED 230V, everything works, if I display a halogen, everything works, but if I display 9 LEDs 230V, you can use the S button, but you can no longer use MQTT

@sqw534
Copy link

sqw534 commented Jan 27, 2020

@OlfrygtACAB Hej, does it work if you use the web-interface? Up to how many LEDs are working? regs

@ressof
Copy link

ressof commented Jan 30, 2020

Im working on a script that is working with two dimmers.
But its the first time writing script for Tasmota.
What does "sld1=Dimmer" do? Is Dimmer refering to the WebUi slider? and the PWM1?
What is the reference to PWM2?

This is what I got so far

>D
sw1=0
sw2=0
pw=0
cnt=0
tmr1=0
tmr2=0
hld1=0
hld2=0
tgl1=0
tgl2=0
sld1=0
sld2=0
dcmd=""
shtpl=2
shtpu=10
dd1=0
dd2=0
dmp=2.2
dsp=2
dll=15
dul=95
dv1=70
dv2=70

>B
=>Counter1 0
=>Counter2 0
=>Baudrate 9600
=#sd1(0)
=#sd2(0)

>F
cnt1=pc[1]
cnt2=pc[2]
if chg[cnt1]>0
then sw1=1
else sw1=0
endif
if chg[cnt2]>0
then sw2=1
else sw2=0
endif

tmr1+=1
tmr2+=1

if sw1==0
and tmr1>shtpl
and tmr1<shtpu
then
tgl1^=1
if tgl1==1
then
=#sd1(dv1)
else
=#sd1(0)
endif
endif

if sw2==0
and tmr2>shtpl
and tmr2<shtpu
then
tgl2^=1
if tgl2==1
then
=#sd2(dv2)
else
=#sd2(0)
endif
endif

if sw1>0
then
if hld1==0
then
dd1^=1
endif
if tmr1>shtpu
then
hld1=1
if tgl1>0
then
if dd1>0
then
dv1+=dsp
if dv1>dul
then
dv1=dul
endif
=#sd1(dv1)
else
dv1-=dsp
if dv1<dll
then
dv1=dll
endif
=#sd1(dv1)
endif
endif
endif
else
tmr1=0
hld1=0
endif

if sw2>0
then
if hld2==0
then
dd2^=1
endif
if tmr2>shtpu
then
hld2=1
if tgl2>0
then
if dd2>0
then
dv2+=dsp
if dv2>dul
then
dv2=dul
endif
=#sd2(dv2)
else
dv2-=dsp
if dv2<dll
then
dv2=dll
endif
=#sd2(dv2)
endif
endif
endif
else
tmr2=0
hld2=0
endif

>E
sld1=Dimmer
if chg[sld1]>0
then
if sld1>0
then
dv1=sld1
else
tgl1=0
endif
endif

sld2=Dimmer
if chg[sld2]>0
then
if sld2>0
then
dv2=sld2
else
tgl2=0
endif
endif

if pwr[1]==1
then
tgl1=1
=#sd1(dv1)
else
tgl1=0
=#sd1(0)
endif

if pwr[2]==1
then
tgl2=1
=#sd2(dv2)
else
tgl2=0
=#sd2(0)
endif

#sd1(pw)
dcmd="FF5501"+hn(pw*dmp)+"3005DC0A"
=>SerialSend5 %dcmd%
=>Dimmer %pw%
#

#sd2(pw)
dcmd="FF5502"+hn(pw*dmp)+"3005DC0A"
=>SerialSend5 %dcmd%
=>Dimmer %pw%
#

But its to long.

Can you pass two parameters to one sub?

@thxthx0
Copy link
Author

thxthx0 commented Jan 31, 2020

@ressof

Hi, plz give it a try (with SetOption68 1):
WiFi-2way-Dimmer-QS-WiFi-D02-Script.txt

I don't have a QS-WiFi-D02 2-way dimmer for testing.

Yes, "=Dimmer" refers to the WebUI slider and PWM1.
Could you please tell us the GPIOs for Counter1 and 2,
so we can add a template.

@Bajger-M
Copy link

Bajger-M commented Feb 8, 2020

It's better to let the orginal firmware or switch to tasmota?

@gadget78
Copy link

hey im using this script as set out in https://templates.blakadder.com/qs-wifi_D01_dimmer.html

is it suppose to, or would it be easy to change it so that...
when your holding button to change dim amount, and it gets to the min/max
could it briefly pause and then flip to go back the other way

so when maxed out, brief pause, and then it would flip and start dimming, and then when at min, brief pause, then start brightening up, till it hits max, brief pause, and then flip and start dimming etc etc etc

@sqw534
Copy link

sqw534 commented Aug 19, 2020

@gadget78: I think it is easily possible to reality this. To simply change the direction changing changing the dimming section with the code belief should work (not tested)

Pausing on upper or lower level might be done via an extra variable counter.

Have fun

; increase dim level
dimval+=dimstp
if dimval>dimul
then
; upper limit change direction
dimdir^=1
endif
=#senddim(dimval)
else
; decrease dim level
dimval-=dimstp
if dimval<dimll
then
; lower limit change direction
dimdir^=1
endif

@sqw534
Copy link

sqw534 commented Aug 19, 2020

@Bajger-M Mix original firmware didn‘t worked. The dimmerd turned off the lights randomly...so no option to keep it. I wanted to control them via openhab so Tasmota was my choice. Working stable since more than a year.

@gadget78
Copy link

@sqw534 Thankyou for reply, the code and how quick this was!!
the code is working a treat, doesnt really need pause either as the minimal crossover pause is good enough :) !!
well happy, much more user friendly now :)

@ajaxedwin
Copy link

ajaxedwin commented Feb 15, 2021

@thxthx0 really love your script. I flashed my QS-WiFi-D01-TRIAC 150W Dimmer with Tasmota (9.2.0.7 with scripting). after adding the script I am able to toggle and change brightness using a pulse switch. But when I click the Toggle switch within the Tasmota web interface the device will crash and reboot. I get the following error in the Console:
23:48:02.288 MQT: tele/dimmer1/INFO1 = {"Module":"WiFi-Dimmer","Version":"9.2.0.7(scripting)","FallbackTopic":"cmnd/DVES_738628_fb/","GroupTopic":"cmnd/tasmotas/"}
23:48:02.293 MQT: tele/dimmer1/INFO2 = {"WebServerMode":"Admin","Hostname":"Tasmota-dimmer1","IPAddress":"xxx.xxx.xxx.xxx"}
23:48:02.297 MQT: tele/dimmer1/INFO3 = {"RestartReason":"Hardware Watchdog"}

Sometimes the action fails with error:
23:55:48.060 MQT: tele/dimmer1/INFO3 = {"RestartReason":{"Exception":4,"Reason":"Software Watchdog","EPC":["40003b53","00000000","401011cf"],"EXCVADDR":"00000000","DEPC":"00000000"}}

Has anyone seen this issue before?

@thxthx0
Copy link
Author

thxthx0 commented Feb 17, 2021

@ajaxedwin

Hi, I have the same problem.
I'm not sure if it's related to the new features of Tasmota or if it's a RAM problem.
That's why my dimmer is still running on Tasmota 8.x

@ajaxedwin
Copy link

Thanks for the reply!
any idea where I can download Tasmota 8.x with scripting enabled?
On the internet I can only find the latest version.

@sqw534
Copy link

sqw534 commented Feb 18, 2021

Hej,

if 7.1.1.1 ist as well ok for you there is an stripped down one incl. scripting, mqtt, hue and timer here:
https://github.com/sqw534/QS-Wifi-D01-Tasmota/blob/master/tasmota_7.1.1.1_scripter_mqtt_hue_timer.bin

Is running stable on all my devices since +1 year.
Otherwise you have to compile your own binary. Sources could be found here:
https://github.com/arendst/Tasmota/tree/pre-release-8.5.1
and an environment should be here: https://gitpod.io/#https://github.com/arendst/Tasmota/tree/pre-release-8.5.1

Might be that it doesn't work from scratch but could be a starting point.
A Howto is here: https://tasmota.github.io/docs/Gitpod/

Hope this helps. Enjoy

@ajaxedwin
Copy link

Hi,

Thank you so much!
For now I installed Tasmota 7.1.1.1 on the dimmer and works like a charm :-D

@dbrb2
Copy link

dbrb2 commented Mar 10, 2021

For info, I have been using 9.1.0 with the dimmer script fine
The latest, 9.3.1, seems to work - but as soon as the light is switched (which works) there is a watchdog restart

@strike56
Copy link

Upgrade to version 9.3.1. And I have watchdog restart too. :-(

@strike56
Copy link

in version 9.3.1 something a problem with SUBROUTINE calls.
I just remove #senddim(tmp)..
And replace all off CALLS:

#senddim(0)

to

=>SerialSend5 FF550005DC0A
=>Dimmer 0

and replace all on CALLS

=#senddim(dimval)

to

dim="FF55"+hn(dimval*dimmlp)+"05DC0A"
=>SerialSend5 %dim%
=>Dimmer %dimval%

It's a temporary variant, but now all works.

@dbrb2
Copy link

dbrb2 commented Mar 18, 2021

That didn't fix it for me - though luckily 9.1 works fine :-)
`>D
sw=0
tmp=0
cnt=0
tmr=0
hold=0
powert=0
slider=0
dim=""
shortprl=2 ;short press lo limit
shortpru=10;short press up limit
dimdir=0 ;dim direction 0/1
dimmlp=2.2 ;dim multiplier 1..2.55
dimstp=2 ;dim step/speed 1..5
dimll=15 ;dim lower limit min. 0
dimul=95 ;dim upper limit max. 100
dimval=70 ;start dim level 0..100

B
=>print > WiFi-Dimmer-Script-v1.0 <
=>Counter1 0
=>Baudrate 9600
; boot sequence
dim="FF55"+hn(dimval*dimmlp)+"05DC0A"
=>SerialSend5 %dim%
=>Dimmer %dimval%
delay(1000)
=>SerialSend5 FF550005DC0A
=>Dimmer 0

F
cnt=pc[1]
if chg[cnt]>0
; sw pressed
then sw=1
else sw=0
; sw not pressed
endif
; 100ms timer
tmr+=1
; short press
if sw==0
and tmr>shortprl
and tmr<shortpru
then
powert^=1
; change light on/off
if powert==1
then
dim="FF55"+hn(dimval*dimmlp)+"05DC0A"
=>SerialSend5 %dim%
=>Dimmer %dimval%
else
=>SerialSend5 FF550005DC0A
=>Dimmer 0
endif
endif

; long press
if sw>0
then
if hold==0
then
; change dim direction
dimdir^=1
endif
if tmr>shortpru
then
hold=1
if powert>0
; dim when on & hold
then
if dimdir>0
then
; increase dim level
dimval+=dimstp
if dimval>dimul
then
; upper limit
dimval=dimul
endif
dim="FF55"+hn(dimvaldimmlp)+"05DC0A"
=>SerialSend5 %dim%
=>Dimmer %dimval%
else
; decrease dim level
dimval-=dimstp
if dimval<dimll
then
; lower limit
dimval=dimll
endif
dim="FF55"+hn(dimval
dimmlp)+"05DC0A"
=>SerialSend5 %dim%
=>Dimmer %dimval%
endif
endif
endif
else
tmr=0
hold=0
endif

E
slider=Dimmer
; slider change
if chg[slider]>0
then
; dim according slider
if slider>0
then
dimval=slider
else
powert=0
endif
endif
if pwr[1]==1
; on/off webui
then
powert=1
dim="FF55"+hn(dimval*dimmlp)+"05DC0A"
=>SerialSend5 %dim%
=>Dimmer %dimval%
else
powert=0
=>SerialSend5 FF550005DC0A
=>Dimmer 0
endif
`

@mikketeus
Copy link

mikketeus commented Jun 24, 2021

Somehow it worked on one of my dimmers but not the other one!
Same configuration, same everything!..

@thxthx0
Copy link
Author

thxthx0 commented Sep 20, 2021

Hi, I have updated the Script to v2.0a with some changes and fixes to work with Tasmota v9.5.0 / 9.5.0.8 ...
(Ver.a: dimming stops when minimum or maximum brightness is reached)

and an alt. Script v2.0b
https://gist.github.com/thxthx0/12074f1f5249e14b2a0aada75f590c9b
(Ver.b: changes the dimming direction when min/max brightness is reached and doesn't stop dimming)

also working with the precompiled tasmota-scripting.bin / tasmota-scripting.bin.gz files:
https://github.com/tasmota/install/tree/main/firmware/unofficial

Template:
{"NAME":"QS-WiFi-D01 150W Dimmer","GPIO":[0,3200,0,3232,0,0,0,0,0,352,416,0,0,1],"FLAG":0,"BASE":18}

THANKS 👍 @ askold2, sqw534, gadget78, strike56 ...

@dbrb2
Copy link

dbrb2 commented Nov 29, 2021

I found today that the scripting builds I was using did not support MQTT autodiscovery - so I rebuilt one on gitpod, and got discovery working again.

The dimming on this version works as expected using the scripts above, as does the discovery over MQTT - however power toggles no longer work at all.

On attempting a toggle I now get:

23:54:29.999 SRC: WebGui from 192.168.1.154
23:54:30.002 var not found: Dimmer
23:54:30.012 MQT: tele/bedroom_light/STATE = {"Time":"2021-11-28T23:54:30","Uptime":"0T00:01:49","UptimeSec":109,"Heap":22,"SleepMode":"Dynamic","Sleep":10,"LoadAvg":19,"MqttCount":1,"POWER":"ON","Dimmer":40,"Fade":"OFF","Speed":1,"LedTable":"ON","Wifi":{"AP":1,"SSId":"barkernet","BSSId":"78:65:59:7C:9E:47","Channel":6,"Mode":"11n","RSSI":98,"Signal":-51,"LinkCount":1,"Downtime":"0T00:00:03"}}
23:54:30.018 MQT: stat/bedroom_light/RESULT = {"POWER":"ON"}
23:54:30.021 MQT: stat/bedroom_light/POWER = ON
23:54:30.555 CFG: Saved to flash at FB, Count 1356, Bytes 4096
23:54:42.435 WIF: Sending Gratuitous ARP
23:54:44.759 WIF: Checking connection...

Any thoughts?

@thxthx0
Copy link
Author

thxthx0 commented Nov 29, 2021

Any thoughts?

Hi,

this must be set in my_user_config.h / user_config_override.h:

#define USE_SCRIPT                               // Add support for script (+17k code)
  #define SCRIPT_POWER_SECTION                   // enable >P section (execute on power changes)

or you may use:
https://github.com/tasmota/install/blob/main/firmware/unofficial/tasmota-scripting.bin
or
https://github.com/tasmota/install/blob/main/firmware/unofficial/tasmota-scripting.bin.gz

@dbrb2
Copy link

dbrb2 commented Nov 29, 2021

Ah - thanks very much, I missed that line. I will try it this evening.
When I initially tried the unofficial build, the dimmer worked, but then MQTT autodiscovery did not - I think it may be left commented out in the automatic builds - but I'll take a look

Thanks for the help

@dbrb2
Copy link

dbrb2 commented Nov 29, 2021

Thanks - getting there!
The precompiled binaries functioned, but did not have auto discovery MQTT enabled
My own build, with your extra #define line, got the power on/off section working fine along with auto discovery

At this point, the dimming works, but toggles on/off do not. I suspect there are some status messages that are not being sent/received as expected.

I think that is because I had not set:

#define TELE_ON_POWER true

from its default of false. Doing this has partially resolved the issue - there is now a STATE message on every power ON, but not one on POWER OFF. Still looking into why!

@thxthx0
Copy link
Author

thxthx0 commented Nov 29, 2021

Domoticz requires a STATE message

maybe autodiscovery plugin issue (?) and maybe SetOption59 on helps,

I also use the dimmer with Domoticz and it works without autodiscovery,
(added the dimmer manually to Domoticz and inserted the Idx in Tasmota).

@dbrb2
Copy link

dbrb2 commented Nov 29, 2021 via email

@MFe-tasmota
Copy link

MFe-tasmota commented Jan 29, 2022

Hi there,

using QS-WiFi-D01-TRIAC since about 2 years with old tasmota an 0.3 script. Since about a year WebUI does reacts anymore, last week dimmer stops working completly (also LED goes defective). So i have reflashed tasmota to reanimate the dimmer, using the actual version. Usind the new v2.0a script, but the light changes with some delay on changes in the WebUI slider and when I dim below 9% the LED begans to flicker and the dimmer resets. So i set limit minimal dimming rangen by setting dlo to 10 and with it works well with the wall switch, but using the slider in the webUi dimvalues below 10 can still be set causing a reset of the dimmer.

So i have read the scripting manual und changed some parts in the script 2.0a:

Changed GPIO14 from pwr1 to relay1 (to get rid of the "old" slider)

replaced

'>E
; slider
;slid=Dimmer
;if chg[slid]>0
;then
; dim per slider
;if slid>0
;then
;dval=slid
;else
;pwrt=0
;endif
;endif

by

'>W
sl(%dlo% %dup% dval "%dlo%" "%dval%" "%dup%")

for new slider.

When Dimmer is off and a differend dval than before is choosen with the new slider, die dimmer goes directly on. To also get correct state of relay1 (must toogle to Power1 1) by adding the following to the script in ^F section in dval-if-loop:

if sw==0
then
=>Power1 1
endif

before:

if chg[dval]>0
then
=#snddim(dval)
=>Dimmer %dval%
endif

after change:

if chg[dval]>0
then
=#snddim(dval)
=>Dimmer %dval%
if sw==0
then
=>Power1 1
endif
endif

pros:

  • The new slider now shows the minimum an maximum values and the actual dimvalue an the LED and reacts much faster in changes in WebUi and can only moved in the ranges set in >D section.

cons:

  • none konwn yet

Best regards

@Krrost
Copy link

Krrost commented Nov 18, 2022

@MFe-tasmota
Cool!

A possible downside is that by changing GPIO14 to Relay, the integration with Home assistant will not be automatic, as it will be considered as a simple switch, losing the dimmer functionality.

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