Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
>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
=#senddim(dimval)
delay(1000)
=#senddim(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
=#senddim(dimval)
else
=#senddim(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
=#senddim(dimval)
else
; decrease dim level
dimval-=dimstp
if dimval<dimll
then
; lower limit
dimval=dimll
endif
=#senddim(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
=#senddim(dimval)
else
powert=0
=#senddim(0)
endif
; subroutine dim
#senddim(tmp)
dim="FF55"+hn(tmp*dimmlp)+"05DC0A"
=>SerialSend5 %dim%
=>Dimmer %tmp%
#
@maximoei

This comment has been minimized.

Copy link

maximoei commented Nov 7, 2019

Thank you!

@thxthx0

This comment has been minimized.

Copy link
Owner Author

thxthx0 commented Nov 7, 2019

:)
1

@askold2

This comment has been minimized.

Copy link

askold2 commented Nov 9, 2019

Thanks for the script. I modified it a bit. I fixed a bug that did not allow me to turn on the dimmer immediately with the desired brightness by wifi. And he did so that after turning off the power, the previously set brightness was restored. Unfortunately, I do not know how to put the code here so that the site does not spoil it. Well, at least so:

>D
sw=0
tmp=0
cnt=0
tmr=0
hold=0
powert=0
slider=0
dim=""
shortprl=1 ;short press lo limit
shortpru=10;short press up limit
dimdir=0 ;dim direction 0/1
dimmlp=2.55 ;dim multiplier 1..2.55
dimstp=2 ;dim step/speed 1..5
dimll=10 ;dim lower limit min. 0
dimul=100 ;dim upper limit max. 100
p:dimval=20 ;start dim level 0..100

>B
=>print > WiFi-Dimmer-Script-v1.1 <
=>Counter1 0
=>Baudrate 9600
; boot sequence
=#senddim(dimval)
delay(1000)

>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
=#senddim(dimval)
=>Power 1
else
=#senddim(0)
=>Power 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
else
; decrease dim level
dimval-=dimstp
if dimval<dimll
then
; lower limit
dimval=dimll
endif
endif
=#senddim(dimval)
=>Dimmer %dimval%
endif
endif
else
tmr=0
if(hold>0)
then
hold=0
svars
endif
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
=#senddim(dimval)
svars
else
powert=0
=#senddim(0)
endif

; subroutine dim
#senddim(tmp)
dim="FF55"+hn(tmp*dimmlp)+"05DC0A"
=>SerialSend5 %dim%
#

@thxthx0

This comment has been minimized.

Copy link
Owner Author

thxthx0 commented Nov 10, 2019

Thanks @askold2,
but it's more a feature than a bug :)

personally, I don't like "Light ON" after power failure / reset / restart...

@askold2

This comment has been minimized.

Copy link

askold2 commented Nov 10, 2019

There is a PowerOnState command for this. It works great in my version of the script.

@keesdekraker

This comment has been minimized.

Copy link

keesdekraker commented Nov 20, 2019

Great work! Thanks for the script.

When powered off, the attached light bulb (halogen 40w E27 screw fitting) sometimes starts to glow faintly.
When sending the serial command to power off (=dimlevel 0%) the bulb turns off again, but only for a while and then start to glow faintly again.
Do you guys recognize this behaviour?

I fixed this by adding a few lines to the script and by basically repeating the serial command every few seconds when in off state.
Not the most elegant way I must say.

Looks like whenever the ESP MCU is transmitting over wifi, this somehow influences the secondary MCU.

@dbrb2

This comment has been minimized.

Copy link

dbrb2 commented Nov 21, 2019

Out of interest, what have people seen their loadAvg to be when using this solution? My devices are working fine - and when off, the loadAvg is very low. But when the light is on, the loadAvg rises to 999 and stays there:

22:34:19 MQT: light/tele/STATE = {"Time":"2019-11-21T22:34:19","Uptime":"0T01:20:15","UptimeSec":4815,"Heap":27,"SleepMode":"Normal","Sleep":250,"LoadAvg":3,"MqttCount":1,"POWER":"OFF","Dimmer":0,"Fade":"OFF","Speed":1,"LedTable":"ON","Wifi":{"AP":1,"SSId":"xxxxxx","BSSId":"xxxxxxx","Channel":11,"RSSI":48,"LinkCount":1,"Downtime":"0T00:00:10"}}

Turned light on, then...

22:44:24 MQT: hall_light/tele/STATE = {"Time":"2019-11-21T22:44:24","Uptime":"0T01:30:20","UptimeSec":5420,"Heap":27,"SleepMode":"Normal","Sleep":0,"LoadAvg":273,"MqttCount":2,"POWER":"ON","Dimmer":70,"Fade":"OFF","Speed":1,"LedTable":"ON","Wifi":{"AP":1,"SSId":"xxxx","BSSId":"xxxx","Channel":11,"RSSI":48,"LinkCount":1,"Downtime":"0T00:00:10"}}

This doesn't seem to affect performance....

@RalphMeuse

This comment has been minimized.

Copy link

RalphMeuse commented Nov 23, 2019

@thxthx0 this is great!

Yesterday I reflashed a Tuya dimmer OTA with tuya-convert. Worked like expected (did some Tuya switches before). Then upgrade to newest version (OTA, thehackbox.org) and uploaded the scriptenabled Tasmota (https://github.com/arendst/Tasmota/files/3776333/Tasmota-v7.0.0.1-w-Scripting-2.6.x-1MB.zip) to one of my own servers (took the .bin out of it). Upgraded (OTA) to that version and the device was 'bricked'.

So, took a soldering iron, opened the box en flashed it using serial. Tried upgrading using 'file upload' and got an error. THEN I used Google (should have done that before) and 'upgraded' to the minimal version of Tasmota FIRST and the uploaded the scriptable version. That is running fine now! I still have to template the device, run the script and make it MQTT, but some questions first:

  1. Is the scriptable version maintained regularily and can that version be used to update the devices using tasmoAdmin
  2. The docs of Tasmota state that the minimal version is used intermediately when flashing a version that is to big to fit in the remaining flash. In my case (using my own webserver) that did not work. Does someone know how that can be achived?
@thxthx0

This comment has been minimized.

Copy link
Owner Author

thxthx0 commented Nov 23, 2019

@keesdekraker
Here a reference to the other comment for all: arendst/Tasmota#5737 (comment)

@dbrb2
interesting, same here, I didn't notice "LoadAvg":999 before.
Maybe "just a kind of" software issue processing the LoadAvg value,

in new Tasmota 7.0.0.6 version it shows "LoadAvg":99 instead of 999

The temperature and power consumption of the ESP8266 doesn't really increase,
and as you mentioned, no effect to the performance.

@RalphMeuse

and the device was 'bricked'.

I'm afraid it's because of changing Cores (2.6.0 -> 2.6.1 -> pre2.6)
without Reset 5 or full erase always a risky operation.

  1. So the scriptable version is already outdated and cannot be maintained directly,
    (blakadder doesn't like too many PRs at the Templates Repo ;) )
    but it's easy to compile (GitPod / TasmoCompiler / Portable VSC)

  2. That can be achived when using direct web update via OTA-URL (http://thehackbox.org/tasmota/...)
    it's called OTA Magic :) https://github.com/arendst/Tasmota/wiki/Upgrading#upgrade-by-web-server
    but no idea how it could work at your own webserver.

@sqw534

This comment has been minimized.

Copy link

sqw534 commented Nov 28, 2019

Hej, I would like to thank you as well.

I have 8 dimmers and 7 of them have done spooky things like turning off without any action taken or glowing like @keesdekraker reported above. Only an accidentically WLAN-Downtime solved this problems. But ok, a Wifi Dimmer that only works if Wifi is off doesn't make a lot of sense or fun.....based on your and blakaddder great work I was able to flash them via tuya-convert and adding your script.....what should I say: Working, no problems!! 😀😀😀ThxThx....

As an addditional info: The linked firmware can't be flashed via OTA/tuya-convert as it is greater than 512kb which is the maximum for OTA (half of the flash). I have compiled the actual 7.0.0.6 version on Gitpod with scripting, mqtt, Hue, web & timers included which is below 512kb so that I can be flashed via tuya-convert/OTA. If anybody is interested, let me know.

Thanks again👍👍👍

@sqw534

This comment has been minimized.

Copy link

sqw534 commented Nov 28, 2019

ah, just forgotten: I know what the variable powert does, but could you tell what powert stands for? regs

@keesdekraker

This comment has been minimized.

Copy link

keesdekraker commented Nov 28, 2019

@sqw534, I have resolved the issue by reverting back to a Tasmota build from the current master branche.
No more sudden dimming actions without commands. It seems it is firmware related after all.
Still having some stability issues with Tasmota though (crashes after some time of Tasmota).

I am very interested in your precompiled binary file. Could you share this bin file plz?

BTW: there is a minimal pre-compiled version of Tasmota, which can be used for two step OTA.
Just OTA flash the minimal binary first using the WebUI and then flash the bigger sized binary using the WebUI.

@RalphMeuse

This comment has been minimized.

Copy link

RalphMeuse commented Nov 28, 2019

@sqw534, I'm also very interested in your pre-compiled version 7.0.0.6. I had MAJOR stability issues (WiFi?) with the supplied Tasmota-v7.0.0.1-w-Scripting-2.6.x-1MB.zip. Using GetPod I compiled a 6.7.1 (2.3.0 core) version and running tests on a Sonoff Basic now. Because I kept losing connection with the Tuya Dimmer using 7.0.0.1 it took a lot of time and I need to be sure that my version (using GitPod for the first time) is stable enough to use it on the Dimmer.
One more question to you all: I just started converting my Tuya switches (and now the first Dimmer) to Tasmota and use Home Assistant as automation platform. Does the version with scripting also support MQTT and how should a Dimmer be defined in Home Assistant?
And did someone already convert a smart light (white or dimmable or colored/dimmable) or a LED strip or smart socket from Tuya to Tasmota? Very interested in your experiences. In Holland the firm Action sells LSC smart lights and sockets for nice prices and I would like to convert them all. Anyone interested in joining in to the project?

@keesdekraker

This comment has been minimized.

Copy link

keesdekraker commented Dec 2, 2019

@sqw534, are you still running your build without any stability or dimming issues?
My wife is getting annoyed by the fact that the living room light isn't doing what it's suppose to be doing and is getting ready to rip out wall switches now.

Could you share your build? I really could ue some good news, as this dimmer is giving me quite a few headaches.
Many thanks in advance!

@sqw534

This comment has been minimized.

Copy link

sqw534 commented Dec 3, 2019

Hej,

@keesdekraker & @RalphMeuse: Yes, I am still running the build and will share it.

I also have added the ability to dim up if the dimmer is off and fading on/off to the script. Will share this as well. This is only tested on the QS-Wifi D01 Dimmer (https://blakadder.github.io/templates/qs-wifi_D01_dimmer.html) So I can‘t tell you if it it is working on other tuya dimmer.

@RalphMeuse: Yes I have compiled mqtt and scripting and Hue within the build. Hue is unfortunately not stable when using Alexa.

I just have tried to recompile my 7.0.0.6 version, but it fails on gitpod. So I have recompiled it on 7.1.1.1. First tests runs ok but no long term tests right now...

I have compiled it without any wifi settings, so device will come up in AP mode. Just connect to the Wifi tasmota-xxxx and open http://192.168.4.1. You now can connect it to your own Wifi. Device will use DHCP so you have to find your local IP after the reboot. (ping tasmota-xxxx worked in my environment)
After reboot you have to configure the module as described on blakadders site. Finally you need to Edit Script to get the button presses running. Script either from thxthx0 (again thanks for it!!) or use the attached script which includes dim-up from 0 when powered off and fading in/out (configurable within the script fading=0/1). Do not forget to hit the "script enabled" check box

I have added the file my_user_config.h as well, so if you want to change the bin or preconfigure your Wifi/IP on gitpod (Have a look at: https://github.com/arendst/Tasmota/wiki/Compiling-Tasmota-on-Gitpod) just upload this one.

I described the steps a little bit more detailed, just in case somebody else find this post..

Would be great if you could share, if this runs for you or otherwise share your device type and the misbehaviour. We might can work together on this.

Files are located here:
https://github.com/sqw534/QS-Wifi-D01-Tasmota

Enjoy testing...

@sqw534

This comment has been minimized.

Copy link

sqw534 commented Dec 3, 2019

@RalphMeuse Just to answer your other questions: I do not use HomeAssitent, I am on openhab, so I can't help you on the configuration. Mqtt is enabled within the bin. So you could use it. I have read something about the LSC while searching on the dimmer. There should already be some information around, but can't point you to a specific link.

Best regards

@RalphMeuse

This comment has been minimized.

Copy link

RalphMeuse commented Dec 4, 2019

@sqw534 MANY thanks for sharing. I hope to find time in the weekend to try it.

The last week I invested (many and frustrating) hours to find what the problem with my device is. I think I found my problem: if the WiFi signal is low, the minute I activate PWM1 on GPIO14 (or GPIO2 for that matter) I loose connection to the device. I have to move it closer to the WiFi point and issue some http:... cm?cmnd=reset 5 to reset the device so the PWM is deactivated and then the WiFi runs fine again.

The device is a 'real' QS-WiFi-D01-TRIAC (from MOES). I read somewhere that the 'clones' have WiFi stability issues when (software) PWM is used.

So, the next question is: do I have to active a PWM at all? Or will it work without it, because of the separate CPU? And will MQTT work automatically then (probably not) or should I implement that in the script?

I'm very willing to share. I have to find the time to test and also ordered some more devices, in case I brick one...

I have 5 of these dimmers in my house (next to about 20 switches) and it would be GREAT if working with MQTT/HA, because the time delay from Tuya to HA when the state changes is between 5 and 30 seconds. And my wife has the same feelings about that as Kees'es :)

Best regards

@sqw534

This comment has been minimized.

Copy link

sqw534 commented Dec 4, 2019

@RalphMeuse: I started with these dimmers around a year ago as there havn't been any other (cheap) Wifi-Dimmer available. I have had as well a lot of trouble. Bulbs starts glimming, turned sometime sporadically off and sometimes often off.
My first Test-order-Dimmer works as expected, the following 8 didn't. The only difference was the firmware delivered from 1.0.0 to 1.0.1.

This all stopped as a friend of my daughter accidentically turn our WLAN off by tapping on button of router. No more glimming, turning off...so I decided to connect the dimmer to another/old router and turn this router off. This dramatically reduces the annoyance of my wife ;)....No more voice control via alexa, but the lights are still dimmable and stays on/off as expected. So there is a problem with the firmware-wifi combination. In my case, tasmota solved this issue.

Looking back (and seeing your comment about frustrating hours) it should have waited for the Shelly Dimmers. They are on the market now, much easier to handle, works out of the box, having much more features, costs are simliar small (20€), having support from manufacturer are prepared to be flashed serial via easy accessable pins. I even think I would have exchanged all of the 9 QS-Wifi-D01 with shellys if blakadder and thxthx0 hadn't had the idea to research and share this.

I have learned a lot regarding openhab, tasmota, programming, electrical installations, LEDs, etc while I have tried solving this problem. But I might have been even nice to grab a good book or doing something else as spending time on a more or less faulty product.

So keep on testing, but stay happy!
Otherwise: Exchange them with shellys.

@thxthx0

This comment has been minimized.

Copy link
Owner Author

thxthx0 commented Dec 4, 2019

@sqw534
"powert" is my short for power toggle, but I think you already know,
because of your deep dive into this 👍

@RalphMeuse
PWM1 is needed for the slider and (easy) home automation integration
(Push-button operation is possible without a slider/PWM1).

To improve WiFi you could try Reset 3 (and then disconnect/connect power) to calibrate WiFi.
And you can set WifiPower up to 20.5 (default 17dBm)

There are already some LSC templates at https://blakadder.github.io/templates/
and some info here: arendst/Tasmota#6495

@RalphMeuse

This comment has been minimized.

Copy link

RalphMeuse commented Dec 5, 2019

@sqw534 and @thxthx0
This is REALLY awesome!!!!!!!!
The 7.1.1.1 build from @sqw534 is definitely different from mine! For starters: it works! Activating PWM does NOT disconnect the device from the WiFi and the size! Perfect! No need for intermediate -minimal.bin. Just flash it after Tuya-convert (-basic.bin) and BANG! Find AP, WiFi parameters -> ready. THANKS A LOT!
Few remarks about this build:

  • HUE emulation is default on (I do not use it)
  • Something strange is happening with MQTT (state) message: The POWER value is sent as a separate topic, but the Dimmer value is embedded in a json packet that is sent to the RESULT topic (POWER is also in this json packet, BTW). Is it possible to send the Dimmer value to it's own topic? It makes is easier for me to integrate it into HA (and gBridge for Google support).

Maybe this is Tasmota default behaviour. Then I have to find a way to automatically copy the right json part from the packet to it's own topic. Node-RED?
Thanks again for the support and special thanks from my wife. She loves you guys :)

@sqw534

This comment has been minimized.

Copy link

sqw534 commented Dec 6, 2019

Hej, great that is working for you.

Regarding the build/hue/mqtt:

This seems to be the tasmota default settings/behaviour.

I json statement in result differs if you send a dimmer value (contains Dimmer) or a Power on/off command (doesn‘t contain Dimmer). In openhab I could use jsonpath transformation but the missing Dimmer value causes an error within openhab. I takes me as well a little bit to get an openhab rule for
that. I don‘t know HA, so I can‘t help you on this.

@thxthx0 Thanks for your reply. Indeed I realized that a little bit later.

Best regards.

@RalphMeuse

This comment has been minimized.

Copy link

RalphMeuse commented Dec 7, 2019

@sqw534 I made a Node-RED flow that (in case the MQTT Dimmer member is supplied) the json properties Dimmer and POWER are copied over to .../stat/.../Dimmer and .../stat/.../POWER topics. Those topics are used in my HA configuration. And also by Google (using gBridge.io). Seems to work fine that way... BTW: the copiïng of the POWER value seems necessary, because when the Tasmota Dimmer is off and the SLIDER is used to turn it on, the RESULT json indicates POWER ON, but the POWER topic is still OFF. Don't know if this is Tasmota default or a flaw in the script...

@RalphMeuse

This comment has been minimized.

Copy link

RalphMeuse commented Dec 8, 2019

Made one addition to the Node-RED flow: when the physical switch is used (and the light fades out) in the end the json in the RESULT topic states "OFF", but the POWER topic is NOT set to "OFF". So HA (and Google) doesn't know it's off. Added it to the flow and now it works as expected!

@RalphMeuse

This comment has been minimized.

Copy link

RalphMeuse commented Dec 8, 2019

@sqw534 is it possible to compile a version that supports more possible characters for the script? I ordered a 2 gang dimmer and would like to try to make that work. But with 40 chars left...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.