Sonoff "own build" and javascript

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)

thx to @rlkoshak

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.

item declaration

Dimmer sonoff_2A16BC "LedStrip[%d %%]" <slider> (gLights_VH_Studeerkamer)
	{mqtt=">[mosquitto:home/esp8266/tasmota/sonoff_2A16BC/cmnd/Pwm1:command:*:JS(multi100.js)]"}  

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.

Rich,

I am not sure what you exactly mean by “Can you check to see if multi100 === “OFF” in the rear and return 0?”

regards

Han

(function(multi100) {
  if(multi100 === "OFF") return 0;

  var percent = multi100 / 100.0;
  var value = 1023 * percent;
  return Math.round(value);
})(input)
1 Like

@rlkoshak,

It looked like it was working but I forgot to take out the rule that replaces OFF by 0. Can’t see any error being logged anywhere.

btw should there really be === in between Multi100 and OFF?

bummer :disappointed_relieved:

@rlkoshak,

you were right and I should upgrade my coding skills :smiley:. 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.

Item declaration:

Dimmer sonoff_2A16BC "LedStrip[%d %%]" <slider> (gLights_VH_Studeerkamer)
	{mqtt=">[mosquitto:home/esp8266/tasmota/sonoff_2A16BC/cmnd/Pwm1:command:*:JS(tasmota_multiply.js)],
	<[mosquitto:home/esp8266/tasmota/sonoff_2A16BC/RESULT:state:JS(tasmota_divide.js)]"
	}

tasmota_multiply:

(function(tasmota_multiply) {
  if(tasmota_multiply === "OFF") return 0;
  var percent = tasmota_multiply / 100.0;
  var value = 1023 * percent;
  return value.toFixed(0);
})(input)

tasmota_divide:

(function(json){
var pwmvalues = JSON.parse(json);
var PWM1 = pwmvalues.PWM.PWM1/10.23;
return PWM1.toFixed(0)
})(input)

Link to my hardware

1 Like