Author Topic: How to combine a single hex value from several individual values?  (Read 537 times)

Offline marti

  • Newbie
  • *
  • Posts: 12
  • Karma: +0/-0
Hello,

I'm creating my first Luup script for Vera.
It will be a heating control script - I'll connect four 24V heating actuators to a single Qubino RGBW module (could also be done with single or double Z-Wave relays, but there you get to control 1 or 2 circuits per 50 dollar module, whereas using RGBW modules you can control 4 circuits per 50 dollar module - makes quite a difference in a house with, say, 12 heating zones. :-)

The script should compose a RGBW value of four different values (either 00 or FF) which correspond to four different rooms.
This RGBW value will then be sent to "RGB Controller" virtual device as "newColorTargetValue = "#rrggbbww".
I could use something like "local RGBWvalue = "#"..RR .. GG .. BB .. WW" where RR to WW are previously defined based on room temperature readings.
But as I understand, this leaves spaces between the individual string components.

Can you suggest a better solution to solve this?

P.S. Of course I could always do a long line of "IF... AND... AND... AND... THEN" expressions but this would be a kilometer long, as four rooms, each On or Off , give I think 24 individual combinations :-)
EDIT: It should be 16 individual combinations. If I don't find a more elegant alternative I might need to do it this way.
« Last Edit: April 03, 2017, 04:55:30 pm by marti »

Offline jswim788

  • Hero Member
  • *****
  • Posts: 726
  • Karma: +48/-2
Re: How to combine a single hex value from several individual values?
« Reply #1 on: April 03, 2017, 05:09:34 pm »
Just tried this test in the AltUI test code window:

Code: [Select]
local RR = "01"
local GG = "23"
local BB = "45"
local WW = "67"
local RGBWvalue = "#"..RR .. GG .. BB .. WW
print ("RGBWvalue is " .. RGBWvalue)
return true
Output is:
Quote
RGBWvalue is #01234567
I don't see any spaces?

Offline akbooer

  • Master Member
  • *******
  • Posts: 5869
  • Karma: +249/-69
  • "Less is more"
Re: How to combine a single hex value from several individual values?
« Reply #2 on: April 03, 2017, 06:33:27 pm »
The other way, of course, is to use the built-in formatter which will do the decimal to hex conversion for you...

Code: [Select]
hex = "#%02x%02x%02x%02x"
print (hex: format(1,7,12,15))

giving

Code: [Select]
#01070c0f
3x Vera Lite-UI5/Edge-UI7, 25x Fibaro, 23x TKB, 9x MiniMote, 2x NorthQ Power, 2x Netatmo, 1x Foscam FI9831P, 9x Philips Hue,
Razberry, MySensors Arduino, HomeWave, AltUI, AltHue, DataYours, Grafana, openLuup, ZWay, ZeroBrane Studio.

Offline marti

  • Newbie
  • *
  • Posts: 12
  • Karma: +0/-0
Re: How to combine a single hex value from several individual values?
« Reply #3 on: April 04, 2017, 04:13:58 am »
Thank you both! I'll try either of these options.
I read the Lua Wiki about string coercion from multiple parts, but got an impression that it _always_ leaves spaces between them. So didn't even try it (testing a code to find out what _exactly_ is wrong is kind of awkward in Vera -  for a newbie, at least). Probably still need to install Alt UI or find some other way for testing.
 

Offline akbooer

  • Master Member
  • *******
  • Posts: 5869
  • Karma: +249/-69
  • "Less is more"
Re: How to combine a single hex value from several individual values?
« Reply #4 on: April 04, 2017, 05:08:42 am »
Try AltUI, you will not regret it.
3x Vera Lite-UI5/Edge-UI7, 25x Fibaro, 23x TKB, 9x MiniMote, 2x NorthQ Power, 2x Netatmo, 1x Foscam FI9831P, 9x Philips Hue,
Razberry, MySensors Arduino, HomeWave, AltUI, AltHue, DataYours, Grafana, openLuup, ZWay, ZeroBrane Studio.