LightWaveRF Switch and Link Hub Integration

In this subforum you can show projects you have made, or you are busy with. Please create your own topic.
Post Reply
User avatar
lonebaggie
Posts: 96
Joined: Tuesday 31 January 2017 14:21
Target OS: Raspberry Pi
Domoticz version: beta
Location: England
Contact:

LightWaveRF Switch and Link Hub Integration

Post by lonebaggie » Saturday 11 March 2017 15:51

I have collected my various lightwaveRF scripts into one universal script. This script will register the Domoticz Server as a lightwave link client. It does not require RFLink or any other third party 433 wireless hardware. It works natively with the LightwaveRF Link hub.
It can currently control lights, dimmers and switches (these are all the devices I have). Parameters are handled via a single user variable.

The script requires luasockets to work correctly (removed this requirement in 1.4).

EDIT Updated to 2.1 now supports all Lightwave power and lighting API see https://api.lightwaverf.com/lighting_power.html

Quick Steps

1 Create Dummy Hardware
2 Create as many virtual switches as required
3 Note IDx numbers of switches
4 Create blank variable string LW-Setting
5 Create a blank Lua device event and copy script into event
6 Edit LW-Settings to match virtual switches

All documentation in script header

Please use the latest script currently version 2.0

Code: Select all

-- This program control LightWaveRF switches, dimmers and sockets via the LIghtWaveRF link
-- The program requires lua sockets to be installed
--
-- This program requires  dummy hardware and virtual switches to be created.  
-- On/off and dimmer virtual switches are supported. 
-- No configuration of the virtual switch is required.
--
-- The program relies on a user string variable to be created in Domoticz interface with the name LW-Setting 
-- Use the following syntax for the string value
--
--                ipaddress|IDx|RnDn|IDx|RnDn
--
-- Where ipaddress  is IP of Wifi link
--       IDx        is the IDx number of virtual switch (obtain the IDx value from the Device list)
--       RnDn       is R=room n=Number D=Device n=Number (example R1D2) 
--                     
--       Room numbers and device numbers  are determined by the LIghtWaveRF app
--       Room 1 is the first room displayed in the app. Devices follow the same pattern 
--       Repeat sequence  for the number of switches, and dimmers required.
--
-- Example configuration 192.168.1.20|67|R1D1|68|R1D2|69|R2D1
--
-- LightwaveRF LInk has a built in security measure and will not allow any device to control lights and switches
-- without being registered to the LIghtWaveRF Link first.
--
-- If the Domoticz Server has not been registered to the LIghtWAveRF link this script will not work until registered !
-- To register you will need to setup a temporary switch  to be used just once as a mechanism to register the Domoticz Server.
-- Once the virtual switch (a simple on/off) has been created. Use the following string variable in LW-Settings
--
--                 ipaddress|IDx|Reg 
--
-- Once the virtual switch has been switched on or Off, the LIghtWAveRF Link should start flashing. 
-- Press the physical button on the Link box, This will register the Domoticz Server. 
-- Once registered remove the Reg setting from LW-Setting and replace with RnDn settings.
--
-- To de-register the Domoticz Server  run the above procedure but substitute Reg for UReg. 
-- Once de-registered no further commands will work until the Server is re-registered.

-- Version 1.0

-- Setup Variables
commandArray = {}
Vars = {}
Vars[1] = "Error"
LWCmd = "001,!"
On="F1"
Off="F0"
Dim="FdP"
id =0

-- functions

function split(pString, pPattern)
    local Table = {}  
    local fpat = "(.-)" .. pPattern
    local last_end = 1
    local s, e, cap = pString:find(fpat, 1)
    while s do
        if s ~= 1 or cap ~= "" then
            table.insert(Table,cap)
        end
        last_end = e+1
        s, e, cap = pString:find(fpat, last_end)
    end
    if last_end <= #pString then
        cap = pString:sub(last_end)
        table.insert(Table, cap)
   end
   return Table
end

function UDPSend(ip, message)
    local port = 9760
    local socket = require "socket"
    sock, msg = socket.udp()
    if sock then
        sock:setpeername(ip, port)
        sock:send(message)
        sock: close()
    end
    return
end

function round(n)
    return math.floor((math.floor(n*2) + 1)/2)
end

-- Obtain all Vars from uservariables
for i,v in pairs(uservariables) do
    if i== "LW-Setting" then
        Vars=split(v, "|")
    end
end
if Vars[1]== "Error" then
    print ("No LW-Setting Exit script")
    return commandArray
end

IPaddress = Vars[1]

if Vars[3] == "Reg" then
    On = " "
    Off = " "
    Vars[3] = "F*p"
    print("Register Device triggered")
end
if Vars[3] == "UReg" then
    On = " "
    Off = " "
    print("Un-register device triggered")
    Vars[3] = "F*xP"
end


print ("LightWaveRF started")
-- loop through all the devices
for DomDevice,DeviceValue in pairs(devicechanged) do
    id = tostring(otherdevices_idx[DomDevice])
    for i=2,#Vars,2 do
        if (Vars[i] == id) then
            if(DeviceValue == 'Off' ) then
                LWCmd =(LWCmd .. Vars[i+1] .. Off)
                UDPSend(IPaddress,LWCmd)
                print("Off " .. DomDevice)
            elseif(DeviceValue == 'On') then
                LWCmd =(LWCmd .. Vars[i+1] .. On)
                UDPSend(IPaddress,LWCmd)
                print("On  "  .. DomDevice)
            else
                DomValue = otherdevices_svalues[DomDevice]
                DomValue = (DomValue/3.14) +0.5
                DomValue = round(DomValue)   
                LWCmd =(LWCmd .. Vars[i+1] .. Dim .. DomValue)
                UDPSend(IPaddress,LWCmd)
                print("Dim " .. DomValue .. " " .. DomDevice) 
            end
            
        end
        
    end
end
return commandArray
Last edited by lonebaggie on Saturday 12 August 2017 17:56, edited 13 times in total.

User avatar
lonebaggie
Posts: 96
Joined: Tuesday 31 January 2017 14:21
Target OS: Raspberry Pi
Domoticz version: beta
Location: England
Contact:

Re: LightWaveRF Switch and Link Integration

Post by lonebaggie » Sunday 12 March 2017 11:10

Updated 1.2 . See Script

Code: Select all

-- This program control LightWaveRF switches, dimmers and sockets via the LIghtWaveRF link
-- The program requires lua sockets to be installed
--
-- This program requires  dummy hardware and virtual switches to be created.  
-- On/off and dimmer virtual switches are supported. 
-- No configuration of the virtual switch is required.
--
-- The program relies on a user string variable to be created in Domoticz interface with the name WL-Setting 
-- Use the following syntax for the string value
--
--                ipaddress|IDx|RnDn|IDx|RnDn
--
-- Where ipaddress  is IP of Wifi link
--       IDx        is the IDx number of virtual switch (obtain the IDx value from the Device list)
--       RnDn       is R=room n=Number D=Device n=Number (example R1D2) 
--                     
--       Room numbers and device numbers  are determined by the LIghtWaveRF app
--       Room 1 is the first room displayed in the app. Devices follow the same pattern 
--       Repeat sequence  for the number of switches, and dimmers required.
--
-- Example configuration 192.168.1.20|67|R1D1|68|R1D2|69|R2D1
--
-- LightwaveRF LInk has a built in security measure and will not allow any device to control lights and switches
-- without beging registed to the LIghtWaveRF Link first.
--
-- If the Domoticz Server has not been registered to the LIghtWAveRF link this script will not work until registered !
-- To register you will need to setup a temporary switch  to be used just once as a mechanism to register the Domoticz Server.
-- Once the virtual switch (a simple on/off) has been created. Use the following string varable in LW-Settings
--
--                 ipaddress|IDx|Reg 
--
-- Once the virtual switch has been switched on or Off, the LIghtWAveRF Link should start flashing. 
-- Press the physical button on the Link box, This will register the Domiticz Server. 
-- Once registeed remove the Reg setting from LW-Setting and replace with RnDn settings.
--
-- To de-register the Domiticz Server  run the above procedure but subsitute Reg for UReg. 
-- Once de-registered no further commands will work until the Server is re-registered.

-- Version 1.2
-- Added error checking on LW-Setting
-- Exit script when switch found and actioned. stop loop quicker

-- Setup Variables
commandArray = {}
Vars = {}
Vars[1] = "Error"
LWCmd = "001,!"
On="F1"
Off="F0"
Dim="FdP"
id =0

-- functions

function split(pString, pPattern)
    local Table = {}  
    local fpat = "(.-)" .. pPattern
    local last_end = 1
    local s, e, cap = pString:find(fpat, 1)
    while s do
        if s ~= 1 or cap ~= "" then
            table.insert(Table,cap)
        end
        last_end = e+1
        s, e, cap = pString:find(fpat, last_end)
    end
    if last_end <= #pString then
        cap = pString:sub(last_end)
        table.insert(Table, cap)
   end
   return Table
end

function UDPSend(ip, message)
    local port = 9760
    local socket = require "socket"
    sock, msg = socket.udp()
    if sock then
        sock:setpeername(ip, port)
        sock:send(message)
        sock: close()
    end
    return
end

function round(n)
    return math.floor((math.floor(n*2) + 1)/2)
end

-- Obtain all Vars from uservariables
for i,v in pairs(uservariables) do
    if i== "LW-Setting" then
        Vars=split(v, "|")
    end
end
if Vars[1]== "Error" then
    print ("No LW-Setting Exit script")
    return commandArray
end
if #Vars < 3 then
    print ("LW-Setting invalid EXit script")
    return commandArray
end
IPaddress = Vars[1]

if Vars[3] == "Reg" then
    On = " "
    Off = " "
    Vars[3] = "F*p"
    print("Register Device triggered")
end
if Vars[3] == "UReg" then
    On = " "
    Off = " "
    print("Un-register device triggered")
    Vars[3] = "F*xP"
end


print ("LightWaveRF started")
-- loop through all the devices
for DomDevice,DeviceValue in pairs(devicechanged) do
    id = tostring(otherdevices_idx[DomDevice])
    for i=2,#Vars,2 do
        if (Vars[i] == id) then
            if(DeviceValue == 'Off' ) then
                LWCmd =(LWCmd .. Vars[i+1] .. Off)
                UDPSend(IPaddress,LWCmd)
                print("Off " .. DomDevice)
            elseif(DeviceValue == 'On') then
                LWCmd =(LWCmd .. Vars[i+1] .. On)
                UDPSend(IPaddress,LWCmd)
                print("On  "  .. DomDevice)
            else
                DomValue = otherdevices_svalues[DomDevice]
                DomValue = (DomValue/3.14) +0.5
                DomValue = round(DomValue)   
                LWCmd =(LWCmd .. Vars[i+1] .. Dim .. DomValue)
                UDPSend(IPaddress,LWCmd)
                print("Dim " .. DomValue .. " " .. DomDevice) 
            end
            return commandArray
        end
        
    end
end
return commandArray
Last edited by lonebaggie on Friday 07 July 2017 14:12, edited 1 time in total.

User avatar
lonebaggie
Posts: 96
Joined: Tuesday 31 January 2017 14:21
Target OS: Raspberry Pi
Domoticz version: beta
Location: England
Contact:

Re: LightWaveRF Switch and Link Integration

Post by lonebaggie » Wednesday 12 April 2017 23:24

Updated to 1.4 to remove the requirement on Sockets. Will now run unmodified on Linux environment, uses Netcat to send UDP to wifi Link. Will require modification to run in windows environment see https://github.com/diegocr/netcat

Code: Select all

-- This program control LightWaveRF switches, dimmers and sockets via the LIghtWaveRF link
-- 
--
-- This program requires  dummy hardware and virtual switches to be created.  
-- On/off and dimmer virtual switches are supported. 
-- No configuration of the virtual switch is required.
--
-- The program relies on a user string variable to be created in Domoticz interface with the name WL-Setting 
-- Use the following syntax for the string value
--
--                ipaddress|IDx|RnDn|IDx|RnDn
--
-- Where ipaddress  is IP of Wifi link
--       IDx        is the IDx number of virtual switch (obtain the IDx value from the Device list)
--       RnDn       is R=room n=Number D=Device n=Number (example R1D2) 
--                     
--       Room numbers and device numbers  are determined by the LIghtWaveRF app
--       Room 1 is the first room displayed in the app. Devices follow the same pattern 
--       Repeat sequence  for the number of switches, and dimmers required.
--
-- Example configuration 192.168.1.20|67|R1D1|68|R1D2|69|R2D1
--
-- LightwaveRF LInk has a built in security measure and will not allow any device to control lights and switches
-- without beging registed to the LIghtWaveRF Link first.
--
-- If the Domoticz Server has not been registered to the LIghtWAveRF link this script will not work until registered !
-- To register you will need to setup a temporary switch  to be used just once as a mechanism to register the Domoticz Server.
-- Once the virtual switch (a simple on/off) has been created. Use the following string varable in LW-Settings
--
--                 ipaddress|IDx|Reg 
--
-- Once the virtual switch has been switched on or Off, the LIghtWAveRF Link should start flashing. 
-- Press the physical button on the Link box, This will register the Domiticz Server. 
-- Once registeed remove the Reg setting from LW-Setting and replace with RnDn settings.
--
-- To de-register the Domiticz Server  run the above procedure but subsitute Reg for UReg. 
-- Once de-registered no further commands will work until the Server is re-registered.

-- Version 1.2
-- Added error checking on LW-Setting
-- Exit script when switch found and actioned. stop loop quicker

-- Version 1.3
-- Simplified picking up user Variables

-- Version 1.4
-- Removed the use of Lua sockets. Now uses netcat. syntax will need to changed to work on Windows machines


-- Setup Variables
commandArray = {}
Vars = {}
Vars[1] = "Error"
LWCmd = "001,\\!"
On="F1"
Off="F0"
Dim="FdP"
id =0

-- functions

function split(pString, pPattern)
    local Table = {}  
    local fpat = "(.-)" .. pPattern
    local last_end = 1
    local s, e, cap = pString:find(fpat, 1)
    while s do
        if s ~= 1 or cap ~= "" then
            table.insert(Table,cap)
        end
        last_end = e+1
        s, e, cap = pString:find(fpat, last_end)
    end
    if last_end <= #pString then
        cap = pString:sub(last_end)
        table.insert(Table, cap)
   end
   return Table
end
-- UDPSend changed to use netcat please change format in udpcall to work on windows machines
function UDPSend(ip, message)
    local port = 9760
    udpcall = "echo -n " .. message .. "| nc -4u -w1 " .. ip .. " " .. port .. " &"
    os.execute(udpcall)  
    return
end

function round(n)
    return math.floor((math.floor(n*2) + 1)/2)
end

-- Obtain all lightwave config  from uservariables

Vars=split(uservariables["LW-Setting"], "|")
if Vars[1]== "Error" then
    print ("No LW-Setting Exit script")
    return commandArray
end
if #Vars < 3 then
    print ("LW-Setting invalid EXit script")
    return commandArray
end
IPaddress = Vars[1]

if Vars[3] == "Reg" then
    On = " "
    Off = " "
    Vars[3] = "F*p"
    print("Register Device triggered")
end
if Vars[3] == "UReg" then
    On = " "
    Off = " "
    print("Un-register device triggered")
    Vars[3] = "F*xP"
end


print ("LightWaveRF started")
-- loop through all the devices
for DomDevice,DeviceValue in pairs(devicechanged) do
    id = tostring(otherdevices_idx[DomDevice])
    for i=2,#Vars,2 do
        if (Vars[i] == id) then
            if(DeviceValue == 'Off' ) then
                LWCmd =(LWCmd .. Vars[i+1] .. Off)
                UDPSend(IPaddress,LWCmd)
                print("Off " .. DomDevice)
            elseif(DeviceValue == 'On') then
                LWCmd =(LWCmd .. Vars[i+1] .. On)
                UDPSend(IPaddress,LWCmd)
                print("On  "  .. DomDevice)
            else
                DomValue = otherdevices_svalues[DomDevice]
                DomValue = (DomValue/3.14) +0.5
                DomValue = round(DomValue)   
                LWCmd =(LWCmd .. Vars[i+1] .. Dim .. DomValue)
                UDPSend(IPaddress,LWCmd)
                print("Dim " .. DomValue .. " " .. DomDevice) 
            end
            return commandArray
        end
        
    end
end
return commandArray
Last edited by lonebaggie on Friday 07 July 2017 14:13, edited 1 time in total.

User avatar
lonebaggie
Posts: 96
Joined: Tuesday 31 January 2017 14:21
Target OS: Raspberry Pi
Domoticz version: beta
Location: England
Contact:

Re: LightWaveRF Switch and Link Hub Integration

Post by lonebaggie » Thursday 15 June 2017 21:57

Updated again 1.5. Now supports Moods and All Off Room functions of the LightwaveRF Link.

Moods require a Virtual Selector switch created in the same order as the Moods in the LighrwaveRF App. Recommend Selector switch names match Mood names.

Use a Push on Button virtual switch to control the all off feature.

Have updated documentation in program.

Code: Select all

-- This program control LightWaveRF switches,dimmers,sockets and Moods via the LIghtWaveRF link
-- 
--
-- This program requires  dummy hardware and virtual switches to be created.  
-- On/off,dimmer and Selector virtual switches are supported. 
-- No configuration of the virtual switch is required.
--
-- The program relies on a user string variable to be created in Domoticz interface with the name WL-Setting 
-- Use the following syntax for the string value
--
--                ipaddress|IDx|RnDn|IDx|RnDn
--
-- Where ipaddress  is IP of Wifi link
--       IDx        is the IDx number of virtual switch (obtain the IDx value from the Device list)
--       RnDn       is R=room n=Number D=Device n=Number (example R1D2) 
--       RnO        is R=room n=Number O= Turns all Devices in room off
--       RnM        is R=room n=Number M=Mood selected by Selector switch (1 to 5)
--  
--       For Moods the Selector Switch must match the Mood sequence from LightwaveRF App 
--       So Mood 1 is level 10 in the Selector Switch Mood 2 is level 20 , etc . 
--       The Selector must have Off disabled.
--
--       Suggest using a pUsh on button virtual switch for All Off (RnO) option. 
--
--       Room numbers and device numbers  are determined by the LIghtWaveRF app
--       Room 1 is the first room displayed in the app. Devices follow the same pattern 
--       Repeat sequence  for the number of switches,dimmers and Moods  required.
--
-- Example configuration 192.168.1.20|67|R1D1|68|R1D2|69|R2D1|34|R2M|54|R1O

-- This program will make no attempt to sync switch settings. So if you issue an all off command (RnO)
-- this will not switch off any individual Domoticz switches in the room. If this is required use the 
-- Sub/Slave Devices option  in the All off Virtual switch to turn off all Domoticz switches in the room

--                           **************************************************
--                           * P L E A S E  R E A D  T H E  F O L L O W I N G *
--                           **************************************************
--
-- LightwaveRF LInk has a built in security measure and will not allow any client devices to control lights and switches
-- without being registed to the LIghtWaveRF Link first.
--
-- If the Domoticz Server has not been registered to the LightWaveRF link this program  will not work until registered !
-- To register you will need to create a single virtual switch as a mechanism to register the Domoticz Server.
-- Once the virtual switch (a simple on/off) has been created. Use the following string varable in LW-Settings.
-- Do not use this setting in conjuction with other settings documented above.
--
--                 ipaddress|IDx|Reg 
--
-- Once the virtual switch has been switched on or Off, the LightWaveRF Link should start flashing. 
-- Press the physical button on the Link box, This will register the Domiticz Server. 
-- Once registeed remove the Reg setting from LW-Setting.
--
-- To de-register the Domiticz Server run the above procedure but subsitute Reg for UReg. 
-- Once de-registered no further commands will work until the Server is re-registered.

-- Version 1.2
-- Added error checking on LW-Setting
-- Exit script when switch found and actioned. stop loop quicker

-- Version 1.3
-- Simplified picking up user Variables

-- Version 1.4
-- Removed the use of Lua sockets. Now uses netcat. syntax will need to changed to work on Windows machines

-- Version 1.5

-- Added Lightwave Moods and all off for rooms. Updated instructions 

-- Setup Variables
commandArray = {}
Vars = {}
Vars[1] = "Error"
LWCmd = "001,\\!"
On="F1"
Off="F0"
Dim="FdP"
Aoff="Fa"
Mood="FmP"
id =0

-- functions

function split(pString, pPattern)
    local Table = {}  
    local fpat = "(.-)" .. pPattern
    local last_end = 1
    local s, e, cap = pString:find(fpat, 1)
    while s do
        if s ~= 1 or cap ~= "" then
            table.insert(Table,cap)
        end
        last_end = e+1
        s, e, cap = pString:find(fpat, last_end)
    end
    if last_end <= #pString then
        cap = pString:sub(last_end)
        table.insert(Table, cap)
   end
   return Table
end
-- UDPSend changed to use netcat please change format in udpcall to work on windows machines
function UDPSend(ip, message)
    local port = 9760
    udpcall = "echo -n " .. message .. "| nc -4u -w1 " .. ip .. " " .. port .. " &"
    os.execute(udpcall)  
    return
end

function round(n)
    return math.floor((math.floor(n*2) + 1)/2)
end

-- Obtain all lightwave config  from uservariables

Vars=split(uservariables["LW-Setting"], "|")
if Vars[1]== "Error" then
    print ("No LW-Setting Exit script")
    return commandArray
end
if #Vars < 3 then
    print ("LW-Setting invalid EXit script")
    return commandArray
end
IPaddress = Vars[1]

if Vars[3] == "Reg" then
    On = " "
    Off = " "
    Vars[3] = "F*p"
    print("Register Device triggered")
end
if Vars[3] == "UReg" then
    On = " "
    Off = " "
    print("Un-register device triggered")
    Vars[3] = "F*xP"
end


print ("LightWaveRF started")
-- loop through all the devices
for DomDevice,DeviceValue in pairs(devicechanged) do
    id = tostring(otherdevices_idx[DomDevice])
    for i=2,#Vars,2 do
        if (Vars[i] == id) then
            if(DeviceValue == 'Off' and string.sub(Vars[i+1],3)~="O") then
                LWCmd =(LWCmd .. Vars[i+1] .. Off)
                UDPSend(IPaddress,LWCmd)
                print("Off " .. DomDevice)
            elseif(DeviceValue == 'On' and string.sub(Vars[i+1],3)~="O") then
                LWCmd =(LWCmd .. Vars[i+1] .. On)
                UDPSend(IPaddress,LWCmd)
                print("On  "  .. DomDevice)
            elseif (string.sub(Vars[i+1],3) =="O" ) then
                LWCmd =(LWCmd .. string.sub(Vars[i+1],1.2)  .. Aoff)
                UDPSend(IPaddress,LWCmd)
                print("All Off " .. DomDevice)
            elseif (string.sub(Vars[i+1],3)=="M") then
                DomValue = otherdevices_svalues[DomDevice]
                DomValue = DomValue /10
                LWCmd =(LWCmd .. string.sub(Vars[i+1],1.2) .. Mood .. DomValue )
                UDPSend(IPaddress,LWCmd)
                print (LWCmd)
                print("Mood " .. DomDevice .. " " .. DeviceValue)
            elseif (string.gmatch(DeviceValue,"%") and string.sub(Vars[i+1],3)~="M") then  
                DomValue = otherdevices_svalues[DomDevice]
                DomValue = (DomValue/3.14) +0.5
                DomValue = round(DomValue)   
                LWCmd =(LWCmd .. Vars[i+1] .. Dim .. DomValue)
                UDPSend(IPaddress,LWCmd)
                print("Dim " .. DomValue .. " " .. DomDevice) 
            end
            return commandArray
        end
        
    end
end
return commandArray

User avatar
lonebaggie
Posts: 96
Joined: Tuesday 31 January 2017 14:21
Target OS: Raspberry Pi
Domoticz version: beta
Location: England
Contact:

Re: LightWaveRF Switch and Link Hub Integration

Post by lonebaggie » Sunday 18 June 2017 16:17

Major update 2.0

now supports all options in lightwareRF power and lighting api https://api.lightwaverf.com/lighting_power.html

To support new features LW-Setting needs to be amended from 192.168.1.1|67|R1D1 to 192.168.1.1|67|R1|D1

See script header for full details

Code: Select all

-- This program control LightWaveRF switches,dimmers,sockets and Moods via the LIghtWaveRF link
-- 
--
-- This program requires  dummy hardware and virtual switches to be created.  
-- On/off,dimmer and Selector virtual switches are supported. 
-- 
--
-- The program relies on a user string variable to be created in Domoticz interface with the name WL-Setting 
-- Use the following syntax for the string value
--
--                ipaddress|IDx|Rn|Commands|Idx|Rn|Commands etc
--
-- Where ipaddress  is IP of Wifi link
--       IDx        is the IDx number of virtual switch (obtain the IDx value from the Device list)
--       Rn         is R=room n=Number
--
--       Commands 
--
--       Dn         is D=Device to be off/on or dimmed    
--       Ln         is L=Device to be unlocked/locked. 
--       Cn         is C=Device to be closed/opened/stopped
--       O          is O=Turns all Devices in room off
--       M          is M=Mood selected by Selector switch (1 to 5)
--  
--       For Moods a Selector Switch must be used and must match the Mood sequence from LightwaveRF App 
--       So Mood 1 is level 10 in the Selector Switch Mood 2 is level 20 , etc . 
--       The Selector must have Off disabled.
--
--       For Closed/open/stopped make sure switch is set to a venetian blinds
--
--       Suggest using a push on button virtual switch for All Off option. 
--
--       Room numbers (R) and D,L and C numbers  are determined by the LightWaveRF app
--       Room 1 is the first room displayed in the app. Devices (D,L and C) follow the same pattern 
--       Repeat sequence  for the number of domoticz switches required.
--
-- Example configuration 192.168.1.20|67|R1|D1|68|R1|C2|69|R2|L1|34|R2|M|54|R1|O

-- See https://api.lightwaverf.com/ for full details of API

-- This program will make no attempt to sync switch settings. So if you issue an all off command
-- this will not switch off any individual Domoticz switches in the room. If this is required use the 
-- Sub/Slave Devices option  in the All off Virtual switch to turn off all Domoticz switches in the room

--                           **************************************************
--                           * P L E A S E  R E A D  T H E  F O L L O W I N G *
--                           **************************************************
--
-- LightwaveRF LInk has a built in security measure and will not allow any client devices to control lights and switches
-- without being registed to the LIghtWaveRF Link first.
--
-- If the Domoticz Server has not been registered to the LightWaveRF link this program  will not work until registered !
-- To register you will need to create a single virtual switch as a mechanism to register the Domoticz Server.
-- Once the virtual switch (a simple on/off) has been created. Use the following string varable in LW-Settings.
-- Do not use this setting in conjuction with other settings documented above. Note the use of two ||
--
--                 ipaddress|IDx||Reg 
--
-- Once the virtual switch has been switched on or Off, the LightWaveRF Link should start flashing. 
-- Press the physical button on the Link box, This will register the Domiticz Server. 
-- Once registeed remove the Reg setting from LW-Setting.
--
-- To de-register the Domiticz Server run the above procedure but subsitute Reg for UReg. 
-- Once de-registered no further commands will work until the Server is re-registered.

-- Version 1.2
-- Added error checking on LW-Setting
-- Exit script when switch found and actioned. stop loop quicker

-- Version 1.3
-- Simplified picking up user Variables

-- Version 1.4
-- Removed the use of Lua sockets. Now uses netcat. syntax will need to changed to work on Windows machines

-- Version 1.5

-- Added Lightwave Moods and all off for rooms. Updated instructions 

-- Version 2.0
-- 
-- Added all options from LightWaveRF api for power and lighting . see https://api.lightwaverf.com/
-- Simplified logic. Added additinal variables  by spliting R1D1 into R1|D1. Allows simple parsing on LW_setting 
-- LW-Setting needs to be modified from previous versions or program wull not run correctly !!
--
-- Setup Variables
commandArray = {}
Vars = {}
Vars[1] = "Error"
-- LightwareRF Commands
LWCmd = "001,\\!"
On="F1"
Off="F0"
Dim="FdP"
Aoff="Fa"
Mood="FmP"
Reg= "F*p"
UReg="F*xP"
Lock="Fl"
Ulock="Fu"
Close="F)"
Open="F("
Stop="F^"

id =0

-- functions

function split(pString, pPattern)
    local Table = {}  
    local fpat = "(.-)" .. pPattern
    local last_end = 1
    local s, e, cap = pString:find(fpat, 1)
    while s do
        if s ~= 1 or cap ~= "" then
            table.insert(Table,cap)
        end
        last_end = e+1
        s, e, cap = pString:find(fpat, last_end)
    end
    if last_end <= #pString then
        cap = pString:sub(last_end)
        table.insert(Table, cap)
    end
   return Table
end
-- UDPSend changed to use netcat please change format in udpcall to work on windows machines
function UDPSend(ip, message)
    local port = 9760
    udpcall = "echo -n " .. message .. "| nc -4u -w1 " .. ip .. " " .. port .. " &"
    os.execute(udpcall)  
    return
end
-- Math rounding 
function round(n)
    return math.floor((math.floor(n*2) + 1)/2)
end

-- Obtain all lightwave config  from uservariables

Vars=split(uservariables["LW-Setting"], "|")

-- ensue LW-Settings conatin info
if Vars[1]== "Error" then
    print ("No LW-Setting Exit script")
    return commandArray
end
if #Vars < 3 then
    print ("LW-Setting invalid EXit script")
    return commandArray
end
-- set ip address of LightwaveRF link
IPaddress = Vars[1]


print ("LightWaveRF started")
-- loop through all the devices
for DomDevice,DeviceValue in pairs(devicechanged) do
-- get IDx number of device
    id = tostring(otherdevices_idx[DomDevice])
--loop through all variables parsed from LW-Setting 
    for i=2,#Vars,3 do
-- if device found action command
        if (Vars[i] == id) then
-- set Lrf to 1st char of command 
            Lrf = string.sub(Vars[i+2],1,1)
-- find command set in variable  and load LighwaveRF command into LWCmd
            if Lrf =="R" then
                LWCmd = LWCmd  .. Reg
                print ("Device Registered")
            end
            if Lrf == "U" then
                LWCmd = LWCmd .. UReg
                print ("Device Un-Registered")
            end
            if Lrf == "M" then
                DomValue = otherdevices_svalues[DomDevice]
                DomValue = DomValue /10
                LWCmd = LWCmd .. Vars[i+1] .. Mood .. DomValue
                print ("Mood " .. DomValue .. " on " .. DomDevice)
                
            end
            if Lrf == "O" then
                LWCmd = LWCmd .. Vars[i+1] .. Aoff
                print ("All devices  in " .. Vars[i+1] .. " off")
            end
            if Lrf == "D" then
                if DeviceValue == "Off" then
                    LWCmd = LWCmd .. Vars[i+1] .. Vars[i+2] .. Off
                    print ("Device " .. DomDevice .. " off")
                elseif DeviceValue == "On" then
                    LWCmd = LWCmd .. Vars[i+1] .. Vars[i+2] .. On
                    print ("Device " .. DomDevice .. " on")
                else
 -- convert 0 -100% Dim Values to Lightwaverf 1 - 32 dim values
                    DomValue = otherdevices_svalues[DomDevice]
                    DomValue = (DomValue/3.14) +0.5
                    DomValue = round(DomValue)
                    LWCmd = LWCmd .. Vars[i+1] .. Vars[i+2] .. Dim .. DomValue
                    print ("Device " .. DomDevice .. " dim " .. DeviceValue)
                end
            end
            if Lrf == "C" then
                Vars[i+2] = "D" .. string.sub(Vars[i+2],2)
                if DeviceValue == "Off" then
                    LWCmd = LWCmd .. Vars[i+1] .. Vars[i+2] .. Open
                    print ("Device " .. DomDevice .. " opened")
                elseif DeviceValue == "On" then
                    LWCmd = LWCmd .. Vars[i+1] .. Vars[i+2] .. Close
                    print ("Device " .. DomDevice .. " closed")
                else
                    LWCmd = LWCmd .. Vars[i+1] .. Vars[i+2] .. Stop
                    print ("Device " .. DomDevice .. " stopped")
                end
            end
            if Lrf == "L" then
                Vars[i+2] = "D" .. string.sub(Vars[i+2],2)
                if DeviceValue == "Off" then
                    LWCmd = LWCmd .. Vars[i+1] .. Vars[i+2] .. Ulock
                    print ("Device " .. DomDevice .. " unlocked")
                elseif DeviceValue == "On" then
                    LWCmd = LWCmd .. Vars[i+1] .. Vars[i+2] .. Lock
                    print ("Device " .. DomDevice .. " locked")
                end
        end
--send complete command to LightwaveRF Link
            UDPSend(IPaddress,LWCmd)
            print ( "Command sent to " .. IPaddress .. " is " .. LWCmd)
            return commandArray
        end
        
    end
end
return commandArray

User avatar
lonebaggie
Posts: 96
Joined: Tuesday 31 January 2017 14:21
Target OS: Raspberry Pi
Domoticz version: beta
Location: England
Contact:

Re: LightWaveRF Switch and Link Hub Integration

Post by lonebaggie » Saturday 12 August 2017 18:03

Updated to 2.1

Have synchronized Domoticz to match Lightwaverf all Off command in a room, This will ensure all switches in that room are marked off when all off command triggered by lightwaverf.

Also added a random command sequence to ensure commands are registered correctly by the lightwaverf hub.

All documentation in script

Code: Select all

-- This program control LightWaveRF switches,dimmers,sockets and Moods via the LIghtWaveRF link
-- 
--
-- This program requires  dummy hardware and virtual switches to be created.  
-- On/off,dimmer and Selector virtual switches are supported. 
-- No configuration of the virtual switch is required.
--
-- The program relies on a user string variable to be created in Domoticz interface with the name WL-Setting 
-- Use the following syntax for the string value
--
--                ipaddress|IDx|Rn|Commands|Idx|Rn|Commands etc
--
-- Where ipaddress  is IP of Wifi link
--       IDx        is the IDx number of virtual switch (obtain the IDx value from the Device list)
--       Rn         is R=room n=Number
--
--       Commands 
--
--       Dn         is D=Device to be off/on or dimmed    
--       Ln         is L=Device to be unlocked/locked. 
--       Cn         is C=Device to be closed/opened/stopped
--       O          is O=Turns all Devices in room off
--       M          is M=Mood selected by Selector switch (1 to 5)
--  
--       For Moods a Selector Switch must be used and must match the Mood sequence from LightwaveRF App 
--       So Mood 1 is level 10 in the Selector Switch Mood 2 is level 20 , etc . 
--       The Selector must have Off disabled.
--
--       For Closed/open/stopped make sure switch is set to a venetian blinds
--
--       Suggest using a push on button virtual switch for All Off option. 
--
--       Room numbers (R) and D,L and C numbers  are determined by the LightWaveRF app
--       Room 1 is the first room displayed in the app. Devices (D,L and C) follow the same pattern 
--       Repeat sequence  for the number of domoticz switches required.
--
-- Example configuration 192.168.1.20|67|R1|D1|68|R1|C2|69|R2|L1|34|R2|M|54|R1|O

-- See https://api.lightwaverf.com/ for full details of API

-- This program will sync switch settings when All off is issued . So if you issue an all off command
-- all switches in the room will be turned off
--
--                           **************************************************
--                           * P L E A S E  R E A D  T H E  F O L L O W I N G *
--                           **************************************************
--
-- LightwaveRF LInk has a built in security measure and will not allow any client devices to control lights and switches
-- without being registed to the LIghtWaveRF Link first.
--
-- If the Domoticz Server has not been registered to the LightWaveRF link this program  will not work until registered !
-- To register you will need to create a single virtual switch as a mechanism to register the Domoticz Server.
-- Once the virtual switch (a simple on/off) has been created. Use the following string varable in LW-Settings.
-- Do not use this setting in conjuction with other settings documented above.
--
--                 ipaddress|IDx||Reg 
--
-- Once the virtual switch has been switched on or Off, the LightWaveRF Link should start flashing. 
-- Press the physical button on the Link box, This will register the Domiticz Server. 
-- Once registeed remove the Reg setting from LW-Setting.
--
-- To de-register the Domiticz Server run the above procedure but subsitute Reg for UReg. 
-- Once de-registered no further commands will work until the Server is re-registered.

-- Version 1.2
-- Added error checking on LW-Setting
-- Exit script when switch found and actioned. stop loop quicker

-- Version 1.3
-- Simplified picking up user Variables

-- Version 1.4
-- Removed the use of Lua sockets. Now uses netcat. syntax will need to changed to work on Windows machines

-- Version 1.5

-- Added Lightwave Moods and all off for rooms. Updated instructions 

-- Version 2.0
-- 
-- Added all options from LightWaveRF api for power and lighting . see https://api.lightwaverf.com/
-- Simplified logic. Added additinal variables  by spliting R1D1 into R1|D1. Allows simple parsing on LW_setting 
-- LW-Setting needs to be modified from previous versions or program wull not run correctly !!
--
-- Version 2.1 
--
-- All Off now turn off Domoticz switches in the room without  the need for slave switches. 
-- Added random number to LightwareRf command sequence to ensure command is run if script run in Group
--
-- Setup Variables
commandArray = {}
Vars = {}
Vars[1] = "Error"
-- LightwareRF Commands
Rand=math.random(998)+1
LWCmd =  Rand .. ",\\!"
On="F1"
Off="F0"
Dim="FdP"
Aoff="Fa"
Mood="FmP"
Reg= "F*p"
UReg="F*xP"
Lock="Fl"
Ulock="Fu"
Close="F)"
Open="F("
Stop="F^"

id =0

-- functions

function split(pString, pPattern)
    local Table = {}  
    local fpat = "(.-)" .. pPattern
    local last_end = 1
    local s, e, cap = pString:find(fpat, 1)
    while s do
        if s ~= 1 or cap ~= "" then
            table.insert(Table,cap)
        end
        last_end = e+1
        s, e, cap = pString:find(fpat, last_end)
    end
    if last_end <= #pString then
        cap = pString:sub(last_end)
        table.insert(Table, cap)
    end
   return Table
end
-- UDPSend changed to use netcat please change format in udpcall to work on windows machines
function UDPSend(ip, message)
    local port = 9760
    udpcall = "echo -n " .. message .. "| nc -4u -w1 " .. ip .. " " .. port .. " &"
    os.execute(udpcall)  
    return
end
-- Math rounding 
function round(n)
    return math.floor((math.floor(n*2) + 1)/2)
end

-- Obtain all lightwave config  from uservariables
Vars=split(uservariables["LW-Setting"], "|")

-- ensue LW-Settings conatin info
if Vars[1]== "Error" then
    print ("No LW-Setting Exit script")
    return commandArray
end
if #Vars < 3 then
    print ("LW-Setting invalid EXit script")
    return commandArray
end
-- set ip address of LightwaveRF link
IPaddress = Vars[1]


print ("LightWaveRF started")
-- loop through all the devices
for DomDevice,DeviceValue in pairs(devicechanged) do
-- get IDx number of device
    id = tostring(otherdevices_idx[DomDevice])
--loop through all variables parsed from LW-Setting 
    for i=2,#Vars,3 do
-- if device found action command
        if (Vars[i] == id) then
-- set Lrf to 1st char of command 
            Lrf = string.sub(Vars[i+2],1,1)
-- find command set in variable  and load LighwaveRF command into LWCmd
            if Lrf =="R" then
                LWCmd = LWCmd  .. Reg
                print ("Device Registered")
            end
            if Lrf == "U" then
                LWCmd = LWCmd .. UReg
                print ("Device Un-Registered")
            end
            if Lrf == "M" then
                DomValue = otherdevices_svalues[DomDevice]
                DomValue = DomValue /10
                LWCmd = LWCmd .. Vars[i+1] .. Mood .. DomValue
                print ("Mood " .. DomValue .. " on " .. DomDevice)
                
            end
            if Lrf == "O" then
                LWCmd = LWCmd .. Vars[i+1] .. Aoff
                for r=3,#Vars,3 do
                    if Vars[i+1] == Vars[r] then
                        DomDevice = Vars[r-1] .. "|0|off"
                        commandArray[r-2]={["UpdateDevice"] = DomDevice}
                    end
                end
                print ("All devices  in " .. Vars[i+1] .. " off")
            end
            if Lrf == "D" then
                if DeviceValue == "Off" then
                    LWCmd = LWCmd .. Vars[i+1] .. Vars[i+2] .. Off
                    print ("Device " .. DomDevice .. " off")
                elseif DeviceValue == "On" then
                    LWCmd = LWCmd .. Vars[i+1] .. Vars[i+2] .. On
                    print ("Device " .. DomDevice .. " on")
                else
-- convert 0 -100% Dim Values to Lightwaverf 1 - 32 dim values
                    DomValue = otherdevices_svalues[DomDevice]
                    DomValue = (DomValue/3.14) +0.5
                    DomValue = round(DomValue)
                    LWCmd = LWCmd .. Vars[i+1] .. Vars[i+2] .. Dim .. DomValue
                    print ("Device " .. DomDevice .. " dim " .. DeviceValue)
                end
            end
            if Lrf == "C" then
                Vars[i+2] = "D" .. string.sub(Vars[i+2],2)
                if DeviceValue == "Off" then
                    LWCmd = LWCmd .. Vars[i+1] .. Vars[i+2] .. Open
                    print ("Device " .. DomDevice .. " opened")
                elseif DeviceValue == "On" then
                    LWCmd = LWCmd .. Vars[i+1] .. Vars[i+2] .. Close
                    print ("Device " .. DomDevice .. " closed")
                else
                    LWCmd = LWCmd .. Vars[i+1] .. Vars[i+2] .. Stop
                    print ("Device " .. DomDevice .. " stopped")
                end
            end
            if Lrf == "L" then
                Vars[i+2] = "D" .. string.sub(Vars[i+2],2)
                if DeviceValue == "Off" then
                    LWCmd = LWCmd .. Vars[i+1] .. Vars[i+2] .. Ulock
                    print ("Device " .. DomDevice .. " unlocked")
                elseif DeviceValue == "On" then
                    LWCmd = LWCmd .. Vars[i+1] .. Vars[i+2] .. Lock
                    print ("Device " .. DomDevice .. " locked")
                end
        end
--send complete command to LightwaveRF Link
            UDPSend(IPaddress,LWCmd)
            print ( "Command sent to " .. IPaddress .. " is " .. LWCmd)
            return commandArray
        end
        
    end
end
return commandArray

dgilbert2
Posts: 13
Joined: Wednesday 16 August 2017 8:08
Target OS: Raspberry Pi
Domoticz version: 3.5877
Location: UK
Contact:

Re: LightWaveRF Switch and Link Hub Integration

Post by dgilbert2 » Friday 20 October 2017 20:43

Wow, you have clearly put so much effort into these LightwaveRF scripts!

I have a number of LightwaveRF TRVs, do you know how I can read their temperatures into Domoticz, their API has blown my mind :-)

thanks

User avatar
lonebaggie
Posts: 96
Joined: Tuesday 31 January 2017 14:21
Target OS: Raspberry Pi
Domoticz version: beta
Location: England
Contact:

Re: LightWaveRF Switch and Link Hub Integration

Post by lonebaggie » Saturday 21 October 2017 14:16

Sorry don't have any these. They are two way devices.

I am slowly replacing my lightwaverf kit for https://www.domoticz.com/wiki/Xiaomi_Gateway_(Aqara) so I will not be investing in anymore lightwaverf kit. The version 2 lightwaverf kit is stupidly expensive, so this is the last script. Have a look at RFLink this may support the options
Last edited by lonebaggie on Sunday 22 October 2017 11:16, edited 1 time in total.

dgilbert2
Posts: 13
Joined: Wednesday 16 August 2017 8:08
Target OS: Raspberry Pi
Domoticz version: 3.5877
Location: UK
Contact:

Re: LightWaveRF Switch and Link Hub Integration

Post by dgilbert2 » Saturday 21 October 2017 17:47

Thanks for the feedback, I will take a look at RFlink. In the meantime I have now got your script working as above, version 2.1, so that's a start :-)

Yes, LightwaveRF version 2 is so expensive and does not seem to care about existing customers :-(

Good luck with your new kit.

User avatar
lonebaggie
Posts: 96
Joined: Tuesday 31 January 2017 14:21
Target OS: Raspberry Pi
Domoticz version: beta
Location: England
Contact:

Re: LightWaveRF Switch and Link Hub Integration

Post by lonebaggie » Sunday 22 October 2017 11:14

Good to know . I also use HA-Bridge. This will let you control LIghtwaverf From Amazon Alexa without having to the LIghtwaverf Skill. The advantage is sub second response. Will run on the same PI as Domoticz. I also have config from HA-bridge to control lightwaverf directly. PM me if interested

dgilbert2
Posts: 13
Joined: Wednesday 16 August 2017 8:08
Target OS: Raspberry Pi
Domoticz version: 3.5877
Location: UK
Contact:

Re: LightWaveRF Switch and Link Hub Integration

Post by dgilbert2 » Sunday 22 October 2017 22:01

Thanks, PM sent.

Post Reply

Who is online

Users browsing this forum: No registered users and 4 guests