I am using Sonoff lately for switching stuff on and off. I really like the Sonoff-Tasmota firmware in favor of my own written software. I have started to use this firmware also for self produced LED dimmers and with the module setting WEMOS D1 I can now drive my LED’s. Some time ago I got some help to calculate new values for the PWM output as the sliders in OH2 give 1-100 and I needed 1-1023.
The transform script looks as follows:
(function(multi100) {
var percent = multi100 / 100.0;
var value = 1023 * percent;
return Math.round(value);
})(input)
now however because I have made these LED sonoffs part of a light group, the “OFF” of “ON” does not seem to work as PWM is expecting a value 0-1023. So a NaN is reported via MQTT.
Somebody can help me with idea’s to overcome this; for now I have a rule that sends a “0” after receiving “OFF”. Is it possible to make this part of the javascript, ie send 0 if OFF is received?
Can you check to see if multi100 === “OFF” in the rear and return 0? I’m not sure what you want to map ON to buy can use the same already if this works for OFF.
I think the variable is passed in is a string (JavaScript plays fast and lose with data types) so the above should work.
you were right and I should upgrade my coding skills . So this is what the solution looks like if using Sonoff-Tasmota WEMOS D1 for PWM. The item declaration in combination with the 2 javascripts will translate OH2 dimmer values 0-100 to 0-1023 on one side and 0-1023 to 0-100 on the returnpath. Latter meaning that if the PWM value is influenced by something else the slider in OH2 will move to the right position.