Joshua
Junior Member
Posts: 75
|
Post by Joshua on Mar 3, 2019 2:08:19 GMT
Disclosure... I'm posting this in the openHAB forums here
How can I convert the dimmer value from a decimal to an integer in paper UI or items? The image attached shows the paper white configuration. The Dimmer\:%s between 0 and 100 is required to trigger the dimmer with the MQTT ESP module I’m using.
Here's a working MQTT 1 item
//ESP Dimmer 3 Switch Switch ESP03Switch "Hall Light" <light> (All,Random_Switches) {mqtt=">[mosquitto:/esp/dimmer03/sub:command:on:R13_ON],>[mosquitto:/esp/dimmer03/sub:command:off:R13_OFF]"} //ESP Dimmer 3 Slider Dimmer ESP03Dimmer "Hall Dimmer [%d %%]" <slider> (All,Quick_Access,Random_Dimmers,Morning,BedTime) {mqtt=">[mosquitto:/esp/dimmer03/sub:command:*:Dimmer\\:${command}]"}
Here's what I have in Paper UI...
and the payload is
Dimmer\\:0.82000000
The ESP needs a payload of
Dimmer\\:82
|
|
|
Post by papa on Mar 3, 2019 2:54:53 GMT
Joshua, I believe something like the following might be a start for what you ask. I believe you need to multiply the slider's value times 100 to get an integer.
One way would be to change the dimming device's software to not only receive the slider value, but also to multiply that value times 100 before it is applied to dimming.
Another way (no changing the device software) ... I am thinking you could use THREE items, including the same Switch item plus splitting the one item for dimming into two items. One dimming item displays a slider on the UI & tracks the slider's value. The other item will get the slider value which was converted to an integer. The rule below will send that converted value to the dimming device. The Switch item & the ESP03Slider item will have entries in the sitemap. The Number Dimmer3Cmd item (whose only purpose is to hold the integer which is sent to the dimming device) need NOT have a sitemap entry. //ESP Dimmer 3 Switch Switch ESP03Switch "Hall Light" <light> (All,Random_Switches) {mqtt=">[mosquitto:/esp/dimmer03/sub:command:on:R13_ON],>[mosquitto:/esp/dimmer03/sub:command:off:R13_OFF]"} //ESP Dimmer 3 Slider for the UI, yields a decimal Dimmer ESP03Slider "Hall Dimmer [%d %%]" <slider> (All,Quick_Access,Random_Dimmers,Morning,BedTime) //ESP Dimmer 3 Command, needs an integer for sending to the actual dimming device Number Dimmer3Cmd {mqtt=">[mosquitto:/esp/dimmer03/sub:command:*:Dimmer\\:${command}]"}
// When the UI slider & its value change, this rule converts the value to an integer & sends it to the actual dimming device.
rule Dimmer3Correct when Item ESP03Slider changed // slider on UI changed then var Number prcnt = ESP03Slider.state // value from UI slider var Number forESP03 = prcnt * 100 // convert value to integer Dimmer3Cmd.sendCommand( forESP03 ) // send converted value to the ESP03 Dimmer logInfo("ESP03", "****Slider Percent = " + prcnt) // debugging info for the log logInfo("ESP03", "****forESP03 = " + forESP03) end
|
|
Joshua
Junior Member
Posts: 75
|
Post by Joshua on Mar 3, 2019 17:16:03 GMT
Got it!... Thanks for the direction. I'll update the build post to include the result.
ITEMS
//ESP Dimmer 1 Switch Switch ESP01Switch "Josh's Light" <light> (All,Random_Switches) { channel="mqtt:topic:2c214008:ESP01Switch" }
//ESP Dimmer 1 Dimmer Dimmer ESP01DimmerProx "Josh's Dimmer" <slider> (All,Quick_Access,Random_Dimmers,Morning,BedTime) Number ESP01Dimmer "Josh's Dimmer" { channel="mqtt:topic:2c214008:ESP01Dimmer" }
RULES
rule "Set ESP Dimmer to 99 from 100" when Item ESP01Dimmer changed to 100 then sendCommand(ESP01Slider, 99) end
// When the UI slider & its value change, this rule converts the value to an integer & sends it to the actual dimming device. rule "Dimmer1Correct" when Item ESP01DimmerProx changed // slider on UI changed then var Number prcnt = ESP01DimmerProx.state // value from UI slider var Number forESP01 = prcnt // convert value to integer ESP01Dimmer.sendCommand( forESP01 ) // send converted value to the ESP01 Dimmer //logInfo("ESP01", "****Slider Percent = " + prcnt) // debugging info for the log //logInfo("ESP01", "****forESP03 = " + forESP01) end
|
|
|
Post by papa on Mar 3, 2019 19:40:24 GMT
You are welcome, Joshua. I'm glad if I helped. Some questions... In the last, largest rule you posted above: var Number prcnt = ESP01DimmerProx.state // value from UI slider var Number forESP01 = prcnt // convert value to integer ESP01Dimmer.sendCommand( forESP01 ) // send converted value to the ESP01 Dimmer Would that not mean that the value from the UI slider would be sent to dimmer WITH NO CHANGE? Do you not need to multiply prcnt by 100 as I did in this line?
var Number forESP01 = prcnt * 100 // convert value to integer
|
|
Joshua
Junior Member
Posts: 75
|
Post by Joshua on Mar 3, 2019 22:36:16 GMT
There's some voodoo doing on down stream. I added a channel for the proxy Dimmer for you to see the MQTT payload. I worked it out by trial and error. Different types of channel combinations with different types of things. I believe the difference is worked out by one item being a dimmer and the other a number.
//ESP Dimmer 1 Dimmer Dimmer ESP01DimmerProx "Josh's Dimmer" <slider> (All,Quick_Access,Random_Dimmers,Morning,BedTime) { channel="mqtt:topic:2c214008:ESP01DimmerProx" } Number ESP01Dimmer "Josh's Dimmer" { channel="mqtt:topic:2c214008:ESP01Dimmer" }
The top payload is from ESP01DimmerProx and the bottom is ESP01Dimmer
|
|
|
Post by papa on Mar 3, 2019 23:32:39 GMT
OK so the Dimmer item sends a percentage/decimal and ( via the rule) the Number item sends an integer. Interesting.
/ESP Dimmer 1 Dimmer Dimmer ESP01DimmerProx "Josh's Dimmer" <slider> (All,Quick_Access,Random_Dimmers,Morning,BedTime) { channel="mqtt:topic:2c214008:ESP01DimmerProx" } Number ESP01Dimmer "Josh's Dimmer" { channel="mqtt:topic:2c214008:ESP01Dimmer" }
So the longer rule above does not need to multiply prcnt times 100 to get an integer. That may mean the rule could be shortened some:// When the UI slider & its value change, this rule makes sure an integer is sent to the actual dimming device. rule "Dimmer1Correct" when Item ESP01Dimmer Prox changed // slider on UI changed state then var Number forESP01 prcnt = ESP01DimmerProx.state // value from UI slider, ?? already an integer ?? // var Number forESP01 = prcnt // convert value to integer << ?? disable/delete this line ?? ESP01Dimmer.sendCommand( forESP01 ) // send integer value to the ESP01 Dimmer //logInfo("ESP01", "****Slider Percent = " + prcnt) // debugging info for the log << ?? delete this line ?? //logInfo("ESP01", "****forESP03 = " + forESP01) end
|
|
Joshua
Junior Member
Posts: 75
|
Post by Joshua on Mar 4, 2019 1:02:09 GMT
Here are the leaner rules. I had to update the "Set ESP Dimmer to 99 from 100" to include the new items.
rule "Set ESP Dimmer to 99 from 100" when Item ESP01DimmerProx changed to 100 then sendCommand(ESP01DimmerProx, 99) end
rule "Dimmer1Correct" when Item ESP01DimmerProx changed // slider on UI changed state then var Number forESP01 = ESP01DimmerProx.state // value from UI slider, ?? already an integer ?? ESP01Dimmer.sendCommand( forESP01 ) // send integer value to the ESP01 Dimmer end
|
|
|
Post by papa on Mar 4, 2019 1:31:51 GMT
Leaner rules are progressing, Joshua. How about one rule?
rule "Dimmer1Correct" when Item ESP01DimmerProx changed // slider on UI changed state then var Number forESP01 = ESP01DimmerProx.state // value from UI slider, already an integer
if ( forESP01 == 100 ) forESP01 = 99 // catch & correct out-of-bounds value
ESP01Dimmer.sendCommand( forESP01 ) // send integer value to the ESP01 Dimmer end
|
|
Joshua
Junior Member
Posts: 75
|
Post by Joshua on Mar 4, 2019 2:09:48 GMT
The new rule works.. Thank you for taking your time to help out.
papa: You are welcome, Joshua. So much enjoyment to work it out together.
|
|