Changeset 27


Ignore:
Timestamp:
2012-06-04 23:43:32 (13 years ago)
Author:
evanes
Message:

some homeeasy logging changed and coding added. no functional difference.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • I_RFXCOM.xml

    r25 r27  
    1919    ipPortRead = 0 
    2020    ipPortSend = 0 
    21      
    22     RFXConstants = { 
    23         sensorTypeMotion = 1, 
    24         sensorTypeDoor = 2, 
    25         sensorTypeKeyFob = 4, 
    26         sensorTypeRemote = 8, 
    27         sensorTypeControler = 16, 
    28         sensorTypeWindowCovering = 32, 
    29         sensorTypeTemperature = 64, 
    30         sensorTypeHumidity = 128, 
    31         sensorTypeWindDirection = 256,  
    32         sensorTypeWindSpeed = 512, 
    33         sensorTypeWindSpeedAv = 1024, 
    34         sensorTypeBarometer = 2048, 
    35         sensorTypeRain = 4096, 
    36          
    37         sensorManufacturerX10 = "X10", 
    38         sensorManufacturerHomeEasy = "HOMEEASY", 
    39         sensorManufacturerVisonic = "VISONIC", 
    40         sensorManufacturerOregon = "OREGON", 
    41         sensorManufacturerHarrison = "HARRISON", 
    42              
    43         cmdGroupOn = 1, 
    44         cmdGroupOff = 2, 
    45         cmdOn = 4, 
    46         cmdOff = 8, 
    47         cmdArmAway = 16, 
    48         cmdArmHome = 32, 
    49         cmdDisarm = 64, 
    50         cmdTamper = 128, 
    51         cmdJamming = 256, 
    52         cmdBatteryLow = 512, 
    53         cmdNormal = 1024, 
    54         cmdAlert = 2048 
    55     } 
    56      
     21   
     22  RFXConstants = { 
     23    sensorTypeMotion = 1, 
     24    sensorTypeDoor = 2, 
     25    sensorTypeKeyFob = 4, 
     26    sensorTypeRemote = 8, 
     27    sensorTypeControler = 16, 
     28    sensorTypeWindowCovering = 32, 
     29    sensorTypeTemperature = 64, 
     30    sensorTypeHumidity = 128, 
     31    sensorTypeWindDirection = 256,  
     32    sensorTypeWindSpeed = 512, 
     33    sensorTypeWindSpeedAv = 1024, 
     34    sensorTypeBarometer = 2048, 
     35    sensorTypeRain = 4096, 
     36     
     37    sensorManufacturerX10 = "X10", 
     38    sensorManufacturerHomeEasy = "HOMEEASY", 
     39    sensorManufacturerVisonic = "VISONIC", 
     40    sensorManufacturerOregon = "OREGON", 
     41    sensorManufacturerHarrison = "HARRISON", 
     42       
     43    cmdGroupOn = 1, 
     44    cmdGroupOff = 2, 
     45    cmdOn = 4, 
     46    cmdOff = 8, 
     47    cmdArmAway = 16, 
     48    cmdArmHome = 32, 
     49    cmdDisarm = 64, 
     50    cmdTamper = 128, 
     51    cmdJamming = 256, 
     52    cmdBatteryLow = 512, 
     53    cmdNormal = 1024, 
     54    cmdAlert = 2048 
     55  } 
     56   
    5757  function mainStartup(lul_device) 
    58         RFXDevicesLS = {} 
    59         RFXCountLS = 0 
    60         RFXDevicesDS = {} 
    61         RFXCountDS = 0 
    62         RFXDevicesMS = {} 
    63         RFXCountMS = 0 
    64         RFXDevicesKF = {} 
    65         RFXCountKF = 0 
    66         RFXCountTS2 = 0 
    67         RFXDevicesTS2 = {} 
    68         RFXCountHS2 = 0 
    69         RFXDevicesHS2 = {} 
    70         RFXCountBS2 = 0 
    71         RFXDevicesBS2 = {} 
    72         RFXCountRS2 = 0 
    73         RFXDevicesRS2 = {} 
    74         RFXCountAS2 = 0 
    75         RFXDevicesAS2 = {} 
    76         RFXCOM_HAR_Count = 0 
    77         RFXCOM_HAR_Devices = {} 
    78          
    79         THIS_DEVICE = lul_device 
    80         luup.log("Starting RFXCOM device: " .. tostring(THIS_DEVICE)) 
    81         local device = luup.devices[THIS_DEVICE] 
    82  
    83         -- Set our device IP number 
    84         if (device.ip == "") then 
    85             return false, "IP Address attribute must be configured", "rfxcom" 
    86         end 
    87         ipAddress = device.ip 
    88  
    89         -- Get defined read and write port. If not set, set the default port numbers 10001 and 10002 
    90         local portList = luup.variable_get("urn:upnp-esweb-nl:serviceId:rfxcom1", "portRead", lul_device) 
    91         if ((portList or "") == "") then 
    92             portList = "10001" 
    93             luup.variable_set("urn:upnp-esweb-nl:serviceId:rfxcom1", "portRead", portList, lul_device) 
    94         elseif (portList:find("%d") == nil) then 
    95             return false, "The Port Read parameter must be of the form nnnn.  eg. 10001" 
    96         end 
    97         ipPortRead = portList 
    98        
    99         local portList = luup.variable_get("urn:upnp-esweb-nl:serviceId:rfxcom1", "portWrite", lul_device) 
    100         if ((portList or "") == "") then 
    101             portList = "10002" 
    102             luup.variable_set("urn:upnp-esweb-nl:serviceId:rfxcom1", "portWrite", portList, lul_device) 
    103         elseif (portList:find("%d") == nil) then 
    104             return false, "The Port Write parameter must be of the form nnnn.  eg. 10002" 
    105         end      
    106         ipPortSend = portList 
    107  
    108         -- Check if we should create the devices automaticly 
    109         local RFXAutoCreate = luup.variable_get("urn:upnp-esweb-nl:serviceId:rfxcom1", "AutoCreate", lul_device) 
    110         if ((RFXAutoCreate or "") == "") then 
    111             luup.variable_set("urn:upnp-esweb-nl:serviceId:rfxcom1", "AutoCreate", "0", lul_device) 
    112         end 
    113          
    114         -- Check all devices if they are children. If so, register in 
    115         -- the correct array based on the device type 
    116         for k, v in pairs(luup.devices) do 
    117             -- Look for devices with this device as parent 
    118             if (v.device_num_parent == lul_device) then 
    119                 luup.log( "Found child device, lets save! id " .. tostring(v.id) .. " device " .. tostring(v.device_type)) 
    120  
    121                 -- Check on door sensors 
    122                 if (tostring(v.device_type) == "urn:schemas-micasaverde-com:device:DoorSensor:1") then 
    123                     RFXCountDS = RFXCountDS + 1 
    124                     RFXDevicesDS[RFXCountDS] = v.id 
    125                 end 
    126                  
    127                 -- Check on motion sensors 
    128                 if (tostring(v.device_type) == "urn:schemas-micasaverde-com:device:MotionSensor:1") then 
    129                     RFXCountMS = RFXCountMS + 1 
    130                     RFXDevicesMS[RFXCountMS] = v.id 
    131                 end      
    132  
    133                 -- Check on keyfobs 
    134                 if (tostring(v.device_type) == "urn:schemas-ActiveRFID:device:RFIDTransmitter:1") then 
    135                     RFXCountKF = RFXCountKF + 1 
    136                     RFXDevicesKF[RFXCountKF] = v.id 
    137                 end      
    138  
    139                 -- Check on light switches  
    140                 if (tostring(v.device_type) == "urn:schemas-upnp-org:device:BinaryLight:1") then 
    141                     RFXCountLS = RFXCountLS + 1 
    142                     RFXDevicesLS[RFXCountLS] = v.id 
    143                 end 
    144  
    145                 -- Check on Harrison window coverings 
    146                 if (tostring(v.device_type) == "urn:schemas-micasaverde-com:device:WindowCovering:1") then 
    147                     RFXCOM_HAR_Count = RFXCOM_HAR_Count + 1 
    148                     RFXCOM_HAR_Devices[RFXCOM_HAR_Count] = v.id 
    149                 end 
    150                  
    151                 -- Check on temperature devices 
    152                 if (tostring(v.device_type) == "urn:schemas-micasaverde-com:device:TemperatureSensor:1") then 
    153                     RFXCountTS2 = RFXCountTS2 + 1 
    154                     RFXDevicesTS2[RFXCountTS2] = v.id 
    155                     luup.log("Registered device " .. RFXDevicesTS2[RFXCountTS2] .. " as temperature sensor ".. RFXCountTS2) 
    156                 end 
    157                  
    158                 -- Check on humidity devices 
    159                 if (tostring(v.device_type) == "urn:schemas-micasaverde-com:device:HumiditySensor:1") then 
    160                     RFXCountHS2 = RFXCountHS2 + 1 
    161                     RFXDevicesHS2[RFXCountHS2] = v.id 
    162                     luup.log("Registered device " .. RFXDevicesHS2[RFXCountHS2] .. " as humidity sensor ".. RFXCountHS2) 
    163                 end 
    164                  
    165                 -- Check on barometer devices 
    166                 if (tostring(v.device_type) == "urn:schemas-micasaverde-com:device:BarometerSensor:1") then 
    167                     RFXCountBS2 = RFXCountBS2 + 1 
    168                     RFXDevicesBS2[RFXCountBS2] = v.id 
    169                     luup.log("Registered device " .. RFXDevicesBS2[RFXCountBS2] .. " as barometer sensor ".. RFXCountBS2) 
    170                 end 
    171                  
    172                 -- Check on barometer devices 
    173                 if (tostring(v.device_type) == "urn:schemas-micasaverde-com:device:RainSensor:1") then 
    174                     RFXCountRS2 = RFXCountRS2 + 1 
    175                     RFXDevicesRS2[RFXCountRS2] = v.id 
    176                     luup.log("Registered device " .. RFXDevicesRS2[RFXCountRS2] .. " as rain sensor ".. RFXCountRS2) 
    177                 end 
    178                  
    179                 -- Check on EZAnemoSensor devices 
    180                 if (tostring(v.device_type) == "urn:schemas-upnp-org:device:EZAnemoSensor:1") then 
    181                     RFXCountAS2 = RFXCountAS2 + 1 
    182                     RFXDevicesAS2[RFXCountAS2] = v.id 
    183                     luup.log("Registered device " .. RFXDevicesAS2[RFXCountAS2] .. " as Anemo sensor ".. RFXCountAS2) 
    184                 end 
    185             end 
    186         end 
    187         luup.log("Tree with number child devices:") 
    188         luup.log("       door sensors: " .. RFXCountDS) 
    189         luup.log("     motion sensors: " .. RFXCountMS) 
    190         luup.log("     keyfob sensors: " .. RFXCountKF) 
    191         luup.log("     light switches: " .. RFXCountLS) 
    192         luup.log("    window covering: " .. RFXCOM_HAR_Count) 
    193         luup.log("temperature sensors: " .. RFXCountTS2) 
    194         luup.log("   humidity sensors: " .. RFXCountHS2) 
    195         luup.log("   pressure sensors: " .. RFXCountBS2) 
    196         luup.log("       rain sensors: " .. RFXCountBS2) 
    197         luup.log("      anemo sensors: " .. RFXCountAS2) 
    198              
    199         luup.log("Open socket for read " .. ipAddress .. " port " .. ipPortRead ) 
    200         luup.io.open(THIS_DEVICE, ipAddress, ipPortRead) 
    201         startupReady = 1 
    202  
    203         luup.log("Startup RFXCOM ready") 
    204  
    205         return true, "ok", "rfxcom" 
     58    RFXDevicesLS = {} 
     59    RFXCountLS = 0 
     60    RFXDevicesDS = {} 
     61    RFXCountDS = 0 
     62    RFXDevicesMS = {} 
     63    RFXCountMS = 0 
     64    RFXDevicesKF = {} 
     65    RFXCountKF = 0 
     66    RFXCountTS2 = 0 
     67    RFXDevicesTS2 = {} 
     68    RFXCountHS2 = 0 
     69    RFXDevicesHS2 = {} 
     70    RFXCountBS2 = 0 
     71    RFXDevicesBS2 = {} 
     72    RFXCountRS2 = 0 
     73    RFXDevicesRS2 = {} 
     74    RFXCountAS2 = 0 
     75    RFXDevicesAS2 = {} 
     76    RFXCOM_HAR_Count = 0 
     77    RFXCOM_HAR_Devices = {} 
     78     
     79    THIS_DEVICE = lul_device 
     80    luup.log("Starting RFXCOM device: " .. tostring(THIS_DEVICE)) 
     81    local device = luup.devices[THIS_DEVICE] 
     82 
     83    -- Set our device IP number 
     84    if (device.ip == "") then 
     85      return false, "IP Address attribute must be configured", "rfxcom" 
     86    end 
     87    ipAddress = device.ip 
     88 
     89    -- Get defined read and write port. If not set, set the default port numbers 10001 and 10002 
     90    local portList = luup.variable_get("urn:upnp-esweb-nl:serviceId:rfxcom1", "portRead", lul_device) 
     91    if ((portList or "") == "") then 
     92      portList = "10001" 
     93      luup.variable_set("urn:upnp-esweb-nl:serviceId:rfxcom1", "portRead", portList, lul_device) 
     94    elseif (portList:find("%d") == nil) then 
     95      return false, "The Port Read parameter must be of the form nnnn.  eg. 10001" 
     96    end 
     97    ipPortRead = portList 
     98     
     99    local portList = luup.variable_get("urn:upnp-esweb-nl:serviceId:rfxcom1", "portWrite", lul_device) 
     100    if ((portList or "") == "") then 
     101      portList = "10002" 
     102      luup.variable_set("urn:upnp-esweb-nl:serviceId:rfxcom1", "portWrite", portList, lul_device) 
     103    elseif (portList:find("%d") == nil) then 
     104      return false, "The Port Write parameter must be of the form nnnn.  eg. 10002" 
     105    end      
     106    ipPortSend = portList 
     107 
     108    -- Check if we should create the devices automaticly 
     109    local RFXAutoCreate = luup.variable_get("urn:upnp-esweb-nl:serviceId:rfxcom1", "AutoCreate", lul_device) 
     110    if ((RFXAutoCreate or "") == "") then 
     111      luup.variable_set("urn:upnp-esweb-nl:serviceId:rfxcom1", "AutoCreate", "0", lul_device) 
     112    end 
     113     
     114    -- Check all devices if they are children. If so, register in 
     115    -- the correct array based on the device type 
     116    for k, v in pairs(luup.devices) do 
     117      -- Look for devices with this device as parent 
     118      if (v.device_num_parent == lul_device) then 
     119        luup.log( "Found child device, lets save! id " .. tostring(v.id) .. " device " .. tostring(v.device_type)) 
     120 
     121        -- Check on door sensors 
     122        if (tostring(v.device_type) == "urn:schemas-micasaverde-com:device:DoorSensor:1") then 
     123          RFXCountDS = RFXCountDS + 1 
     124          RFXDevicesDS[RFXCountDS] = v.id 
     125        end 
     126         
     127        -- Check on motion sensors 
     128        if (tostring(v.device_type) == "urn:schemas-micasaverde-com:device:MotionSensor:1") then 
     129          RFXCountMS = RFXCountMS + 1 
     130          RFXDevicesMS[RFXCountMS] = v.id 
     131        end    
     132 
     133        -- Check on keyfobs 
     134        if (tostring(v.device_type) == "urn:schemas-ActiveRFID:device:RFIDTransmitter:1") then 
     135          RFXCountKF = RFXCountKF + 1 
     136          RFXDevicesKF[RFXCountKF] = v.id 
     137        end    
     138 
     139        -- Check on light switches  
     140        if (tostring(v.device_type) == "urn:schemas-upnp-org:device:BinaryLight:1") then 
     141          RFXCountLS = RFXCountLS + 1 
     142          RFXDevicesLS[RFXCountLS] = v.id 
     143        end 
     144 
     145        -- Check on Harrison window coverings 
     146        if (tostring(v.device_type) == "urn:schemas-micasaverde-com:device:WindowCovering:1") then 
     147          RFXCOM_HAR_Count = RFXCOM_HAR_Count + 1 
     148          RFXCOM_HAR_Devices[RFXCOM_HAR_Count] = v.id 
     149        end 
     150         
     151        -- Check on temperature devices 
     152        if (tostring(v.device_type) == "urn:schemas-micasaverde-com:device:TemperatureSensor:1") then 
     153          RFXCountTS2 = RFXCountTS2 + 1 
     154          RFXDevicesTS2[RFXCountTS2] = v.id 
     155          luup.log("Registered device " .. RFXDevicesTS2[RFXCountTS2] .. " as temperature sensor ".. RFXCountTS2) 
     156        end 
     157         
     158        -- Check on humidity devices 
     159        if (tostring(v.device_type) == "urn:schemas-micasaverde-com:device:HumiditySensor:1") then 
     160          RFXCountHS2 = RFXCountHS2 + 1 
     161          RFXDevicesHS2[RFXCountHS2] = v.id 
     162          luup.log("Registered device " .. RFXDevicesHS2[RFXCountHS2] .. " as humidity sensor ".. RFXCountHS2) 
     163        end 
     164         
     165        -- Check on barometer devices 
     166        if (tostring(v.device_type) == "urn:schemas-micasaverde-com:device:BarometerSensor:1") then 
     167          RFXCountBS2 = RFXCountBS2 + 1 
     168          RFXDevicesBS2[RFXCountBS2] = v.id 
     169          luup.log("Registered device " .. RFXDevicesBS2[RFXCountBS2] .. " as barometer sensor ".. RFXCountBS2) 
     170        end 
     171         
     172        -- Check on barometer devices 
     173        if (tostring(v.device_type) == "urn:schemas-micasaverde-com:device:RainSensor:1") then 
     174          RFXCountRS2 = RFXCountRS2 + 1 
     175          RFXDevicesRS2[RFXCountRS2] = v.id 
     176          luup.log("Registered device " .. RFXDevicesRS2[RFXCountRS2] .. " as rain sensor ".. RFXCountRS2) 
     177        end 
     178         
     179        -- Check on EZAnemoSensor devices 
     180        if (tostring(v.device_type) == "urn:schemas-upnp-org:device:EZAnemoSensor:1") then 
     181          RFXCountAS2 = RFXCountAS2 + 1 
     182          RFXDevicesAS2[RFXCountAS2] = v.id 
     183          luup.log("Registered device " .. RFXDevicesAS2[RFXCountAS2] .. " as Anemo sensor ".. RFXCountAS2) 
     184        end 
     185      end 
     186    end 
     187    luup.log("Tree with number child devices:") 
     188    luup.log("       door sensors: " .. RFXCountDS) 
     189    luup.log("     motion sensors: " .. RFXCountMS) 
     190    luup.log("     keyfob sensors: " .. RFXCountKF) 
     191    luup.log("     light switches: " .. RFXCountLS) 
     192    luup.log("    window covering: " .. RFXCOM_HAR_Count) 
     193    luup.log("temperature sensors: " .. RFXCountTS2) 
     194    luup.log("   humidity sensors: " .. RFXCountHS2) 
     195    luup.log("   pressure sensors: " .. RFXCountBS2) 
     196    luup.log("       rain sensors: " .. RFXCountBS2) 
     197    luup.log("      anemo sensors: " .. RFXCountAS2) 
     198       
     199    luup.log("Open socket for read " .. ipAddress .. " port " .. ipPortRead ) 
     200    luup.io.open(THIS_DEVICE, ipAddress, ipPortRead) 
     201    startupReady = 1 
     202 
     203    luup.log("Startup RFXCOM ready") 
     204 
     205    return true, "ok", "rfxcom" 
    206206  end 
    207207      
     
    247247    return lsResult 
    248248  end 
    249      
    250     function SendHarrisonCmd( psHouse, psCmd) 
     249   
     250  function SendHarrisonCmd( psHouse, psCmd) 
    251251    local liMODEHAR = 0x34 
    252       local liMODERFXCOM = 0x33 
    253       local client = nil 
    254       local lsSend = "" 
    255       local lsSendStat = "" 
    256       local lsRecvStat = "" 
    257            
     252    local liMODERFXCOM = 0x33 
     253    local client = nil 
     254    local lsSend = "" 
     255    local lsSendStat = "" 
     256    local lsRecvStat = "" 
     257         
    258258    luup.log("Connect to rfxtrans ip " .. ipAddress .. " port " .. ipPortSend) 
    259259    client = socket.connect(ipAddress, ipPortSend) 
    260260 
    261       if client ~= nil then 
    262             client:settimeout(2) 
    263  
    264             luup.log("Send cmd " .. psCmd .. " to " .. psHouse) 
    265  
    266             --to send a command we need to specify:  F0 CMD F0 CMD    where CMD has special meaning. 
    267             lsSend = string.char(0xf0) .. string.char(liMODEHAR) .. string.char(0xf0) .. string.char(liMODEHAR) 
    268             --luup.log("send cmd: " .. formattohex(lsSend)) 
    269  
    270             lsSendStat = client:send(lsSend) 
    271             --luup.log("send cmd result: " .. formattohex(lsSendStat)) 
    272  
    273             -- We will receive an ack with the last byte of the command sent. or a nack 0x5a 
    274             lsRecvStat=client:receive(1) 
    275             --luup.log("recv result: " .. formattohex(lsRecvStat)) 
    276  
    277             --to send a command we need to specify: [House Code][Command] 
    278             -- House code is 2 bytes. Open up the remote. There are 8 dip switches with a 3 state. 
    279             -- note down the bits where state top = 2 bits: 11, middle = 10, down = 00. 
    280             -- write down the bits starting from switch 1 to 8. 
    281             -- I get the following code of bits:  00 00 11 11 00 10 10 10 
    282             -- This is binary and can be written in hex as follows:  0F2A and there you have the house code 
    283             -- The curtain has three command: open, close, stop: F380, CF80 and FC80 
    284             -- 
    285             if ( psCmd == "OPEN" ) then 
    286                 lsSend = psHouse .. "F380" 
    287                 luup.log("send open command for Harrison house code: " .. psHouse) 
    288             elseif ( psCmd == "CLOSE") then 
    289                 lsSend = psHouse .. "CF80" 
    290                 luup.log("send close command for Harrison house code: " .. psHouse) 
    291             else 
    292                 --stop 
    293                 lsSend = psHouse .. "FC80" 
    294                 luup.log("send stop command for Harrison house code: " .. psHouse) 
    295             end 
    296  
    297             lsSend = ToByteString( lsSend ) 
    298  
    299             local lsSendStat = client:send(lsSend) 
    300             --luup.log("send cmd result: " .. formattohex(lsSendStat)) 
    301  
    302             -- We will recieve an ack with the last byte. or a nack 0x5a 
    303             local lsRecvStat=client:receive(1) 
    304             --luup.log("recv result: " .. formattohex(lsRecvStat)) 
    305  
    306             -- Close client connection 
    307             client:close() 
    308         else 
    309             return false 
    310         end 
    311         luup.log("finished: ") 
     261    if client ~= nil then 
     262      client:settimeout(2) 
     263 
     264      luup.log("Send cmd " .. psCmd .. " to " .. psHouse) 
     265 
     266      --to send a command we need to specify:  F0 CMD F0 CMD    where CMD has special meaning. 
     267      lsSend = string.char(0xf0) .. string.char(liMODEHAR) .. string.char(0xf0) .. string.char(liMODEHAR) 
     268      --luup.log("send cmd: " .. formattohex(lsSend)) 
     269 
     270      lsSendStat = client:send(lsSend) 
     271      --luup.log("send cmd result: " .. formattohex(lsSendStat)) 
     272 
     273      -- We will receive an ack with the last byte of the command sent. or a nack 0x5a 
     274      lsRecvStat=client:receive(1) 
     275      --luup.log("recv result: " .. formattohex(lsRecvStat)) 
     276 
     277      --to send a command we need to specify: [House Code][Command] 
     278      -- House code is 2 bytes. Open up the remote. There are 8 dip switches with a 3 state. 
     279      -- note down the bits where state top = 2 bits: 11, middle = 10, down = 00. 
     280      -- write down the bits starting from switch 1 to 8. 
     281      -- I get the following code of bits:  00 00 11 11 00 10 10 10 
     282      -- This is binary and can be written in hex as follows:  0F2A and there you have the house code 
     283      -- The curtain has three command: open, close, stop: F380, CF80 and FC80 
     284      -- 
     285      if ( psCmd == "OPEN" ) then 
     286        lsSend = psHouse .. "F380" 
     287        luup.log("send open command for Harrison house code: " .. psHouse) 
     288      elseif ( psCmd == "CLOSE") then 
     289        lsSend = psHouse .. "CF80" 
     290        luup.log("send close command for Harrison house code: " .. psHouse) 
     291      else 
     292        --stop 
     293        lsSend = psHouse .. "FC80" 
     294        luup.log("send stop command for Harrison house code: " .. psHouse) 
     295      end 
     296 
     297      lsSend = ToByteString( lsSend ) 
     298 
     299      local lsSendStat = client:send(lsSend) 
     300      --luup.log("send cmd result: " .. formattohex(lsSendStat)) 
     301 
     302      -- We will recieve an ack with the last byte. or a nack 0x5a 
     303      local lsRecvStat=client:receive(1) 
     304      --luup.log("recv result: " .. formattohex(lsRecvStat)) 
     305 
     306      -- Close client connection 
     307      client:close() 
     308    else 
     309      return false 
     310    end 
     311    luup.log("finished: ") 
    312312  end 
    313313  
    314     function SendCmd( psID, psCmd ) 
     314  function SendCmd( psID, psCmd ) 
    315315    local liMODERESET = 0x30 
    316316    local liMODEHAR = 0x34 
    317       local liMODEVAR = 0x33 
    318       local liMODEBRB48 = 0x37 
    319       local liMODEKAKURF = 0x3d 
    320       local client = nil 
    321       local lsSend = "" 
    322       local lsSendStat = "" 
    323       local lsRecvStat = "" 
    324        
    325        
     317    local liMODEVAR = 0x33 
     318    local liMODEBRB48 = 0x37 
     319    local liMODEKAKURF = 0x3d 
     320    local client = nil 
     321    local lsSend = "" 
     322    local lsSendStat = "" 
     323    local lsRecvStat = "" 
     324     
     325     
    326326    luup.log("Connect to rfxtrans ip " .. ipAddress .. " port " .. ipPortSend) 
    327327    client = socket.connect(ipAddress, ipPortSend) 
    328328 
    329       if client ~= nil then 
    330             client:settimeout(2) 
    331  
    332             luup.log("Send cmd " .. psCmd .. " to " .. psID) 
    333  
    334             -- Because this is a homeeasy, kaku device, calc the 
    335             -- command to be send based on the ID of the device 
    336              
    337             local lsDeviceType = string.sub( psID, 1, 3) 
    338             luup.log("Device type is " .. lsDeviceType .. " based on id " .. psID) 
    339              
    340             if lsDeviceType == "HEA" then 
    341             --to send a command we need to specify:  F0 CMD F0 CMD    where CMD has special meaning. 
    342             lsSend = string.char(0xf0) .. string.char(liMODERESET) .. string.char(0xf0) .. string.char(liMODERESET) 
     329    if client ~= nil then 
     330      client:settimeout(2) 
     331 
     332      luup.log("Send cmd " .. psCmd .. " to " .. psID) 
     333 
     334      -- Because this is a homeeasy, kaku device, calc the 
     335      -- command to be send based on the ID of the device 
     336       
     337      local lsDeviceType = string.sub( psID, 1, 3) 
     338      luup.log("Device type is " .. lsDeviceType .. " based on id " .. psID) 
     339       
     340      if lsDeviceType == "HEA" then 
     341        --to send a command we need to specify:  F0 CMD F0 CMD    where CMD has special meaning. 
     342        lsSend = string.char(0xf0) .. string.char(liMODERESET) .. string.char(0xf0) .. string.char(liMODERESET) 
    343343        lsRecvStat = DoSend( client, lsSend) 
    344             lsSend = string.char(0xf0) .. string.char(liMODEVAR) .. string.char(0xf0) .. string.char(liMODEVAR) 
     344        lsSend = string.char(0xf0) .. string.char(liMODEVAR) .. string.char(0xf0) .. string.char(liMODEVAR) 
    345345        lsRecvStat = DoSend( client, lsSend) 
    346346 
    347                 lsSend = RecodeHomeEasy34(psID, psCmd) 
    348             elseif lsDeviceType == "X10" then 
    349             --to send a command we need to specify:  F0 CMD F0 CMD    where CMD has special meaning. 
    350 --              lsSend = string.char(0xf0) .. string.char(liMODERESET) .. string.char(0xf0) .. string.char(liMODERESET) 
     347        lsSend = RecodeHomeEasy33(psID, psCmd) 
     348      elseif lsDeviceType == "X10" then 
     349--        to send a command we need to specify:  F0 CMD F0 CMD    where CMD has special meaning. 
     350--        lsSend = string.char(0xf0) .. string.char(liMODERESET) .. string.char(0xf0) .. string.char(liMODERESET) 
    351351--        lsRecvStat = DoSend( client, lsSend) 
    352             lsSend = string.char(0xf0) .. string.char(liMODEKAKURF) .. string.char(0xf0) .. string.char(liMODEKAKURF) 
     352        lsSend = string.char(0xf0) .. string.char(liMODEKAKURF) .. string.char(0xf0) .. string.char(liMODEKAKURF) 
    353353        lsRecvStat = DoSend( client, lsSend) 
    354                 lsSend = RecodeX10(psID, psCmd) 
    355             else 
    356                 lsSend = nil 
    357             end 
    358              
    359             if lsSend ~= nil then 
    360                 lsSend = ToByteString(lsSend) 
    361                 luup.log("send command " .. psCmd .. " as " .. formattohex(lsSend)) 
     354        lsSend = RecodeX10(psID, psCmd) 
     355      else 
     356        lsSend = nil 
     357      end 
     358       
     359      if lsSend ~= nil then 
     360        lsSend = ToByteString(lsSend) 
     361        luup.log("send command " .. psCmd .. " as " .. formattohex(lsSend)) 
    362362         
    363                 -- We will recieve an ack with the last byte. or a nack 0x5a 
    364                 lsRecvStat = DoSend( client, lsSend) 
    365             end 
    366              
    367             -- Close client connection 
    368             client:close() 
    369         else 
    370         luup.log("connection failed.") 
    371             return false 
    372         end 
    373         luup.log("finished: ") 
     363        -- We will receive an ack with the last byte. or a nack 0x5a 
     364        lsRecvStat = DoSend( client, lsSend) 
     365      end 
     366       
     367      -- Close client connection 
     368      client:close() 
     369    else 
     370      luup.log("connection failed.") 
     371      return false 
     372    end 
     373    luup.log("finished: ") 
    374374  end 
    375375 
    376376  function DoSend( poClient, psMessage) 
    377     if psMessage ~= nil then luup.log("send cmd: " .. formattohex(psMessage)) end 
    378  
    379     local lsSendStat = poClient:send(psMessage) 
    380  
    381     -- We will receive an ack with the last byte of the command sent. or a nack 0x5a 
    382     local lsRecvStat = poClient:receive(1) 
    383     if lsRecvStat ~= nil then luup.log("recv result: " .. formattohex(lsRecvStat)) end 
    384     return lsRecvStat 
     377    if psMessage ~= nil then luup.log("send cmd: " .. formattohex(psMessage)) end 
     378 
     379    local lsSendStat = poClient:send(psMessage) 
     380 
     381    -- We will receive an ack with the last byte of the command sent. or a nack 0x5a 
     382    local lsRecvStat = poClient:receive(1) 
     383    if lsRecvStat ~= nil then luup.log("recv result: " .. formattohex(lsRecvStat)) end 
     384    return lsRecvStat 
    385385  end 
    386386 
    387     function RecodeHomeEasy33(psID,psCmd) 
    388         local lbCmd = string.byte(psID, 5) 
    389          
    390         if psCmd == RFXConstants.cmdOff then 
    391             lbCmd = bitw.bor(lbCmd,0x00) 
    392         elseif psCmd == RFXConstants.cmdOn then 
    393             lbCmd = bitw.bor(lbCmd,0x10) 
    394         elseif psCmd == RFXConstants.cmdGroupOff then 
    395             lbCmd = bitw.bor(lbCmd,0x20) 
    396         elseif psCmd == RFXConstants.cmdGroupOn then         
    397             lbCmd = bitw.bor(lbCmd,0x30) 
    398         end 
    399          
    400         return string.format("%02x%02x%02x%02x%02x%02x",string.byte(psID, 1),string.byte(psID, 2),string.byte(psID, 3), 
    401                                 string.byte(psID, 4),lbCmd, string.byte(psID, 6)) 
    402     end 
    403      
    404     function RecodeHomeEasy34(psID, psCmd) 
    405     luup.log("Recode homeeasy" ) 
    406      
    407       local lsIDBytes = ToByteString( string.sub(psID, 4, 14 ) ) 
     387  function RecodeHomeEasy33(psID, psCmd) 
     388    luup.log("Recode homeeasy33" ) 
     389 
     390    local lsIDBytes = ToByteString( string.sub(psID, 4, 14 ) ) 
     391    luup.log("Raw data " .. formattohex( lsIDBytes) ) 
     392 
     393    local lbCmd = 0 
     394    if psCmd == RFXConstants.cmdOff then 
     395      lbCmd = 0x00 
     396    elseif psCmd == RFXConstants.cmdOn then 
     397      lbCmd = 0x10 
     398    elseif psCmd == RFXConstants.cmdGroupOff then 
     399      lbCmd = 0x20 
     400    elseif psCmd == RFXConstants.cmdGroupOn then     
     401      lbCmd = 0x30 
     402    end 
     403 
     404    local lsEnd = "00" 
    408405    local lsID = "21" 
     406    lsID = lsID .. string.format("%02X", bitw.lshift( bitw.band( string.byte(lsIDBytes, 1), 0x03 ), 6) + bitw.rshift( bitw.band( string.byte(lsIDBytes, 2), 0xFC ), 2)) 
     407    lsID = lsID .. string.format("%02X", bitw.lshift( bitw.band( string.byte(lsIDBytes, 2), 0x03 ), 6) + bitw.rshift( bitw.band( string.byte(lsIDBytes, 3), 0xFC ), 2)) 
     408    lsID = lsID .. string.format("%02X", bitw.lshift( bitw.band( string.byte(lsIDBytes, 3), 0x03 ), 6) + bitw.rshift( bitw.band( string.byte(lsIDBytes, 4), 0xFC ), 2)) 
     409    lsID = lsID .. string.format("%02X", bitw.lshift( bitw.band( string.byte(lsIDBytes, 4), 0x03 ), 6) + lbCmd + bitw.band( string.byte(lsIDBytes, 5), 0x0F)) 
     410 
     411    return lsID .. lsEnd 
     412  end 
     413   
     414  function RecodeHomeEasy34(psID, psCmd) 
     415    luup.log("Recode homeeasy34" ) 
     416   
     417    local lsIDBytes = ToByteString( string.sub(psID, 4, 14 ) ) 
     418    local lsID = "22" 
    409419    luup.log("Raw data " .. formattohex( lsIDBytes) ) 
    410420     
     
    415425      lsEnd = "00" 
    416426    end 
    417     --Raw data 00 0E 28 73 0A 0B  wordt 22 03 8A 1C DA 
    418         -- 00.0C.86.0A.B cmd 4 moet  worden 22 03 A1 C2 9B 80  
    419         -- Make ID as received bytes minus command 
     427    --Raw data 00 0E 28 73 0A 0B  is changed to 22 03 8A 1C DA 
     428    -- 00.0C.86.0A.B cmd 4        should be     22 03 A1 C2 9B 80   
     429     
     430    -- Make ID as received bytes minus command 
    420431    lsID = lsID .. string.format("%02X", bitw.lshift( bitw.band( string.byte(lsIDBytes, 1), 0x03 ), 6) + bitw.rshift( bitw.band( string.byte(lsIDBytes, 2), 0xFC ), 2)) 
    421432    lsID = lsID .. string.format("%02X", bitw.lshift( bitw.band( string.byte(lsIDBytes, 2), 0x03 ), 6) + bitw.rshift( bitw.band( string.byte(lsIDBytes, 3), 0xFC ), 2)) 
     
    423434    lsID = lsID .. string.format("%02X", bitw.lshift( bitw.band( string.byte(lsIDBytes, 4), 0x03 ), 6) + liCommandCorrection + bitw.band( string.byte(lsIDBytes, 5), 0x0F)) 
    424435 
    425         return lsID .. lsEnd 
    426     end 
    427  
    428     function RecodeHomeEasy36(psID,psCmd) 
    429         return nil 
    430     end 
    431      
    432     function RecodeX10( psID, psCmd) 
    433      
     436    return lsID .. lsEnd 
     437  end 
     438 
     439  function RecodeHomeEasy36(psID,psCmd) 
     440    luup.log("Recode homeeasy36 not implemented" ) 
     441    return nil 
     442  end 
     443   
     444  function RecodeX10( psID, psCmd) 
     445   
    434446    luup.log("RecodeX10") 
    435      
    436       local laX10HOUSE = { 0x60, 0x70, 0x40, 0x50, 0x80, 0x90, 0xa0, 0xb0, 0xe0, 0xf0, 0xc0, 0xd0, 0x00, 0x10, 0x20, 0x30 } 
    437       local laX10UNIT  = { 0x00, 0x10, 0x08, 0x18, 0x40, 0x50, 0x48, 0x58, 0x00, 0x10, 0x08, 0x18, 0x40, 0x50, 0x48, 0x58 } 
    438        
    439       local lsHouseCode = string.sub(psID, 4, 4) 
    440       local liHouseCode = 1 + string.byte(lsHouseCode) - string.byte("A") 
    441       local liHouseByte = laX10HOUSE[ liHouseCode ] 
    442        
    443       local lsUnitCode = "" 
    444       local liUnitCode = 0 
    445       local liUnitByte = 0 
    446       local liCommandCorrection = 0 
    447  
    448       if ( string.len( psID ) > 5 ) then 
    449         lsUnitCode = string.sub(psID, 5, 6) 
    450         liUnitCode = tonumber(lsUnitCode)  
    451       liUnitByte = laX10UNIT[ liUnitCode ] 
    452  
    453         if ((psCmd == RFXConstants.cmdOff) or (psCmd == RFXConstants.CmdGroupOff)) then 
    454             liCommandCorrection = 32 
    455             luup.log("cmdoff corr") 
    456         end 
    457     else 
    458       -- group command!  80 = off, 90 = on 
    459       liUnitByte = 0x80 
    460       liCommandCorrection = 0 
    461         if ((psCmd == RFXConstants.cmdOn) or (psCmd == RFXConstants.CmdGroupOn)) then 
    462           liCommandCorrection = 16 
    463       end 
    464             luup.log("group cmd " .. liCommandCorrection ) 
    465       end 
    466        
    467         local liHouseCorrection = 0 
    468         if (liUnitCode >= 9) then 
    469             liHouseCorrection = 4 
    470         end 
    471       liHouseByte = liHouseCorrection + liHouseByte 
    472         liUnitByte = liCommandCorrection + liUnitByte 
    473  
    474       local lsResultstrA = string.format("%02X", bitw.band(bitw.bnot(liHouseByte),0x7fffffff)) 
    475       local lsResultstrB = string.format("%02X", bitw.band(bitw.bnot(liUnitByte),0x7fffffff)) 
    476       local lsResultstr = string.format("%02X%02X%s%02X%s", 0x20, liHouseByte, string.sub(lsResultstrA,7,8), liUnitByte, string.sub(lsResultstrB,7,8)) 
    477     return lsResultstr 
    478     end 
    479  
    480     ------------------------------------------------------------------------------------             
    481     -- decode X10 commands 
    482     ------------------------------------------------------------------------------------             
    483     function decodeX10(psCommand) 
    484       local lsCommand = string.sub( psCommand, 2) 
     447   
     448    local laX10HOUSE = { 0x60, 0x70, 0x40, 0x50, 0x80, 0x90, 0xa0, 0xb0, 0xe0, 0xf0, 0xc0, 0xd0, 0x00, 0x10, 0x20, 0x30 } 
     449    local laX10UNIT  = { 0x00, 0x10, 0x08, 0x18, 0x40, 0x50, 0x48, 0x58, 0x00, 0x10, 0x08, 0x18, 0x40, 0x50, 0x48, 0x58 } 
     450     
     451    local lsHouseCode = string.sub(psID, 4, 4) 
     452    local liHouseCode = 1 + string.byte(lsHouseCode) - string.byte("A") 
     453    local liHouseByte = laX10HOUSE[ liHouseCode ] 
     454     
     455    local lsUnitCode = "" 
     456    local liUnitCode = 0 
     457    local liUnitByte = 0 
     458    local liCommandCorrection = 0 
     459 
     460    if ( string.len( psID ) > 5 ) then 
     461      lsUnitCode = string.sub(psID, 5, 6) 
     462      liUnitCode = tonumber(lsUnitCode)  
     463      liUnitByte = laX10UNIT[ liUnitCode ] 
     464 
     465      if ((psCmd == RFXConstants.cmdOff) or (psCmd == RFXConstants.CmdGroupOff)) then 
     466        liCommandCorrection = 32 
     467        luup.log("cmdoff corr") 
     468      end 
     469    else 
     470      -- group command!  80 = off, 90 = on 
     471      liUnitByte = 0x80 
     472      liCommandCorrection = 0 
     473      if ((psCmd == RFXConstants.cmdOn) or (psCmd == RFXConstants.CmdGroupOn)) then 
     474        liCommandCorrection = 16 
     475      end 
     476      luup.log("group cmd " .. liCommandCorrection ) 
     477    end 
     478     
     479    local liHouseCorrection = 0 
     480    if (liUnitCode >= 9) then 
     481      liHouseCorrection = 4 
     482    end 
     483    liHouseByte = liHouseCorrection + liHouseByte 
     484    liUnitByte = liCommandCorrection + liUnitByte 
     485 
     486    local lsResultstrA = string.format("%02X", bitw.band(bitw.bnot(liHouseByte),0x7fffffff)) 
     487    local lsResultstrB = string.format("%02X", bitw.band(bitw.bnot(liUnitByte),0x7fffffff)) 
     488    local lsResultstr = string.format("%02X%02X%s%02X%s", 0x20, liHouseByte, string.sub(lsResultstrA,7,8), liUnitByte, string.sub(lsResultstrB,7,8)) 
     489    return lsResultstr 
     490  end 
     491 
     492  ------------------------------------------------------------------------------------       
     493  -- decode X10 commands 
     494  ------------------------------------------------------------------------------------       
     495  function decodeX10(psCommand) 
     496    local lsCommand = string.sub( psCommand, 2) 
    485497    local liCommandLength = string.len(lsCommand) 
    486498    local lsRFXMSG = "" 
    487         local lsSensorManufacturer = RFXConstants.sensorManufacturerX10 
    488         local lnSensorType = RFXConstants.sensorTypeControler 
    489         local lsID = "" 
    490         local lsCmd = "" 
     499    local lsSensorManufacturer = RFXConstants.sensorManufacturerX10 
     500    local lnSensorType = RFXConstants.sensorTypeControler 
     501    local lsID = "" 
     502    local lsCmd = "" 
    491503 
    492504    luup.log("trying x10") 
     
    541553    end 
    542554 
    543         return lsRFXMSG,lnSensorType,lsID,lsCmd,lsSensorManufacturer 
    544     end 
    545      
    546     ------------------------------------------------------------------------------------             
    547     -- decode homeeasy / kaku commands 
    548     ------------------------------------------------------------------------------------             
    549     function decodeHomeEasy36(psCommand) 
    550         local lsSensorManufacturer = RFXConstants.sensorManufacturerHomeEasy 
    551         local lnSensorType = RFXConstants.sensorTypeControler 
    552         local lsID = "" 
    553         local lsCmd = "" 
    554          
    555         -- 22 03 A1 C2 8B C0  ->   
    556         -- Make ID as received bytes minus command 
     555    return lsRFXMSG,lnSensorType,lsID,lsCmd,lsSensorManufacturer 
     556  end 
     557   
     558  ------------------------------------------------------------------------------------       
     559  -- decode homeeasy / kaku commands 
     560  ------------------------------------------------------------------------------------       
     561  function decodeHomeEasy36(psCommand) 
     562    local lsSensorManufacturer = RFXConstants.sensorManufacturerHomeEasy 
     563    local lnSensorType = RFXConstants.sensorTypeControler 
     564    local lsID = "" 
     565    local lsCmd = "" 
     566     
     567    -- 22 03 A1 C2 8B C0  ->   
     568    -- Make ID as received bytes minus command 
    557569    lsID = lsID .. string.format("%02X", bitw.rshift( string.byte(psCommand, 2), 6)) 
    558570    lsID = lsID .. string.format("%02X", bitw.lshift( bitw.band(string.byte(psCommand, 2), 0x3F), 2) + bitw.rshift(string.byte(psCommand, 3), 6)) 
     
    561573    -- add button id to device. 
    562574    lsID = "HEA" .. lsID .. string.format("%01X", bitw.band( string.byte(psCommand, 5), 0x0F ) ) 
    563                              
    564         -- Extract on/off/group on/group off command 
    565         local lnDataM = bitw.rshift( bitw.band( string.byte(psCommand, 5) , 0x30 ), 4 ) 
    566         if (lnDataM == 0) then lsCmd = RFXConstants.cmdOff end 
    567         if (lnDataM == 1) then lsCmd = RFXConstants.cmdOn end 
    568         if (lnDataM == 2) then lsCmd = RFXConstants.cmdGroupOff end 
    569         if (lnDataM == 3) then lsCmd = RFXConstants.cmdGroupOn end 
    570          
    571         -- Make message 
    572         lsRFXMSG = string.format ("[%s:%d ID: %s CMD: %d]", lsSensorManufacturer,lnSensorType, lsID, lsCmd) 
    573          
    574         return lsRFXMSG,lnSensorType,lsID,lsCmd,lsSensorManufacturer 
    575     end 
    576  
    577     function decodeHomeEasy34(psCommand) 
    578         local lsSensorManufacturer = RFXConstants.sensorManufacturerHomeEasy 
    579         local lnSensorType = RFXConstants.sensorTypeControler 
    580         local lsID = "" 
    581         local lsCmd = "" 
    582                  
    583         lsRFXMSG,lnSensorType,lsID,lsCmd,lsSensorManufacturer = decodeHomeEasy36(psCommand) 
    584         return lsRFXMSG,lnSensorType,lsID,lsCmd,lsSensorManufacturer 
    585     end 
    586          
    587     ------------------------------------------------------------------------------------             
    588     -- decode Visonic commands 
    589     ------------------------------------------------------------------------------------             
    590     function decodeVisonic( psCommand ) 
    591         local lsRFXMSG = "" 
    592         local liCommandLength = string.len(psCommand) 
    593         local lnSensorType = 0 
    594         local lsSensorManufacturer = RFXConstants.sensorManufacturerVisonic 
    595         local lsCmd = "" 
    596         local lsID = "" 
     575               
     576    -- Extract on/off/group on/group off command 
     577    local lnDataM = bitw.rshift( bitw.band( string.byte(psCommand, 5) , 0x30 ), 4 ) 
     578    if (lnDataM == 0) then lsCmd = RFXConstants.cmdOff end 
     579    if (lnDataM == 1) then lsCmd = RFXConstants.cmdOn end 
     580    if (lnDataM == 2) then lsCmd = RFXConstants.cmdGroupOff end 
     581    if (lnDataM == 3) then lsCmd = RFXConstants.cmdGroupOn end 
     582     
     583    -- Make message 
     584    lsRFXMSG = string.format ("[%s:%d ID: %s CMD: %d]", lsSensorManufacturer,lnSensorType, lsID, lsCmd) 
     585     
     586    return lsRFXMSG,lnSensorType,lsID,lsCmd,lsSensorManufacturer 
     587  end 
     588 
     589  function decodeHomeEasy34(psCommand) 
     590    local lsSensorManufacturer = RFXConstants.sensorManufacturerHomeEasy 
     591    local lnSensorType = RFXConstants.sensorTypeControler 
     592    local lsID = "" 
     593    local lsCmd = "" 
     594         
     595    lsRFXMSG,lnSensorType,lsID,lsCmd,lsSensorManufacturer = decodeHomeEasy36(psCommand) 
     596    return lsRFXMSG,lnSensorType,lsID,lsCmd,lsSensorManufacturer 
     597  end 
     598     
     599  ------------------------------------------------------------------------------------       
     600  -- decode Visonic commands 
     601  ------------------------------------------------------------------------------------       
     602  function decodeVisonic( psCommand ) 
     603    local lsRFXMSG = "" 
     604    local liCommandLength = string.len(psCommand) 
     605    local lnSensorType = 0 
     606    local lsSensorManufacturer = RFXConstants.sensorManufacturerVisonic 
     607    local lsCmd = "" 
     608    local lsID = "" 
    597609 
    598610    luup.log("trying Visonic") 
    599     luup.log("decodeV msg : " .. formattohex(psCommand)) 
     611  luup.log("decodeV msg : " .. formattohex(psCommand)) 
    600612    if ( liCommandLength >= 6 ) then 
    601613 
    602         -- First decode ID 
    603         -- lsID = "VIS" .. string.format("%02X%02X%02X-%d",string.byte(psCommand, 2),string.byte(psCommand, 3),string.byte(psCommand, 6),string.byte(psCommand,2) * 256 + string.byte(psCommand,3))  
    604         lsID = "VIS" .. string.format("%02X", string.byte(psCommand, 2) ) 
    605         local liHsAddr = 0   
    606         --liHsAddr = createhsaddr(psCommand) 
    607          
    608         --if protocol = MODEB32 then 
    609         --  lsRFXMSG = string.format(" addr: %02x%d",string.byte(psCommand, 2),liHsAddr) 
    610         --else 
    611         --  lsRFXMSG = string.format(" addr: %s ID: %d,lsDevice,liHsAddr) 
    612         --end    
    613         -- Next decode message data 
    614         if string.byte(psCommand, 4) == 0x44 then 
    615             lnSensorType = RFXConstants.sensorTypeDoor 
    616             lsCmd = RFXConstants.cmdAlert + RFXConstants.cmdTamper 
    617         elseif string.byte(psCommand, 4) == 0xC4 then 
    618             lnSensorType = RFXConstants.sensorTypeDoor 
    619             lsCmd = RFXConstants.cmdNormal + RFXConstants.cmdTamper 
    620         elseif string.byte(psCommand, 4) == 0x4 then 
    621             lnSensorType = RFXConstants.sensorTypeDoor 
    622             lsCmd = RFXConstants.cmdAlert    
    623         elseif string.byte(psCommand, 4) == 0x5 then 
    624             lnSensorType = RFXConstants.sensorTypeDoor 
    625             lsCmd = RFXConstants.cmdAlert + RFXConstants.cmdBatteryLow 
    626         elseif string.byte(psCommand, 4) == 0x84 then 
    627             lnSensorType = RFXConstants.sensorTypeDoor 
    628             lsCmd = RFXConstants.cmdNormal   
    629         elseif string.byte(psCommand, 4) == 0x85 then 
    630             lnSensorType = RFXConstants.sensorTypeDoor 
    631             lsCmd = RFXConstants.cmdNormal + RFXConstants.cmdBatteryLow 
    632         elseif string.byte(psCommand, 4) == 0x4C then 
    633             lnSensorType = RFXConstants.sensorTypeMotion 
    634             lsCmd = RFXConstants.cmdAlert + RFXConstants.cmdTamper 
    635         elseif string.byte(psCommand, 4) == 0xCC then 
    636             lnSensorType = RFXConstants.sensorTypeMotion 
    637             lsCmd = RFXConstants.cmdNormal + RFXConstants.cmdTamper 
    638         elseif string.byte(psCommand, 4) == 0x0C then 
    639             lnSensorType = RFXConstants.sensorTypeMotion 
    640             lsCmd = RFXConstants.cmdAlert 
    641         elseif string.byte(psCommand, 4) == 0x0D then 
    642             lnSensorType = RFXConstants.sensorTypeMotion 
    643             lsCmd = RFXConstants.cmdAlert + RFXConstants.cmdBatteryLow 
    644         elseif string.byte(psCommand, 4) == 0x8C then 
    645             lnSensorType = RFXConstants.sensorTypeMotion 
    646             lsCmd = RFXConstants.cmdNormal 
    647         elseif string.byte(psCommand, 4) == 0x8D then 
    648             lnSensorType = RFXConstants.sensorTypeMotion 
    649             lsCmd = RFXConstants.cmdNormal + RFXConstants.cmdBatteryLow 
    650         elseif string.byte(psCommand, 4) == 0xE0 then 
    651             if string.byte(psCommand, 2) == 0xff then 
    652                 lsCmd = RFXConstants.cmdJamming -- Master receiver jamming detected 
    653             elseif string.byte(psCommand, 2) == 0x0 then 
    654                 lsCmd = RFXConstants.cmdJamming -- Slave receiver jamming detected 
    655             else 
    656                 -- Unknown data packet received 
    657             end 
    658         elseif string.byte(psCommand, 4) == 0xF8 then 
    659             if string.byte(psCommand, 2) == 0xff then 
    660                 lsCmd = RFXConstants.cmdJamming -- Master receiver jamming detected 
    661             elseif string.byte(psCommand, 2) == 0x0 then 
    662                 lsCmd = RFXConstants.cmdJamming -- Slave receiver jamming detected 
    663             else 
    664                 -- Unknown command 
    665             end 
    666          
    667         -- Marmitek KR21 code 
    668         elseif string.byte(psCommand, 4) == 0x6 then 
    669             lnSensorType = RFXConstants.sensorTypeKeyFob 
    670             lsCmd = RFXConstants.cmdArmAway 
    671         elseif string.byte(psCommand, 4) == 0x86 then 
    672             lnSensorType = RFXConstants.sensorTypeKeyFob 
    673             lsCmd = RFXConstants.cmdDisarm   
    674         elseif string.byte(psCommand, 4) == 0x46 then 
    675             lnSensorType = RFXConstants.sensorTypeKeyFob 
    676             lsCmd = RFXConstants.cmdOn 
    677         elseif string.byte(psCommand, 4) == 0xC6 then 
    678             lnSensorType = RFXConstants.sensorTypeKeyFob 
    679             lsCmd = RFXConstants.cmdOff 
    680              
    681         elseif string.byte(psCommand, 4) == 0x2 then 
    682             lnSensorType = RFXConstants.sensorTypeKeyFob 
    683             lsCmd = RFXConstants.cmdArmAway  
    684         elseif string.byte(psCommand, 4) == 0xE then 
    685             lnSensorType = RFXConstants.sensorTypeKeyFob 
    686             lsCmd = RFXConstants.cmdArmHome 
    687         elseif string.byte(psCommand, 4) == 0x22 then 
    688             lnSensorType = RFXConstants.sensorTypeKeyFob 
    689             lsCmd = RFXConstants.cmdPanic    
    690         elseif string.byte(psCommand, 4) == 0x42 then 
    691             lnSensorType = RFXConstants.sensorTypeKeyFob 
    692             lsCmd = RFXConstants.cmdGroupOn  
    693         elseif string.byte(psCommand, 4) == 0x82 then 
    694             lnSensorType = RFXConstants.sensorTypeKeyFob 
    695             lsCmd = RFXConstants.cmdDisarm 
     614      -- First decode ID 
     615      -- lsID = "VIS" .. string.format("%02X%02X%02X-%d",string.byte(psCommand, 2),string.byte(psCommand, 3),string.byte(psCommand, 6),string.byte(psCommand,2) * 256 + string.byte(psCommand,3))  
     616    lsID = "VIS" .. string.format("%02X", string.byte(psCommand, 2) ) 
     617      local liHsAddr = 0   
     618      --liHsAddr = createhsaddr(psCommand) 
     619     
     620      --if protocol = MODEB32 then 
     621      --  lsRFXMSG = string.format(" addr: %02x%d",string.byte(psCommand, 2),liHsAddr) 
     622      --else 
     623      --  lsRFXMSG = string.format(" addr: %s ID: %d,lsDevice,liHsAddr) 
     624      --end  
     625    -- Next decode message data 
     626      if string.byte(psCommand, 4) == 0x44 then 
     627        lnSensorType = RFXConstants.sensorTypeDoor 
     628        lsCmd = RFXConstants.cmdAlert + RFXConstants.cmdTamper 
     629      elseif string.byte(psCommand, 4) == 0xC4 then 
     630        lnSensorType = RFXConstants.sensorTypeDoor 
     631        lsCmd = RFXConstants.cmdNormal + RFXConstants.cmdTamper 
     632      elseif string.byte(psCommand, 4) == 0x4 then 
     633        lnSensorType = RFXConstants.sensorTypeDoor 
     634        lsCmd = RFXConstants.cmdAlert  
     635      elseif string.byte(psCommand, 4) == 0x5 then 
     636        lnSensorType = RFXConstants.sensorTypeDoor 
     637        lsCmd = RFXConstants.cmdAlert + RFXConstants.cmdBatteryLow 
     638      elseif string.byte(psCommand, 4) == 0x84 then 
     639        lnSensorType = RFXConstants.sensorTypeDoor 
     640        lsCmd = RFXConstants.cmdNormal   
     641      elseif string.byte(psCommand, 4) == 0x85 then 
     642        lnSensorType = RFXConstants.sensorTypeDoor 
     643        lsCmd = RFXConstants.cmdNormal + RFXConstants.cmdBatteryLow 
     644      elseif string.byte(psCommand, 4) == 0x4C then 
     645        lnSensorType = RFXConstants.sensorTypeMotion 
     646        lsCmd = RFXConstants.cmdAlert + RFXConstants.cmdTamper 
     647      elseif string.byte(psCommand, 4) == 0xCC then 
     648        lnSensorType = RFXConstants.sensorTypeMotion 
     649        lsCmd = RFXConstants.cmdNormal + RFXConstants.cmdTamper 
     650      elseif string.byte(psCommand, 4) == 0x0C then 
     651        lnSensorType = RFXConstants.sensorTypeMotion 
     652        lsCmd = RFXConstants.cmdAlert 
     653      elseif string.byte(psCommand, 4) == 0x0D then 
     654        lnSensorType = RFXConstants.sensorTypeMotion 
     655        lsCmd = RFXConstants.cmdAlert + RFXConstants.cmdBatteryLow 
     656      elseif string.byte(psCommand, 4) == 0x8C then 
     657        lnSensorType = RFXConstants.sensorTypeMotion 
     658        lsCmd = RFXConstants.cmdNormal 
     659      elseif string.byte(psCommand, 4) == 0x8D then 
     660        lnSensorType = RFXConstants.sensorTypeMotion 
     661        lsCmd = RFXConstants.cmdNormal + RFXConstants.cmdBatteryLow 
     662      elseif string.byte(psCommand, 4) == 0xE0 then 
     663        if string.byte(psCommand, 2) == 0xff then 
     664          lsCmd = RFXConstants.cmdJamming -- Master receiver jamming detected 
     665        elseif string.byte(psCommand, 2) == 0x0 then 
     666          lsCmd = RFXConstants.cmdJamming -- Slave receiver jamming detected 
    696667        else 
    697             -- Unknown data packet received 
    698         end 
    699          
    700         -- Create RFXMSG text 
    701         lsRFXMSG = string.format ("[%s:%d ID: %s CMD: %s]", lsSensorManufacturer,lnSensorType, lsID, lsCmd) 
    702         end 
    703      
    704         return lsRFXMSG,lnSensorType,lsID,lsCmd,lsSensorManufacturer 
    705     end 
    706      
    707      
     668          -- Unknown data packet received 
     669        end 
     670      elseif string.byte(psCommand, 4) == 0xF8 then 
     671        if string.byte(psCommand, 2) == 0xff then 
     672          lsCmd = RFXConstants.cmdJamming -- Master receiver jamming detected 
     673        elseif string.byte(psCommand, 2) == 0x0 then 
     674          lsCmd = RFXConstants.cmdJamming -- Slave receiver jamming detected 
     675        else 
     676          -- Unknown command 
     677        end 
     678     
     679    -- Marmitek KR21 code 
     680    elseif string.byte(psCommand, 4) == 0x6 then 
     681        lnSensorType = RFXConstants.sensorTypeKeyFob 
     682        lsCmd = RFXConstants.cmdArmAway 
     683    elseif string.byte(psCommand, 4) == 0x86 then 
     684        lnSensorType = RFXConstants.sensorTypeKeyFob 
     685        lsCmd = RFXConstants.cmdDisarm   
     686    elseif string.byte(psCommand, 4) == 0x46 then 
     687        lnSensorType = RFXConstants.sensorTypeKeyFob 
     688        lsCmd = RFXConstants.cmdOn 
     689    elseif string.byte(psCommand, 4) == 0xC6 then 
     690        lnSensorType = RFXConstants.sensorTypeKeyFob 
     691        lsCmd = RFXConstants.cmdOff 
     692       
     693    elseif string.byte(psCommand, 4) == 0x2 then 
     694        lnSensorType = RFXConstants.sensorTypeKeyFob 
     695        lsCmd = RFXConstants.cmdArmAway  
     696      elseif string.byte(psCommand, 4) == 0xE then 
     697        lnSensorType = RFXConstants.sensorTypeKeyFob 
     698        lsCmd = RFXConstants.cmdArmHome 
     699      elseif string.byte(psCommand, 4) == 0x22 then 
     700        lnSensorType = RFXConstants.sensorTypeKeyFob 
     701        lsCmd = RFXConstants.cmdPanic  
     702      elseif string.byte(psCommand, 4) == 0x42 then 
     703        lnSensorType = RFXConstants.sensorTypeKeyFob 
     704        lsCmd = RFXConstants.cmdGroupOn  
     705    elseif string.byte(psCommand, 4) == 0x82 then 
     706        lnSensorType = RFXConstants.sensorTypeKeyFob 
     707        lsCmd = RFXConstants.cmdDisarm 
     708        else 
     709        -- Unknown data packet received 
     710      end 
     711     
     712      -- Create RFXMSG text 
     713      lsRFXMSG = string.format ("[%s:%d ID: %s CMD: %s]", lsSensorManufacturer,lnSensorType, lsID, lsCmd) 
     714    end 
     715     
     716    return lsRFXMSG,lnSensorType,lsID,lsCmd,lsSensorManufacturer 
     717  end 
     718   
     719   
    708720    function createhsaddr(psCommand) 
    709721        local liHsAddr = 0 
     
    757769            liHsAddr = bitw.bor(liHsAddr,0x100) 
    758770        end 
    759          
     771     
    760772        return liHsAddr 
    761773    end 
     
    763775     
    764776  function decodeOregon(psCommand) 
    765     local lsCommand = string.sub( psCommand, 2) 
    766     local lsRFXMSG = "" 
    767     local lnSensorType = 0 
    768     local lsID = "" 
    769     local lsCmd = "" 
    770     local lsSensorManufacturer = "" 
    771     local liBitLength = bitw.band( string.byte(psCommand, 1), 0x7f) 
    772     if ( ( string.byte(lsCommand, 1) == 0x3a ) and ( string.byte(lsCommand, 2) == 0x0d ) ) then 
    773       luup.log("Identified Oregon WGR918") 
    774            
    775       lnSensorType = RFXConstants.sensorTypeWindDirection + RFXConstants.sensorTypeWindSpeed + RFXConstants.sensorTypeWindSpeedAv 
     777  local lsCommand = string.sub( psCommand, 2) 
     778  local lsRFXMSG = "" 
     779  local lnSensorType = 0 
     780  local lsID = "" 
     781  local lsCmd = "" 
     782  local lsSensorManufacturer = "" 
     783  local liBitLength = bitw.band( string.byte(psCommand, 1), 0x7f) 
     784  if ( ( string.byte(lsCommand, 1) == 0x3a ) and ( string.byte(lsCommand, 2) == 0x0d ) ) then 
     785    luup.log("Identified Oregon WGR918") 
     786           
     787    lnSensorType = RFXConstants.sensorTypeWindDirection + RFXConstants.sensorTypeWindSpeed + RFXConstants.sensorTypeWindSpeedAv 
    776788      lsSensorManufacturer = RFXConstants.sensorManufacturerOregon 
    777789      local lnDataDir = (string.byte(lsCommand, 6) * 10 + bitw.rshift(string.byte(lsCommand, 5), 4) ) / 10 
     
    779791      local lnDataSpeedav = string.byte(lsCommand, 9) + ( bitw.rshift( string.byte(lsCommand, 8), 4) / 10) 
    780792       
    781       lsID = "ORE" .. string.format("%02X", string.byte(lsCommand, 4) ) 
    782       lsCmd = string.format("direction=%.1f;speed=%.2f;speedav=%.2f",lnDataDir, lnDataSpeed, lnDataSpeedav) 
     793    lsID = "ORE" .. string.format("%02X", string.byte(lsCommand, 4) ) 
     794    lsCmd = string.format("direction=%.1f;speed=%.2f;speedav=%.2f",lnDataDir, lnDataSpeed, lnDataSpeedav) 
    783795      lsRFXMSG = string.format("[OREGON: WIND3 Device: %s Direction: %.1f Speed: %.2f Speed av: %.2f]", lsID, lnDataDir, lnDataSpeed, lnDataSpeedav) 
    784796 
    785     elseif ( ( string.byte(lsCommand, 1) == 0x1A ) and ( string.byte(lsCommand, 2) == 0x2d ) ) then 
    786         luup.log("Identified Oregon THGN122N,THGR122NX,THGR228N,THGR268") 
    787  
    788         lnSensorType = RFXConstants.sensorTypeTemperature + RFXConstants.sensorTypeHumidity  
    789         lsSensorManufacturer = RFXConstants.sensorManufacturerOregon 
    790          
    791         local lnDataTemp = getOregonTemperature(lsCommand) 
    792         local lnDataBattery = getOregonBattery(lsCommand) 
    793         local lnDataHum = getOregonHumidity(lsCommand) 
    794        
    795         lsID = "ORE" .. string.format("%02X", string.byte(lsCommand, 4) ) 
    796         lsCmd = string.format("temperature=%.1f;humidity=%d;battery=%d", lnDataTemp, lnDataHum, lnDataBattery) 
    797         lsRFXMSG = string.format("[OREGON: Temp1 Device: %s Temperature: %5.1f Humidity: %d Battery: %d]", lsID, lnDataTemp, lnDataHum, lnDataBattery) 
    798          
    799         -- Support for Oregon THWR800 ca48 68 
    800     elseif ( ( string.byte(lsCommand, 1) == 0xca ) and ( string.byte(lsCommand, 2) == 0x48 ) ) then 
    801         luup.log("Identified Oregon THWR800") 
    802  
    803         lnSensorType = RFXConstants.sensorTypeTemperature 
    804         lsSensorManufacturer = RFXConstants.sensorManufacturerOregon 
    805          
    806         local lnDataTemp = getOregonTemperature(lsCommand) 
    807         local lnDataBattery = getOregonBattery(lsCommand) 
    808                
    809         lsID = "ORE" .. string.format("%02X", string.byte(lsCommand, 4) ) 
    810         lsCmd = string.format("temperature=%.1f;battery=%d", lnDataTemp, lnDataBattery) 
    811         lsRFXMSG = string.format("[OREGON: Temperature Device: %s Temperature: %.1f Battery: %d]", lsID, lnDataTemp, lnDataBattery) 
    812  
    813         -- Support for Oregon THGR810 fa28 80 
    814     elseif ( ( string.byte(lsCommand, 1) == 0xfa ) and ( string.byte(lsCommand, 2) == 0x28 ) ) then 
    815         luup.log("Identified Oregon THGR810") 
    816         lnSensorType = RFXConstants.sensorTypeTemperature + RFXConstants.sensorTypeHumidity  
    817         lsSensorManufacturer = RFXConstants.sensorManufacturerOregon 
    818          
    819         local lnDataTemp = getOregonTemperature(lsCommand) 
    820         local lnDataBattery = getOregonBattery(lsCommand) 
    821         local lnDataHum = getOregonHumidity(lsCommand) 
    822          
    823         lsID = "ORE" .. string.format("%02X", string.byte(lsCommand, 4) ) 
    824         lsCmd = string.format("temperature=%.1f;humidity=%d;battery=%d", lnDataTemp, lnDataHum, lnDataBattery) 
    825         lsRFXMSG = string.format("[OREGON: Temp1 Device: %s Temperature: %.1f Humidity: %d Battery: %d]", lsID, lnDataTemp, lnDataHum, lnDataBattery) 
    826          
    827         -- Support for Oregon THWR288A ea4c 68 
    828     elseif ( ( string.byte(lsCommand, 1) == 0xea ) and ( string.byte(lsCommand, 2) == 0x4c ) ) then 
    829         luup.log("Identified Oregon THWR288A") 
    830         lnSensorType = RFXConstants.sensorTypeTemperature + RFXConstants.sensorTypeHumidity  
    831         lsSensorManufacturer = RFXConstants.sensorManufacturerOregon 
    832          
    833         local lnDataTemp = getOregonTemperature(lsCommand) 
    834         local lnDataBattery = getOregonBattery(lsCommand) 
    835         local lnDataHum = getOregonHumidity(lsCommand) 
    836          
    837         lsID = "ORE" .. string.format("%02X", string.byte(lsCommand, 4) ) 
    838         lsCmd = string.format("temperature=%.1f;humidity=%d;battery=%d", lnDataTemp, lnDataHum, lnDataBattery) 
    839         lsRFXMSG = string.format("[OREGON: Temp1 Device: %s Temperature: %.1f Humidity: %d Battery: %d]", lsID, lnDataTemp, lnDataHum, lnDataBattery) 
    840      
    841      
    842     -- Support for Oregon TH3 Oregon RTGR328N  
    843     elseif ( ( bitw.band(string.byte(lsCommand, 1), 0x0f) == 0x0a ) and ( string.byte(lsCommand, 2) == 0xcc ) ) then 
    844         luup.log("Identified Oregon TH3 RTGR328N ") 
    845         lnSensorType = RFXConstants.sensorTypeTemperature + RFXConstants.sensorTypeHumidity  
    846         lsSensorManufacturer = RFXConstants.sensorManufacturerOregon 
    847          
    848         local lnDataTemp = getOregonTemperature(lsCommand) 
    849         local lnDataBattery = getOregonBattery(lsCommand) 
    850         local lnDataHum = getOregonHumidity(lsCommand) 
    851          
    852         lsID = "ORE" .. string.format("%02X", string.byte(lsCommand, 4) ) 
    853         lsCmd = string.format("temperature=%.1f;humidity=%d;battery=%d", lnDataTemp, lnDataHum, lnDataBattery) 
    854         lsRFXMSG = string.format("[OREGON: Temp1 Device: %s Temperature: %.1f Humidity: %d Battery: %d]", lsID, lnDataTemp, lnDataHum, lnDataBattery) 
    855  
    856     -- Support for Oregon TH4 Oregon THGR328 
    857     elseif ( ( string.byte(lsCommand, 1) == 0xca ) and ( string.byte(lsCommand, 2) == 0x2c ) ) then 
    858         luup.log("Identified Oregon TH4 RTGR328 ") 
    859         lnSensorType = RFXConstants.sensorTypeTemperature + RFXConstants.sensorTypeHumidity  
    860         lsSensorManufacturer = RFXConstants.sensorManufacturerOregon 
    861          
    862         local lnDataTemp = getOregonTemperature(lsCommand) 
    863         local lnDataBattery = getOregonBattery(lsCommand) 
    864         local lnDataHum = getOregonHumidity(lsCommand) 
    865          
    866         lsID = "ORE" .. string.format("%02X", string.byte(lsCommand, 4) ) 
    867         lsCmd = string.format("temperature=%.1f;humidity=%d;battery=%d", lnDataTemp, lnDataHum, lnDataBattery) 
    868         lsRFXMSG = string.format("[OREGON: Temp1 Device: %s Temperature: %.1f Humidity: %d Battery: %d]", lsID, lnDataTemp, lnDataHum, lnDataBattery) 
    869          
    870     -- Support for Oregon WTGR800 fab8 80 
    871     elseif ( ( string.byte(lsCommand, 1) == 0xfa ) and ( string.byte(lsCommand, 2) == 0xb8 ) ) then 
    872         luup.log("Identified Oregon WTGR800") 
    873         lnSensorType = RFXConstants.sensorTypeTemperature + RFXConstants.sensorTypeHumidity  
    874         lsSensorManufacturer = RFXConstants.sensorManufacturerOregon 
    875          
    876         local lnDataTemp = getOregonTemperature(lsCommand) 
    877         local lnDataPercentBattery = getOregonPercentBattery(lsCommand) 
    878         local lnDataHum = getOregonHumidity(lsCommand) 
    879          
    880         lsID = "ORF" .. string.format("%02X", string.byte(lsCommand, 4) ) 
    881         lsCmd = string.format("temperature=%.1f;humidity=%d;battery=%d", lnDataTemp, lnDataHum, lnDataPercentBattery) 
    882         lsRFXMSG = string.format("[OREGON: Temperature Device: %s Temperature: %.1f Humidity: %d  Battery: %d]", lsID, lnDataTemp, lnDataHum, lnDataPercentBattery) 
    883          
    884     -- Support for Oregon WTGR800 1A99 88 
    885     elseif ( ( string.byte(lsCommand, 1) == 0x1a ) and ( string.byte(lsCommand, 2) == 0x99 ) ) then 
    886         luup.log("Identified Oregon Anemo WTGR800") 
    887      
    888         lnSensorType = RFXConstants.sensorTypeWindDirection 
     797  elseif ( ( string.byte(lsCommand, 1) == 0x1A ) and ( string.byte(lsCommand, 2) == 0x2d ) ) then 
     798    luup.log("Identified Oregon THGN122N,THGR122NX,THGR228N,THGR268") 
     799 
     800    lnSensorType = RFXConstants.sensorTypeTemperature + RFXConstants.sensorTypeHumidity  
     801    lsSensorManufacturer = RFXConstants.sensorManufacturerOregon 
     802     
     803    local lnDataTemp = getOregonTemperature(lsCommand) 
     804    local lnDataBattery = getOregonBattery(lsCommand) 
     805    local lnDataHum = getOregonHumidity(lsCommand) 
     806     
     807    lsID = "ORE" .. string.format("%02X", string.byte(lsCommand, 4) ) 
     808      lsCmd = string.format("temperature=%.1f;humidity=%d;battery=%d", lnDataTemp, lnDataHum, lnDataBattery) 
     809    lsRFXMSG = string.format("[OREGON: Temp1 Device: %s Temperature: %5.1f Humidity: %d Battery: %d]", lsID, lnDataTemp, lnDataHum, lnDataBattery) 
     810     
     811    -- Support for Oregon THWR800 ca48 68 
     812  elseif ( ( string.byte(lsCommand, 1) == 0xca ) and ( string.byte(lsCommand, 2) == 0x48 ) ) then 
     813    luup.log("Identified Oregon THWR800") 
     814 
     815    lnSensorType = RFXConstants.sensorTypeTemperature 
     816    lsSensorManufacturer = RFXConstants.sensorManufacturerOregon 
     817     
     818    local lnDataTemp = getOregonTemperature(lsCommand) 
     819    local lnDataBattery = getOregonBattery(lsCommand) 
     820         
     821    lsID = "ORE" .. string.format("%02X", string.byte(lsCommand, 4) ) 
     822      lsCmd = string.format("temperature=%.1f;battery=%d", lnDataTemp, lnDataBattery) 
     823    lsRFXMSG = string.format("[OREGON: Temperature Device: %s Temperature: %.1f Battery: %d]", lsID, lnDataTemp, lnDataBattery) 
     824 
     825    -- Support for Oregon THGR810 fa28 80 
     826  elseif ( ( string.byte(lsCommand, 1) == 0xfa ) and ( string.byte(lsCommand, 2) == 0x28 ) ) then 
     827    luup.log("Identified Oregon THGR810") 
     828    lnSensorType = RFXConstants.sensorTypeTemperature + RFXConstants.sensorTypeHumidity  
     829    lsSensorManufacturer = RFXConstants.sensorManufacturerOregon 
     830     
     831    local lnDataTemp = getOregonTemperature(lsCommand) 
     832    local lnDataBattery = getOregonBattery(lsCommand) 
     833    local lnDataHum = getOregonHumidity(lsCommand) 
     834     
     835    lsID = "ORE" .. string.format("%02X", string.byte(lsCommand, 4) ) 
     836      lsCmd = string.format("temperature=%.1f;humidity=%d;battery=%d", lnDataTemp, lnDataHum, lnDataBattery) 
     837    lsRFXMSG = string.format("[OREGON: Temp1 Device: %s Temperature: %.1f Humidity: %d Battery: %d]", lsID, lnDataTemp, lnDataHum, lnDataBattery) 
     838     
     839    -- Support for Oregon THWR288A ea4c 68 
     840  elseif ( ( string.byte(lsCommand, 1) == 0xea ) and ( string.byte(lsCommand, 2) == 0x4c ) ) then 
     841    luup.log("Identified Oregon THWR288A") 
     842    lnSensorType = RFXConstants.sensorTypeTemperature + RFXConstants.sensorTypeHumidity  
     843    lsSensorManufacturer = RFXConstants.sensorManufacturerOregon 
     844     
     845    local lnDataTemp = getOregonTemperature(lsCommand) 
     846    local lnDataBattery = getOregonBattery(lsCommand) 
     847    local lnDataHum = getOregonHumidity(lsCommand) 
     848     
     849    lsID = "ORE" .. string.format("%02X", string.byte(lsCommand, 4) ) 
     850      lsCmd = string.format("temperature=%.1f;humidity=%d;battery=%d", lnDataTemp, lnDataHum, lnDataBattery) 
     851    lsRFXMSG = string.format("[OREGON: Temp1 Device: %s Temperature: %.1f Humidity: %d Battery: %d]", lsID, lnDataTemp, lnDataHum, lnDataBattery) 
     852   
     853   
     854  -- Support for Oregon TH3 Oregon RTGR328N  
     855  elseif ( ( bitw.band(string.byte(lsCommand, 1), 0x0f) == 0x0a ) and ( string.byte(lsCommand, 2) == 0xcc ) ) then 
     856    luup.log("Identified Oregon TH3 RTGR328N ") 
     857    lnSensorType = RFXConstants.sensorTypeTemperature + RFXConstants.sensorTypeHumidity  
     858    lsSensorManufacturer = RFXConstants.sensorManufacturerOregon 
     859     
     860    local lnDataTemp = getOregonTemperature(lsCommand) 
     861    local lnDataBattery = getOregonBattery(lsCommand) 
     862    local lnDataHum = getOregonHumidity(lsCommand) 
     863     
     864    lsID = "ORE" .. string.format("%02X", string.byte(lsCommand, 4) ) 
     865      lsCmd = string.format("temperature=%.1f;humidity=%d;battery=%d", lnDataTemp, lnDataHum, lnDataBattery) 
     866    lsRFXMSG = string.format("[OREGON: Temp1 Device: %s Temperature: %.1f Humidity: %d Battery: %d]", lsID, lnDataTemp, lnDataHum, lnDataBattery) 
     867 
     868  -- Support for Oregon TH4 Oregon THGR328 
     869  elseif ( ( string.byte(lsCommand, 1) == 0xca ) and ( string.byte(lsCommand, 2) == 0x2c ) ) then 
     870    luup.log("Identified Oregon TH4 RTGR328 ") 
     871    lnSensorType = RFXConstants.sensorTypeTemperature + RFXConstants.sensorTypeHumidity  
     872    lsSensorManufacturer = RFXConstants.sensorManufacturerOregon 
     873     
     874    local lnDataTemp = getOregonTemperature(lsCommand) 
     875    local lnDataBattery = getOregonBattery(lsCommand) 
     876    local lnDataHum = getOregonHumidity(lsCommand) 
     877     
     878    lsID = "ORE" .. string.format("%02X", string.byte(lsCommand, 4) ) 
     879      lsCmd = string.format("temperature=%.1f;humidity=%d;battery=%d", lnDataTemp, lnDataHum, lnDataBattery) 
     880    lsRFXMSG = string.format("[OREGON: Temp1 Device: %s Temperature: %.1f Humidity: %d Battery: %d]", lsID, lnDataTemp, lnDataHum, lnDataBattery) 
     881     
     882  -- Support for Oregon WTGR800 fab8 80 
     883  elseif ( ( string.byte(lsCommand, 1) == 0xfa ) and ( string.byte(lsCommand, 2) == 0xb8 ) ) then 
     884    luup.log("Identified Oregon WTGR800") 
     885    lnSensorType = RFXConstants.sensorTypeTemperature + RFXConstants.sensorTypeHumidity  
     886    lsSensorManufacturer = RFXConstants.sensorManufacturerOregon 
     887     
     888    local lnDataTemp = getOregonTemperature(lsCommand) 
     889    local lnDataPercentBattery = getOregonPercentBattery(lsCommand) 
     890    local lnDataHum = getOregonHumidity(lsCommand) 
     891     
     892    lsID = "ORF" .. string.format("%02X", string.byte(lsCommand, 4) ) 
     893      lsCmd = string.format("temperature=%.1f;humidity=%d;battery=%d", lnDataTemp, lnDataHum, lnDataPercentBattery) 
     894    lsRFXMSG = string.format("[OREGON: Temperature Device: %s Temperature: %.1f Humidity: %d  Battery: %d]", lsID, lnDataTemp, lnDataHum, lnDataPercentBattery) 
     895     
     896  -- Support for Oregon WTGR800 1A99 88 
     897  elseif ( ( string.byte(lsCommand, 1) == 0x1a ) and ( string.byte(lsCommand, 2) == 0x99 ) ) then 
     898    luup.log("Identified Oregon Anemo WTGR800") 
     899   
     900    lnSensorType = RFXConstants.sensorTypeWindDirection 
    889901    -- + RFXConstants.sensorTypeWindSpeed + RFXConstants.sensorTypeWindSpeedAv 
    890         lsSensorManufacturer = RFXConstants.sensorManufacturerOregon 
    891  
    892         local liDataDir = nib(lsCommand,8) * 22.5 
    893         local lnDataSpeed = nib(lsCommand,15)*10 + nib(lsCommand,12) + nib(lsCommand,13)/10 
    894         local lnDataSpeedav = nib(lsCommand,16)*10 + nib(lsCommand,17) + nib(lsCommand,14)/10 
    895         local lnDataPercentBattery = getOregonPercentBattery(lsCommand) 
    896  
    897         --conversion mps en kmh 
    898         --lnDataSpeed = lnDataSpeed * 3.6 
    899         --lnDataSpeedav = lnDataSpeedav * 3.6 
    900  
    901         lsID = "ORE" .. string.format("%02X", string.byte(lsCommand, 4) ) 
    902         lsCmd = string.format("direction=%.2f;speed=%.2f;speedav=%.2f;battery=%d",liDataDir, lnDataSpeed, lnDataSpeedav, lnDataPercentBattery) 
    903         lsRFXMSG = string.format("[OREGON: Anemometer Device: %s Direction: %.1f  Speed: %.1f mps  Speed av: %.1f mps  Battery: %d]", lsID, liDataDir, lnDataSpeed, lnDataSpeedav, lnDataPercentBattery) 
    904          
     902    lsSensorManufacturer = RFXConstants.sensorManufacturerOregon 
     903 
     904    local liDataDir = nib(lsCommand,8) * 22.5 
     905    local lnDataSpeed = nib(lsCommand,15)*10 + nib(lsCommand,12) + nib(lsCommand,13)/10 
     906    local lnDataSpeedav = nib(lsCommand,16)*10 + nib(lsCommand,17) + nib(lsCommand,14)/10 
     907    local lnDataPercentBattery = getOregonPercentBattery(lsCommand) 
     908 
     909    --conversion mps en kmh 
     910    --lnDataSpeed = lnDataSpeed * 3.6 
     911    --lnDataSpeedav = lnDataSpeedav * 3.6 
     912 
     913    lsID = "ORE" .. string.format("%02X", string.byte(lsCommand, 4) ) 
     914    lsCmd = string.format("direction=%.2f;speed=%.2f;speedav=%.2f;battery=%d",liDataDir, lnDataSpeed, lnDataSpeedav, lnDataPercentBattery) 
     915    lsRFXMSG = string.format("[OREGON: Anemometer Device: %s Direction: %.1f  Speed: %.1f mps  Speed av: %.1f mps  Battery: %d]", lsID, liDataDir, lnDataSpeed, lnDataSpeedav, lnDataPercentBattery) 
     916     
    905917         -- Support for Oregon BTHR918N 5a6d 96 
    906     elseif ( ( string.byte(lsCommand, 1) == 0x5a ) and ( string.byte(lsCommand, 2) == 0x6d ) ) then 
    907         luup.log("Identified Oregon BTHR918N") 
    908         lnSensorType = RFXConstants.sensorTypeTemperature + RFXConstants.sensorTypeHumidity + RFXConstants.sensorTypeBarometer 
    909         lsSensorManufacturer = RFXConstants.sensorManufacturerOregon 
    910          
    911         local lnDataTemp = getOregonTemperature(lsCommand) 
    912         local lnDataBattery = getOregonBattery(lsCommand) 
    913         local lnDataHum = getOregonHumidity(lsCommand) 
    914          
    915         lnDataPres = 856 + string.byte(lsCommand, 9) 
    916         lsID = "ORE" .. string.format("%02X", string.byte(lsCommand, 4) ) 
    917         lsCmd = string.format("temperature=%.1f;humidity=%d;pressure=%s;battery=%d", lnDataTemp, lnDataHum, lnDataPres, lnDataBattery) 
    918         lsRFXMSG = string.format("[OREGON: Temperature Device: %s Temperature: %.1f Humidity: %d Pressure: %s Battery: %s]", lsID, lnDataTemp, lnDataHum, lnDataPres, lnDataBattery) 
    919  
    920         -- Support for Oregon PCR800 2a19 92 
    921     elseif ( ( string.byte(lsCommand, 1) == 0x2a ) and ( string.byte(lsCommand, 2) == 0x19 ) ) then 
    922         luup.log("Identified Oregon PCR800") 
    923         lnSensorType = RFXConstants.sensorTypeRain 
    924         lsSensorManufacturer = RFXConstants.sensorManufacturerOregon 
    925         local lnDataRain = 0.0 
    926         local lnDataTRain = 0.0 
    927         local lnDataBattery = getOregonBattery(lsCommand) 
    928          
    929         lnDataRain = bitw.band(bitw.lshift(string.byte(lsCommand, 7), 0),0x0f) * 10 + bitw.band(bitw.rshift(string.byte(lsCommand, 6), 4),0x0f)  
    930           + bitw.band(bitw.lshift(string.byte(lsCommand, 6), 0),0x0f) / 10 + bitw.band(bitw.rshift(string.byte(lsCommand, 5), 4),0x0f) / 100  
    931         lnDataRain = 25.4 * lnDataRain 
    932          
    933         lnDataTRain = bitw.band(bitw.lshift(string.byte(lsCommand, 10), 0),0x0f) * 1000 + bitw.band(bitw.rshift(string.byte(lsCommand, 9), 4),0x0f) * 10  
    934           + bitw.band(bitw.lshift(string.byte(lsCommand, 9), 0),0x0f) + bitw.band(bitw.rshift(string.byte(lsCommand, 8), 4),0x0f) / 10  
    935           + bitw.band(bitw.lshift(string.byte(lsCommand, 8), 0),0x0f) / 100 + bitw.band(bitw.rshift(string.byte(lsCommand, 7), 4),0x0f) / 1000 
    936                  
    937         lnDataTRain = 25.4 * lnDataTRain 
    938                                
    939         lsID = "ORE" .. string.format("%02X", string.byte(lsCommand, 4) ) 
    940       lsCmd = string.format("rain=%s;train=%s;battery=%d", lnDataRain, lnDataTRain, lnDataBattery) 
    941         lsRFXMSG = string.format("[OREGON: Rain Device: %s Rain: %s TRain: %s Battery: %s]", lsID, lnDataRain, lnDataTRain, lnDataBattery) 
    942          
    943         -- Support for Oregon RGR126,RGR682,RGR918 2A1D 84  :  54 2A 1D 00 F9 00 00 12 91 A5 43 04   
    944     elseif ( ( string.byte(lsCommand, 1) == 0x2a ) and ( string.byte(lsCommand, 2) == 0x1D ) ) then 
    945         luup.log("Identified Oregon RGR126,RGR682,RGR918") 
    946         lnSensorType = RFXConstants.sensorTypeRain 
    947         lsSensorManufacturer = RFXConstants.sensorManufacturerOregon 
    948         local lnDataRain = 0.0 
    949         local lnDataTRain = 0.0 
    950         local lnDataBattery = getOregonBattery(lsCommand) 
    951  
    952         lnDataRain = bitw.band(bitw.lshift(string.byte(lsCommand, 6), 0),0xff) * 10 + bitw.band(bitw.rshift(string.byte(lsCommand, 5), 4),0x0f)  
    953  
    954         lnDataTRain = bitw.band(bitw.lshift(string.byte(lsCommand, 9), 0),0x0f) * 1000 + bitw.band(bitw.rshift(string.byte(lsCommand, 8), 0),0xff) * 10  
    955           + bitw.band(bitw.rshift(string.byte(lsCommand, 7), 4),0x0f)  
    956  
    957         lsID = "ORE" .. string.format("%02X", string.byte(lsCommand, 4) ) 
    958       lsCmd = string.format("rain=%s;train=%s;battery=%d", lnDataRain, lnDataTRain, lnDataBattery) 
    959         lsRFXMSG = string.format("[OREGON: Rain Device: %s Rain: %s TRain: %s Battery: %s]", lsID, lnDataRain, lnDataTRain, lnDataBattery) 
    960          
    961     -- Support for Oregon THGR918 1A3D    
    962     elseif ( ( string.byte(lsCommand, 1) == 0x1a ) and ( string.byte(lsCommand, 2) == 0x3D ) ) then 
    963         luup.log("Identified Oregon THGR918") 
    964         lnSensorType = RFXConstants.sensorTypeTemperature + RFXConstants.sensorTypeHumidity + RFXConstants.sensorTypeBarometer 
    965         lsSensorManufacturer = RFXConstants.sensorManufacturerOregon 
    966  
    967         local lnDataTemp = getOregonTemperature(lsCommand) 
    968         local lnDataBattery = getOregonBattery(lsCommand) 
    969         local lnDataHum = getOregonHumidity(lsCommand) 
    970  
    971         lnDataPres = 795 + string.byte(lsCommand, 9) 
    972       lsID = "ORE" .. string.format("%02X", string.byte(lsCommand, 4) ) 
    973       lsCmd = string.format("temperature=%.1f;humidity=%d;pressure=%s;battery=%d", lnDataTemp, lnDataHum, lnDataPres, lnDataBattery) 
    974         lsRFXMSG = string.format("[OREGON: Temperature Device: %s Temperature: %.1f Humidity: %d Pressure: %s Battery: %s]", lsID, lnDataTemp, lnDataHum, lnDataPres, lnDataBattery) 
    975  
    976     end 
    977  
    978       return lsRFXMSG,lnSensorType,lsID,lsCmd,lsSensorManufacturer 
     918  elseif ( ( string.byte(lsCommand, 1) == 0x5a ) and ( string.byte(lsCommand, 2) == 0x6d ) ) then 
     919    luup.log("Identified Oregon BTHR918N") 
     920    lnSensorType = RFXConstants.sensorTypeTemperature + RFXConstants.sensorTypeHumidity + RFXConstants.sensorTypeBarometer 
     921    lsSensorManufacturer = RFXConstants.sensorManufacturerOregon 
     922     
     923    local lnDataTemp = getOregonTemperature(lsCommand) 
     924    local lnDataBattery = getOregonBattery(lsCommand) 
     925    local lnDataHum = getOregonHumidity(lsCommand) 
     926     
     927    lnDataPres = 856 + string.byte(lsCommand, 9) 
     928      lsID = "ORE" .. string.format("%02X", string.byte(lsCommand, 4) ) 
     929      lsCmd = string.format("temperature=%.1f;humidity=%d;pressure=%s;battery=%d", lnDataTemp, lnDataHum, lnDataPres, lnDataBattery) 
     930    lsRFXMSG = string.format("[OREGON: Temperature Device: %s Temperature: %.1f Humidity: %d Pressure: %s Battery: %s]", lsID, lnDataTemp, lnDataHum, lnDataPres, lnDataBattery) 
     931 
     932    -- Support for Oregon PCR800 2a19 92 
     933  elseif ( ( string.byte(lsCommand, 1) == 0x2a ) and ( string.byte(lsCommand, 2) == 0x19 ) ) then 
     934    luup.log("Identified Oregon PCR800") 
     935    lnSensorType = RFXConstants.sensorTypeRain 
     936    lsSensorManufacturer = RFXConstants.sensorManufacturerOregon 
     937    local lnDataRain = 0.0 
     938    local lnDataTRain = 0.0 
     939    local lnDataBattery = getOregonBattery(lsCommand) 
     940     
     941    lnDataRain = bitw.band(bitw.lshift(string.byte(lsCommand, 7), 0),0x0f) * 10 + bitw.band(bitw.rshift(string.byte(lsCommand, 6), 4),0x0f)  
     942      + bitw.band(bitw.lshift(string.byte(lsCommand, 6), 0),0x0f) / 10 + bitw.band(bitw.rshift(string.byte(lsCommand, 5), 4),0x0f) / 100  
     943    lnDataRain = 25.4 * lnDataRain 
     944     
     945    lnDataTRain = bitw.band(bitw.lshift(string.byte(lsCommand, 10), 0),0x0f) * 1000 + bitw.band(bitw.rshift(string.byte(lsCommand, 9), 4),0x0f) * 10  
     946      + bitw.band(bitw.lshift(string.byte(lsCommand, 9), 0),0x0f) + bitw.band(bitw.rshift(string.byte(lsCommand, 8), 4),0x0f) / 10  
     947      + bitw.band(bitw.lshift(string.byte(lsCommand, 8), 0),0x0f) / 100 + bitw.band(bitw.rshift(string.byte(lsCommand, 7), 4),0x0f) / 1000 
     948         
     949    lnDataTRain = 25.4 * lnDataTRain 
     950                 
     951    lsID = "ORE" .. string.format("%02X", string.byte(lsCommand, 4) ) 
     952    lsCmd = string.format("rain=%s;train=%s;battery=%d", lnDataRain, lnDataTRain, lnDataBattery) 
     953    lsRFXMSG = string.format("[OREGON: Rain Device: %s Rain: %s TRain: %s Battery: %s]", lsID, lnDataRain, lnDataTRain, lnDataBattery) 
     954     
     955      -- Support for Oregon RGR126,RGR682,RGR918 2A1D 84  :  54 2A 1D 00 F9 00 00 12 91 A5 43 04   
     956    elseif ( ( string.byte(lsCommand, 1) == 0x2a ) and ( string.byte(lsCommand, 2) == 0x1D ) ) then 
     957      luup.log("Identified Oregon RGR126,RGR682,RGR918") 
     958      lnSensorType = RFXConstants.sensorTypeRain 
     959      lsSensorManufacturer = RFXConstants.sensorManufacturerOregon 
     960      local lnDataRain = 0.0 
     961      local lnDataTRain = 0.0 
     962      local lnDataBattery = getOregonBattery(lsCommand) 
     963 
     964      lnDataRain = bitw.band(bitw.lshift(string.byte(lsCommand, 6), 0),0xff) * 10 + bitw.band(bitw.rshift(string.byte(lsCommand, 5), 4),0x0f)  
     965 
     966      lnDataTRain = bitw.band(bitw.lshift(string.byte(lsCommand, 9), 0),0x0f) * 1000 + bitw.band(bitw.rshift(string.byte(lsCommand, 8), 0),0xff) * 10  
     967        + bitw.band(bitw.rshift(string.byte(lsCommand, 7), 4),0x0f)  
     968 
     969      lsID = "ORE" .. string.format("%02X", string.byte(lsCommand, 4) ) 
     970      lsCmd = string.format("rain=%s;train=%s;battery=%d", lnDataRain, lnDataTRain, lnDataBattery) 
     971      lsRFXMSG = string.format("[OREGON: Rain Device: %s Rain: %s TRain: %s Battery: %s]", lsID, lnDataRain, lnDataTRain, lnDataBattery) 
     972       
     973    -- Support for Oregon THGR918 1A3D    
     974    elseif ( ( string.byte(lsCommand, 1) == 0x1a ) and ( string.byte(lsCommand, 2) == 0x3D ) ) then 
     975      luup.log("Identified Oregon THGR918") 
     976      lnSensorType = RFXConstants.sensorTypeTemperature + RFXConstants.sensorTypeHumidity + RFXConstants.sensorTypeBarometer 
     977      lsSensorManufacturer = RFXConstants.sensorManufacturerOregon 
     978 
     979      local lnDataTemp = getOregonTemperature(lsCommand) 
     980      local lnDataBattery = getOregonBattery(lsCommand) 
     981      local lnDataHum = getOregonHumidity(lsCommand) 
     982 
     983      lnDataPres = 795 + string.byte(lsCommand, 9) 
     984      lsID = "ORE" .. string.format("%02X", string.byte(lsCommand, 4) ) 
     985      lsCmd = string.format("temperature=%.1f;humidity=%d;pressure=%s;battery=%d", lnDataTemp, lnDataHum, lnDataPres, lnDataBattery) 
     986      lsRFXMSG = string.format("[OREGON: Temperature Device: %s Temperature: %.1f Humidity: %d Pressure: %s Battery: %s]", lsID, lnDataTemp, lnDataHum, lnDataPres, lnDataBattery) 
     987 
     988    end 
     989 
     990    return lsRFXMSG,lnSensorType,lsID,lsCmd,lsSensorManufacturer 
    979991  end     
    980      
    981     function getOregonTemperature(lsCommand) 
    982         local lnData = nib(lsCommand,10) * 10 + nib(lsCommand,11) + nib(lsCommand,8)/10 
    983         if (bitw.band( string.byte(lsCommand, 7) , 0x08) == 0) then 
    984         lnData = lnData 
    985         else 
    986         lnData = 0-lnData 
    987         end 
    988         return lnData 
     992   
     993  function getOregonTemperature(lsCommand) 
     994    local lnData = nib(lsCommand,10) * 10 + nib(lsCommand,11) + nib(lsCommand,8)/10 
     995    if (bitw.band( string.byte(lsCommand, 7) , 0x08) == 0) then 
     996    lnData = lnData 
     997    else 
     998    lnData = 0-lnData 
     999    end 
     1000    return lnData 
    9891001    end 
    9901002 
    9911003    function getOregonHumidity(lsCommand) 
    992         local lnData = nib(lsCommand,15) * 10 + nib(lsCommand,12) 
    993         return lnData 
     1004    local lnData = nib(lsCommand,15) * 10 + nib(lsCommand,12) 
     1005    return lnData 
    9941006    end 
    9951007 
    9961008    function getOregonBattery(lsCommand) 
    997         local battery_low = bitw.band( string.byte(lsCommand, 3) , 0x04) 
    998         local lnData = 90 --'high' 
    999         if battery_low == 1 then 
    1000             lnData = 10 --'low' 
    1001         end 
     1009    local battery_low = bitw.band( string.byte(lsCommand, 3) , 0x04) 
     1010    local lnData = 90 --'high' 
     1011    if battery_low == 1 then 
     1012      lnData = 10 --'low' 
     1013    end 
    10021014    return lnData 
    10031015    end 
    10041016 
    10051017    function getOregonPercentBattery(lsCommand) 
    1006         local lnData = 100 - 10*nib(lsCommand,9) 
    1007         return lnData 
     1018    local lnData = 100 - 10*nib(lsCommand,9) 
     1019    return lnData 
    10081020    end 
    10091021 
    10101022    function nib(lsCommand,i) 
    1011         local n = (i-i%2)/2 + 1 
    1012         if (i%2 == 0) then 
    1013         return bitw.band(bitw.rshift(string.byte(lsCommand, n), 4),0x0f) 
    1014         else 
    1015         return bitw.band(string.byte(lsCommand, n),0x0f) 
    1016         end 
     1023    local n = (i-i%2)/2 + 1 
     1024    if (i%2 == 0) then 
     1025    return bitw.band(bitw.rshift(string.byte(lsCommand, n), 4),0x0f) 
     1026    else 
     1027    return bitw.band(string.byte(lsCommand, n),0x0f) 
     1028    end 
    10171029    end 
    10181030 
     
    10471059      -- Log found message and decode!! 
    10481060      if ( lsCommand ~= nil and #lsCommand > 0 ) then 
    1049             -- luup.log("RFX message compleet: " .. formattohex(lsCommand)) 
    1050             table.insert( RFXmsgTable, lsCommand ) 
    1051             luup.call_delay("processMessage", 0 , "none") 
     1061        -- luup.log("RFX message compleet: " .. formattohex(lsCommand)) 
     1062        table.insert( RFXmsgTable, lsCommand ) 
     1063        luup.call_delay("processMessage", 0 , "none") 
    10521064      end 
    10531065 
     
    10581070  end 
    10591071  
    1060     ------------------------------------------------------------------------------------             
    1061     -- Main routine to decode and process the received messages 
    1062     ------------------------------------------------------------------------------------             
     1072  ------------------------------------------------------------------------------------       
     1073  -- Main routine to decode and process the received messages 
     1074  ------------------------------------------------------------------------------------       
    10631075  function processMessage(psCommand) 
    1064         while ( #RFXmsgTable > 0 ) do 
    1065             local lsRFXMSG = "" 
    1066             local lnSensorType = 0 
    1067             local lsID = "" 
    1068             local lsCmd = "" 
    1069             local lsSensorManufacturer = "" 
    1070  
    1071             psCommand = table.remove( RFXmsgTable, 1) 
    1072             local liBitLength = bitw.band( string.byte(psCommand, 1), 0x7f) 
    1073  
    1074             luup.log(string.format("decodeMessage bits:%d data:%s",liBitLength,formattohex(psCommand))) 
    1075  
    1076             --psCommand = GetStringPart( psCommand, 2, string.len( psCommand ) -1 ) 
    1077             --local liCommandLength = string.len(psCommand) 
    1078  
    1079             -- Decode based on bitlength 
    1080             if liBitLength == 12 then 
    1081                 luup.log("decodeHomeEasyHeater12") 
    1082             elseif liBitLength == 13 then 
    1083                 luup.log("decodeMertikMaxitrol13") 
    1084             elseif liBitLength == 20 then 
    1085                 luup.log("decodeATIRemoteWonder20") 
    1086             elseif liBitLength == 22 then 
    1087                 luup.log("decodeRobocamNinja22") 
    1088             elseif liBitLength == 24 then 
    1089                 luup.log("decodeArc24") 
    1090                 luup.log("decodeWaveman24") 
    1091             elseif liBitLength == 25 then 
    1092                 luup.log("decodeHarrison25") 
    1093             elseif liBitLength == 26 then 
    1094                 luup.log("decodeIkeaKoppla26") 
    1095             elseif liBitLength == 32 then 
    1096                 luup.log("decodeX1032") 
    1097                 luup.log("decodeDM1032") 
    1098                 luup.log("decodePCRemote32") 
    1099                 luup.log("decodeRFXSensor32")            
     1076    while ( #RFXmsgTable > 0 ) do 
     1077      local lsRFXMSG = "" 
     1078      local lnSensorType = 0 
     1079      local lsID = "" 
     1080      local lsCmd = "" 
     1081      local lsSensorManufacturer = "" 
     1082 
     1083      psCommand = table.remove( RFXmsgTable, 1) 
     1084      local liBitLength = bitw.band( string.byte(psCommand, 1), 0x7f) 
     1085 
     1086      luup.log(string.format("decodeMessage bits:%d data:%s",liBitLength,formattohex(psCommand))) 
     1087 
     1088      --psCommand = GetStringPart( psCommand, 2, string.len( psCommand ) -1 ) 
     1089      --local liCommandLength = string.len(psCommand) 
     1090 
     1091      -- Decode based on bitlength 
     1092      if liBitLength == 12 then 
     1093        luup.log("decodeHomeEasyHeater12") 
     1094      elseif liBitLength == 13 then 
     1095        luup.log("decodeMertikMaxitrol13") 
     1096      elseif liBitLength == 20 then 
     1097        luup.log("decodeATIRemoteWonder20") 
     1098      elseif liBitLength == 22 then 
     1099        luup.log("decodeRobocamNinja22") 
     1100      elseif liBitLength == 24 then 
     1101        luup.log("decodeArc24") 
     1102        luup.log("decodeWaveman24") 
     1103      elseif liBitLength == 25 then 
     1104        luup.log("decodeHarrison25") 
     1105      elseif liBitLength == 26 then 
     1106        luup.log("decodeIkeaKoppla26") 
     1107      elseif liBitLength == 32 then 
     1108        luup.log("decodeX1032") 
     1109        luup.log("decodeDM1032") 
     1110        luup.log("decodePCRemote32") 
     1111        luup.log("decodeRFXSensor32")      
    11001112        lsRFXMSG,lnSensorType,lsID,lsCmd,lsSensorManufacturer = decodeX10( psCommand ) 
    1101             elseif liBitLength == 33 then 
    1102                 luup.log("decodeHomeEasy33")                             
    1103             elseif liBitLength == 34  then 
    1104                 lsRFXMSG,lnSensorType,lsID,lsCmd,lsSensorManufacturer = decodeHomeEasy34(psCommand) 
    1105             elseif liBitLength == 36  then 
    1106                 lsRFXMSG,lnSensorType,lsID,lsCmd,lsSensorManufacturer = decodeHomeEasy36(psCommand) 
    1107             elseif liBitLength == 37 then 
    1108                 luup.log("decodeHomeEasy37")                             
    1109             elseif liBitLength == 41 then 
    1110                 luup.log("decodeX10Security41") 
    1111                 lsRFXMSG,lnSensorType,lsID,lsCmd,lsSensorManufacturer = decodeVisonic(psCommand) 
    1112             elseif liBitLength == 44 then 
    1113                 luup.log("decodeDigimaxRTS1044")                             
    1114             elseif liBitLength == 47 then 
    1115                 luup.log("decodeCheaperTronics47") 
    1116             elseif liBitLength == 56 or liBitLength > 59 then 
    1117                 luup.log("decodeoregon") 
    1118                 lsRFXMSG,lnSensorType,lsID,lsCmd,lsSensorManufacturer = decodeOregon(psCommand) 
    1119             end 
    1120              
    1121             if ( string.len( lsRFXMSG ) == 0 ) then 
    1122                 luup.log("Unknown Msg : " .. formattohex(psCommand) ) 
    1123             else 
    1124                 ------------------------------------------------------------------------------------             
    1125                 -- ignore repeated messages. 
    1126                 -- Really new and decoded messages must be processed. After processing, update 
    1127                 -- last message variable 
    1128                 -- Unfortunately this sometimes has an unwanted effect. This is why after a pause we need 
    1129                 -- to forget the last message... 
    1130                 ------------------------------------------------------------------------------------     
    1131                  
    1132                 if ((os.time() - RFXLastMsgTime) > 10) then 
    1133                   RFXLastMsg = "" 
    1134                 end 
    1135                          
    1136                 if ( RFXLastMsg ~= lsRFXMSG ) then 
    1137                     actOnMessage(lnSensorType,lsID,lsCmd)                                
    1138                     luup.variable_set("urn:upnp-esweb-nl:serviceId:rfxcom1", "Message", lsRFXMSG, THIS_DEVICE) 
    1139                     luup.log( lsRFXMSG ) 
    1140                 end 
    1141             end 
    1142              
    1143             RFXLastMsgTime = os.time() 
    1144             RFXLastMsg = lsRFXMSG 
    1145         end 
    1146     end 
    1147  
    1148      
    1149     ------------------------------------------------------------------------ 
    1150     -- Act on decoded message 
    1151     --  
    1152     -- If variable 'AutoCreate' is set to 1 and a received sensor doesn't 
    1153     -- exist yet, it's created.  
    1154     -- Next execution depends on kind of sensor 
    1155     -- Controller (lightswitch): Status variable is set based on cmdOn or cmdOff 
    1156     -- Motion Sensor: Tripped status is set 
    1157     -- Door Sensor: Tripped status is set 
    1158     -- KeyFox: All door and motion sensors are armed or disarmed 
    1159     ------------------------------------------------------------------------ 
    1160  
    1161     function actOnMessage(lnSensorType,lsID,lsCmd) 
    1162  
    1163         luup.log("Received command " .. lsCmd .. " for " .. lnSensorType .. " with ID " .. lsID)     
    1164  
    1165         -- Check if we can find a device with the decoded ID and ourself as parent 
    1166         local device = findChild(THIS_DEVICE, lsID ) 
     1113      elseif liBitLength == 33 then 
     1114        luup.log("decodeHomeEasy33")               
     1115      elseif liBitLength == 34  then 
     1116        lsRFXMSG,lnSensorType,lsID,lsCmd,lsSensorManufacturer = decodeHomeEasy34(psCommand) 
     1117      elseif liBitLength == 36  then 
     1118        lsRFXMSG,lnSensorType,lsID,lsCmd,lsSensorManufacturer = decodeHomeEasy36(psCommand) 
     1119      elseif liBitLength == 37 then 
     1120        luup.log("decodeHomeEasy37")               
     1121      elseif liBitLength == 41 then 
     1122        luup.log("decodeX10Security41") 
     1123        lsRFXMSG,lnSensorType,lsID,lsCmd,lsSensorManufacturer = decodeVisonic(psCommand) 
     1124      elseif liBitLength == 44 then 
     1125        luup.log("decodeDigimaxRTS1044")               
     1126      elseif liBitLength == 47 then 
     1127        luup.log("decodeCheaperTronics47") 
     1128      elseif liBitLength == 56 or liBitLength > 59 then 
     1129        luup.log("decodeoregon") 
     1130        lsRFXMSG,lnSensorType,lsID,lsCmd,lsSensorManufacturer = decodeOregon(psCommand) 
     1131      end 
     1132       
     1133      if ( string.len( lsRFXMSG ) == 0 ) then 
     1134        luup.log("Unknown Msg : " .. formattohex(psCommand) ) 
     1135      else 
     1136        ------------------------------------------------------------------------------------       
     1137        -- ignore repeated messages. 
     1138        -- Really new and decoded messages must be processed. After processing, update 
     1139        -- last message variable 
     1140        -- Unfortunately this sometimes has an unwanted effect. This is why after a pause we need 
     1141        -- to forget the last message... 
     1142        ------------------------------------------------------------------------------------   
     1143         
     1144        if ((os.time() - RFXLastMsgTime) > 10) then 
     1145          RFXLastMsg = "" 
     1146        end 
     1147             
     1148        if ( RFXLastMsg ~= lsRFXMSG ) then 
     1149          actOnMessage(lnSensorType,lsID,lsCmd)                
     1150          luup.variable_set("urn:upnp-esweb-nl:serviceId:rfxcom1", "Message", lsRFXMSG, THIS_DEVICE) 
     1151          luup.log( lsRFXMSG ) 
     1152        end 
     1153      end 
     1154       
     1155      RFXLastMsgTime = os.time() 
     1156      RFXLastMsg = lsRFXMSG 
     1157    end 
     1158  end 
     1159 
     1160   
     1161  ------------------------------------------------------------------------ 
     1162  -- Act on decoded message 
     1163  --  
     1164  -- If variable 'AutoCreate' is set to 1 and a received sensor doesn't 
     1165  -- exist yet, it's created.  
     1166  -- Next execution depends on kind of sensor 
     1167  -- Controller (lightswitch): Status variable is set based on cmdOn or cmdOff 
     1168  -- Motion Sensor: Tripped status is set 
     1169  -- Door Sensor: Tripped status is set 
     1170  -- KeyFox: All door and motion sensors are armed or disarmed 
     1171  ------------------------------------------------------------------------ 
     1172 
     1173  function actOnMessage(lnSensorType,lsID,lsCmd) 
     1174 
     1175    luup.log("Received command " .. lsCmd .. " for " .. lnSensorType .. " with ID " .. lsID)     
     1176 
     1177    -- Check if we can find a device with the decoded ID and ourself as parent 
     1178    local device = findChild(THIS_DEVICE, lsID ) 
    11671179      
    1168         ---------------------------------------------------------------------------------------- 
    1169         -- If we can't find a device and we have autocreate active, setup a new tree with child devices 
    1170         -- and add a new child with the correct type and ID 
    1171         ---------------------------------------------------------------------------------------- 
    1172         if device == nil and luup.variable_get("urn:upnp-esweb-nl:serviceId:rfxcom1", "AutoCreate", lul_device) == "1" then 
    1173             luup.log( "device not found and autocreate enabled..." )  
    1174              
    1175             local child_devices = luup.chdev.start(THIS_DEVICE);   
    1176  
    1177             ------------------------------------------------------------------------------------             
    1178             -- First add all 'old' children to the tree 
    1179             -- Start with door sensors 
    1180             ------------------------------------------------------------------------------------             
    1181             for teller = 1, RFXCountDS do 
    1182               luup.chdev.append(THIS_DEVICE, child_devices, RFXDevicesDS[teller], "RFX DS sensor #" .. RFXDevicesDS[teller] ,  
    1183                 "urn:schemas-micasaverde-com:device:DoorSensor:1","D_DoorSensor1.xml","","",false) 
    1184             end 
    1185             ------------------------------------------------------------------------------------             
    1186             -- Next with motion sensors 
    1187             ------------------------------------------------------------------------------------             
    1188             for teller = 1, RFXCountMS do 
    1189               luup.chdev.append(THIS_DEVICE, child_devices, RFXDevicesMS[teller], "RFX MS sensor #" .. RFXDevicesMS[teller] ,  
    1190                 "urn:schemas-micasaverde-com:device:MotionSensor:1","D_MotionSensor1.xml","","",false) 
    1191             end          
    1192             ------------------------------------------------------------------------------------             
    1193             -- Next with keyfobs 
    1194             ------------------------------------------------------------------------------------             
    1195             for teller = 1, RFXCountKF do 
    1196               luup.chdev.append(THIS_DEVICE, child_devices, RFXDevicesKF[teller], "RFX KF sensor #" .. RFXDevicesKF[teller] ,  
    1197                 "urn:schemas-ActiveRFID:device:RFIDTransmitter:1","D_RFIDTransmitter.xml","","",false) 
    1198             end          
    1199             ------------------------------------------------------------------------------------             
    1200             -- Next with light switches 
    1201             ------------------------------------------------------------------------------------             
    1202             for teller = 1, RFXCountLS do 
    1203               luup.chdev.append(THIS_DEVICE, child_devices, RFXDevicesLS[teller], "RFX LS #" .. RFXDevicesLS[teller] ,  
    1204                 "urn:schemas-upnp-org:device:BinaryLight:1","D_BinaryLight1.xml","","",false) 
    1205             end          
    1206             ------------------------------------------------------------------------------------             
    1207             -- Next Harrison window covering 
    1208             ------------------------------------------------------------------------------------             
    1209             for teller = 1, RFXCOM_HAR_Count do 
    1210                 luup.chdev.append(THIS_DEVICE, child_devices, RFXCOM_HAR_Devices[teller],  
    1211                   "Harrison #" .. RFXCOM_HAR_Devices[teller] , "urn:schemas-micasaverde-com:device:WindowCovering:1", 
    1212                   "D_RFXCOMHarrison.xml","","",false) 
    1213             end 
    1214             ------------------------------------------------------------------------------------             
    1215             -- Next with temperature devices 
    1216             ------------------------------------------------------------------------------------             
    1217             for teller = 1, RFXCountTS2 do 
    1218               luup.log("debut temp:" .. RFXDevicesTS2[teller]) 
    1219               luup.chdev.append(THIS_DEVICE, child_devices, RFXDevicesTS2[teller], "RFX TS #" .. RFXDevicesTS2[teller] ,  
    1220                 "urn:schemas-micasaverde-com:device:TemperatureSensor:1", "D_TemperatureSensor1.xml","","",false) 
    1221             end          
    1222             ------------------------------------------------------------------------------------             
    1223             -- Next with humidity devices 
    1224             ------------------------------------------------------------------------------------             
    1225             for teller = 1, RFXCountHS2 do 
    1226               luup.chdev.append(THIS_DEVICE, child_devices, RFXDevicesHS2[teller], "RFX HS #" .. RFXDevicesHS2[teller] ,  
    1227                 "urn:schemas-micasaverde-com:device:HumiditySensor:1", "D_HumiditySensor1.xml","","",false) 
    1228             end  
    1229             ------------------------------------------------------------------------------------             
    1230             -- Next with barometer devices 
    1231             ------------------------------------------------------------------------------------             
    1232             for teller = 1, RFXCountBS2 do 
    1233               luup.chdev.append(THIS_DEVICE, child_devices, RFXDevicesBS2[teller], "RFX BS #" .. RFXDevicesBS2[teller] ,  
    1234                 "urn:schemas-micasaverde-com:device:BarometerSensor:1", "D_BarometerSensor1.xml","","",false) 
    1235             end  
    1236              
    1237             ------------------------------------------------------------------------------------             
    1238             -- Next with rain devices 
    1239             ------------------------------------------------------------------------------------             
    1240             for teller = 1, RFXCountRS2 do 
    1241               luup.chdev.append(THIS_DEVICE, child_devices, RFXDevicesRS2[teller], "RFX RS #" .. RFXDevicesRS2[teller] ,  
    1242                 "urn:schemas-micasaverde-com:device:RainSensor:1", "D_RainSensor1.xml","","",false) 
    1243             end  
    1244              
    1245             ------------------------------------------------------------------------------------ 
    1246             -- Next with EZAnemoSensor devices 
    1247             ------------------------------------------------------------------------------------ 
    1248             for teller = 1, RFXCountAS2 do 
    1249                 luup.chdev.append(THIS_DEVICE, child_devices, RFXDevicesAS2[teller], "RFX AS #" .. RFXDevicesAS2[teller] , 
    1250                 "urn:schemas-upnp-org:device:EZAnemoSensor:1", "D_EZAnemoSensor.xml","","",false) 
    1251             end 
    1252  
    1253             ------------------------------------------------------------------------------------             
    1254             -- Now add the new device to the tree depending on the sensor type 
    1255             ------------------------------------------------------------------------------------             
    1256  
    1257             luup.log("Tree buildup, now add " .. lsID .. " with sensortype " .. lnSensorType) 
    1258  
    1259             ------------------------------------------------------------------------------------             
    1260             -- Add if doorsensor 
    1261             ------------------------------------------------------------------------------------             
    1262             if bitw.band(lnSensorType,RFXConstants.sensorTypeDoor) ~= 0 then 
    1263                 luup.log("CREATING CHILD DEVICE DOOR SENSOR WITH ID " .. lsID) 
    1264                 luup.chdev.append(THIS_DEVICE, child_devices, lsID, "RFX DS sensor #" .. lsID ,  
    1265                   "urn:schemas-micasaverde-com:device:DoorSensor:1","D_DoorSensor1.xml","","",false) 
    1266                 RFXCountDS = RFXCountDS + 1 
    1267                 RFXDevicesDS[RFXCountDS] = lsID 
    1268             end 
    1269             ------------------------------------------------------------------------------------             
    1270             -- Add if motionsensor 
    1271             ------------------------------------------------------------------------------------             
    1272             if bitw.band(lnSensorType,RFXConstants.sensorTypeMotion) ~= 0 then 
    1273                 luup.log("CREATING CHILD DEVICE MOTION SENSOR WITH ID " .. lsID) 
    1274                 luup.chdev.append(THIS_DEVICE, child_devices, lsID, "RFX MS sensor #" .. lsID ,  
    1275                   "urn:schemas-micasaverde-com:device:MotionSensor:1","D_MotionSensor1.xml","","",false) 
    1276                 RFXCountMS = RFXCountMS + 1 
    1277                 RFXDevicesMS[RFXCountMS] = lsID 
    1278             end 
    1279             ------------------------------------------------------------------------------------             
    1280             -- Add if keybof 
    1281             ------------------------------------------------------------------------------------             
    1282             if bitw.band(lnSensorType,RFXConstants.sensorTypeKeyFob) ~= 0 then 
    1283                 luup.log("CREATING CHILD DEVICE KEYFOB WITH ID " .. lsID) 
    1284                 luup.chdev.append(THIS_DEVICE, child_devices, "KF/" .. lsID, "RFX KF sensor #" .. lsID ,  
    1285                   "urn:schemas-ActiveRFID:device:RFIDTransmitter:1","D_RFIDTransmitter.xml","","",false) 
    1286                 RFXCountKF = RFXCountKF + 1 
    1287                 RFXDevicesKF[RFXCountKF] = "KF/" .. lsID 
    1288             end 
    1289             ------------------------------------------------------------------------------------             
    1290             -- Add if lightswitch 
    1291             ------------------------------------------------------------------------------------             
    1292             if bitw.band(lnSensorType,RFXConstants.sensorTypeControler) ~= 0 then 
    1293                 luup.log("CREATING CHILD DEVICE LIGHT SWITCH WITH ID " .. lsID) 
    1294                 luup.chdev.append(THIS_DEVICE, child_devices, lsID, "RFX LS #" .. lsID ,  
    1295                   "urn:schemas-upnp-org:device:BinaryLight:1","D_BinaryLight1.xml","","",false) 
    1296                 RFXCountLS = RFXCountLS + 1 
    1297                 RFXDevicesLS[RFXCountLS] = lsID 
    1298             end          
    1299             ------------------------------------------------------------------------------------             
    1300             -- Add if Harrison window covering 
    1301             ------------------------------------------------------------------------------------             
    1302             if bitw.band(lnSensorType,RFXConstants.sensorTypeWindowCovering) ~= 0 then 
    1303                 luup.chdev.append(THIS_DEVICE, child_devices, lsID, "Harrison #" .. lsID , 
    1304                     "urn:schemas-micasaverde-com:device:WindowCovering:1","D_RFXCOMHarrison.xml","","",false)            
    1305                 RFXCOM_HAR_Count = RFXCOM_HAR_Count + 1 
    1306                 RFXCOM_HAR_Devices[RFXCOM_HAR_Count] = lsID 
    1307             end  
    1308             ------------------------------------------------------------------------------------             
    1309             -- Add temperature sensor 
    1310             ------------------------------------------------------------------------------------             
    1311             if bitw.band(lnSensorType,RFXConstants.sensorTypeTemperature) ~= 0 then 
    1312                 luup.chdev.append(THIS_DEVICE, child_devices, "TS/" .. lsID, "RFX TS #" .. lsID, 
    1313                     "urn:schemas-micasaverde-com:device:TemperatureSensor:1", "D_TemperatureSensor1.xml","","",false) 
    1314                 RFXCountTS2 = RFXCountTS2 + 1 
    1315                 RFXDevicesTS2[RFXCountTS2] = "TS/" .. lsID 
    1316             end  
    1317             ------------------------------------------------------------------------------------             
    1318             -- Add humidity sensor 
    1319             ------------------------------------------------------------------------------------             
    1320             if bitw.band(lnSensorType,RFXConstants.sensorTypeHumidity) ~= 0 then 
    1321                 luup.chdev.append(THIS_DEVICE, child_devices, "HS/" .. lsID, "RFX HS #" .. lsID, 
    1322                     "urn:schemas-micasaverde-com:device:HumiditySensor:1", "D_HumiditySensor1.xml","","",false) 
    1323                 RFXCountHS2 = RFXCountHS2 + 1 
    1324                 RFXDevicesHS2[RFXCountHS2] = "HS/" .. lsID 
    1325             end  
    1326             ------------------------------------------------------------------------------------             
    1327             -- Add barometer sensor 
    1328             ------------------------------------------------------------------------------------             
    1329             if bitw.band(lnSensorType,RFXConstants.sensorTypeBarometer) ~= 0 then 
    1330                 luup.chdev.append(THIS_DEVICE, child_devices, "BS/" .. lsID, "RFX BS #" .. lsID, 
    1331                     "urn:schemas-micasaverde-com:device:BarometerSensor:1", "D_BarometerSensor1.xml","","",false) 
    1332                 RFXCountBS2 = RFXCountBS2 + 1 
    1333                 RFXDevicesBS2[RFXCountBS2] = "BS/" .. lsID 
    1334             end  
    1335              
    1336             ------------------------------------------------------------------------------------             
    1337             -- Add rain sensor 
    1338             ------------------------------------------------------------------------------------             
    1339             if bitw.band(lnSensorType,RFXConstants.sensorTypeRain) ~= 0 then 
    1340                 luup.chdev.append(THIS_DEVICE, child_devices, "RS/" .. lsID, "RFX RS #" .. lsID, 
    1341                     "urn:schemas-micasaverde-com:device:RainSensor:1", "D_RainSensor1.xml","","",false) 
    1342                 RFXCountRS2 = RFXCountRS2 + 1 
    1343                 RFXDevicesRS2[RFXCountRS2] = "RS/" .. lsID 
    1344             end  
    1345              
    1346             ------------------------------------------------------------------------------------ 
    1347             -- Add D_EZAnemoSensor sensor 
    1348             ------------------------------------------------------------------------------------ 
    1349             if bitw.band(lnSensorType,RFXConstants.sensorTypeWindDirection) ~= 0 then 
    1350                 luup.chdev.append(THIS_DEVICE, child_devices, "AS/" .. lsID, "RFX AS #" .. lsID, 
    1351                 "urn:schemas-upnp-org:device:EZAnemoSensor:1", "D_EZAnemoSensor.xml","","",false) 
    1352                 RFXCountAS2 = RFXCountAS2 + 1 
    1353                 RFXDevicesAS2[RFXCountAS2] = "AS/" .. lsID 
    1354             end 
    1355              
    1356              
    1357              
    1358             luup.log("Tree buildup with number child devices:") 
    1359             luup.log("       door sensors: " .. RFXCountDS) 
    1360             luup.log("     motion sensors: " .. RFXCountMS) 
    1361             luup.log("     keyfob sensors: " .. RFXCountKF) 
    1362             luup.log("     light switches: " .. RFXCountLS) 
    1363             luup.log("   window coverings: " .. RFXCOM_HAR_Count) 
    1364             luup.log("temperature sensors: " .. RFXCountTS2) 
    1365             luup.log("   humidity sensors: " .. RFXCountHS2) 
    1366             luup.log("   pressure sensors: " .. RFXCountBS2) 
    1367             luup.log("       rain sensors: " .. RFXCountRS2) 
    1368             luup.log("      Anemo sensors: " .. RFXCountAS2) 
    1369              
    1370             -- Synch the new tree with the old three 
    1371             luup.log("Start sync") 
    1372             luup.chdev.sync(THIS_DEVICE, child_devices) 
    1373             luup.log("End sync")        
    1374         end 
    1375          
    1376         ------------------------------------------------------------------------------ 
    1377         -- Deliver message to all devices with the correct altid (multiple 
    1378         -- devices can have the correct altid!) 
    1379         ------------------------------------------------------------------------------ 
    1380          
    1381         local fullId = "" 
    1382         for k, v in pairs(luup.devices) do 
    1383             -- Check if we have a device with the correct parent (THIS_DEVICE) and  
    1384             -- altid we are looking for (lsID) 
    1385             if (v.device_num_parent == THIS_DEVICE) then 
    1386                 device = k 
    1387  
    1388                 -- luup.log("Device found or created, now processing command " .. lsCmd .. " for " .. lsID) 
    1389                 -- luup.log("Device Number: " .. k .. 
    1390                 --      " v.device_type: " .. tostring(v.device_type) .. 
    1391                 --      " v.device_num_parent: " .. tostring(v.device_num_parent) .. 
    1392                 --      " v.id: " .. tostring(v.id) 
    1393                 -- ) 
    1394              
    1395                 -- Execution door sensors same as motion sensors 
    1396                 if bitw.band(lnSensorType,RFXConstants.sensorTypeDoor) ~= 0 or bitw.band(lnSensorType,RFXConstants.sensorTypeMotion) ~= 0  
    1397                   and v.id == lsId then 
    1398                     local TrippedValue = "0" 
    1399                     if lsCmd == RFXConstants.cmdAlert then TrippedValue = "1" end 
    1400                     luup.log( "Set tripped value " .. TrippedValue .. " to device " .. lsID )  
    1401                     luup.variable_set("urn:micasaverde-com:serviceId:SecuritySensor1", "Tripped", TrippedValue, device) 
    1402                 end 
    1403  
    1404                 -- Execution for keyfobs 
    1405                 -- Arm or Disarm all door and motion sensors when keyfob armed or disarmed 
    1406                 -- Set trip state keyfob as indication home or away 
    1407                 fullId = "KF/" .. lsID 
    1408                 if bitw.band(lnSensorType,RFXConstants.sensorTypeKeyFob) ~= 0  
    1409                     and v.id == fullId then 
    1410                     luup.log("KeyFob action: " .. lsCmd)  
    1411                      
    1412                     if lsCmd == RFXConstants.cmdArmAway or lsCmd == RFXConstants.cmdArmHome or lsCmd == RFXConstants.cmdDisarm then 
    1413                       ArmDisarmAll(lsCmd) 
    1414                     --  local newArmedValue = "1" -- armed 
    1415                     --  local newTrippedValue = "1" -- home 
    1416          
    1417                     --  if lsCmd == RFXConstants.cmdDisarm then  
    1418                     --  newArmedValue = "0"  
    1419                     --  newTrippedValue = "0" 
    1420                     --  end 
    1421                     --  if lsCmd == RFXConstants.cmdArmAway then newTrippedValue = "0" end 
    1422                     --    luup.log( "Set ArmedValue value " .. newArmedValue .. " to device " .. device )  
    1423                     --  luup.variable_set("urn:micasaverde-com:serviceId:SecuritySensor1","Armed",newArmedValue, device) 
    1424                     --  luup.variable_set("urn:micasaverde-com:serviceId:SecuritySensor1", "Tripped", newTrippedValue, device) 
    1425                     elseif lsCmd == RFXConstants.cmdPanic then 
    1426                         -- not implemented 
    1427                     elseif lsCmd == RFXConstants.cmdOn  then 
    1428                         luup.log("urn:ActiveRFID:serviceId:RFIDTransmitter1  Action:On:Portail" .. fullId)  
    1429                         local Pstatus = luup.variable_get("urn:upnp-org:serviceId:SwitchPower1","Status",26) 
    1430                         if Pstatus == "0" then 
    1431                             luup.variable_set("urn:upnp-org:serviceId:SwitchPower1","Status","1",26) 
    1432                         else 
    1433                             luup.variable_set("urn:upnp-org:serviceId:SwitchPower1","Status","0",26) 
    1434                         end 
    1435                     elseif lsCmd == RFXConstants.cmdOff then 
    1436                         luup.log("urn:ActiveRFID:serviceId:RFIDTransmitter1  Action:Off:Garage" .. fullId)  
    1437                         local Pstatus = luup.variable_get("urn:upnp-org:serviceId:SwitchPower1","Status",27) 
    1438                         if Pstatus == "0" then 
    1439                             luup.variable_set("urn:upnp-org:serviceId:SwitchPower1","Status","1",27) 
    1440                         else 
    1441                             luup.variable_set("urn:upnp-org:serviceId:SwitchPower1","Status","0",27) 
    1442                         end 
    1443                     end          
    1444                 end  
    1445  
    1446                 -- Execution light switches.  
    1447                 -- Set Status based on cmdOn or cmdOff 
    1448                 if bitw.band(lnSensorType,RFXConstants.sensorTypeControler) ~= 0  
    1449                    and v.id == lsID then 
    1450                     local newTargetValue = "0" 
    1451                      
    1452                     if lsCmd == RFXConstants.cmdOn then newTargetValue = "1" end 
    1453                     if lsCmd == RFXConstants.cmdGroupOn then newTargetValue = "1" end 
    1454                      
    1455                     luup.log("SET TARGET from " .. lsID .. " with value " .. newTargetValue) 
    1456                     luup.variable_set("urn:upnp-org:serviceId:SwitchPower1","Status",newTargetValue,device) 
    1457                 end          
    1458                  
    1459                 -- Execution for temperature sensor 
    1460                 -- Set temperature to command 
    1461                 fullId = "TS/" .. lsID 
    1462                 if bitw.band(lnSensorType,RFXConstants.sensorTypeTemperature) ~= 0  
    1463                         and tostring(v.device_type) == "urn:schemas-micasaverde-com:device:TemperatureSensor:1"  
    1464                         and v.id == fullId then 
    1465                     for lsKey,lsValue in string.gmatch(lsCmd, "(%w+)=([%w%.%-_]+)") do 
    1466                         -- luup.log("Key=" ..  lsKey .. ";Value=" .. lsValue .. ";") 
    1467                         if lsKey == "temperature" then 
    1468                             luup.log("SET CURRENTTEMPERATURE from " .. lsID .. " with value " .. lsValue) 
    1469                             luup.variable_set("urn:upnp-org:serviceId:TemperatureSensor1", "CurrentTemperature", 
    1470                                 lsValue, device) 
    1471                         elseif lsKey == "battery" then 
    1472                             luup.log("SET BATTERY from " .. lsID .. " with value " .. lsValue) 
    1473                             luup.variable_set("urn:micasaverde-com:serviceId:HaDevice1", "BatteryLevel", lsValue, device) 
    1474                         end 
    1475                     end 
    1476                 end 
    1477  
    1478                 -- Execution for humidity sensor 
    1479                 -- Set humidity to command  
    1480                 fullId = "HS/" .. lsID 
    1481                 if bitw.band(lnSensorType,RFXConstants.sensorTypeHumidity) ~= 0  
    1482                         and tostring(v.device_type) == "urn:schemas-micasaverde-com:device:HumiditySensor:1"  
    1483                         and v.id == fullId then 
    1484                     for lsKey,lsValue in string.gmatch(lsCmd, "(%w+)=([%w%.%-_]+)") do 
    1485                         -- luup.log("Key=" ..  lsKey .. ";Value=" .. lsValue .. ";") 
    1486                         if lsKey == "humidity" then 
    1487                             luup.log("SET CURRENTLEVEL from " .. lsID .. " with value " .. lsValue) 
    1488                             luup.variable_set("urn:micasaverde-com:serviceId:HumiditySensor1", "CurrentLevel", 
    1489                                 lsValue, device) 
    1490                         elseif lsKey == "battery" then 
    1491                             luup.log("SET BATTERY from " .. lsID .. " with value " .. lsValue) 
    1492                             luup.variable_set("urn:micasaverde-com:serviceId:HaDevice1", "BatteryLevel", lsValue, device) 
    1493                         end 
    1494                     end 
    1495                 end  
    1496  
    1497                 -- Execution for pressure sensor 
    1498                 -- Set pressure to command 
    1499                 fullId = "BS/" .. lsID 
    1500                 if bitw.band(lnSensorType,RFXConstants.sensorTypeBarometer) ~= 0  
    1501                         and tostring(v.device_type) == "urn:schemas-micasaverde-com:device:BarometerSensor:1"  
    1502                         and v.id == fullId then 
    1503                     for lsKey,lsValue in string.gmatch(lsCmd, "(%w+)=([%w%.%-_]+)") do 
    1504                         -- luup.log("Key=" ..  lsKey .. ";Value=" .. lsValue .. ";") 
    1505                         if lsKey == "pressure" then 
    1506                             luup.log("SET CURRENTPRESSURE from " .. lsID .. " with value " .. lsValue) 
    1507                             luup.variable_set("urn:upnp-org:serviceId:BarometerSensor1", "CurrentPressure", 
    1508                                 lsValue, device) 
    1509                         elseif lsKey == "battery" then 
    1510                             luup.log("SET BATTERY from " .. lsID .. " with value " .. lsValue) 
    1511                             luup.variable_set("urn:micasaverde-com:serviceId:HaDevice1", "BatteryLevel", lsValue, device) 
    1512                         end 
    1513                     end 
    1514                 end 
    1515                 fullId = "RS/" .. lsID 
    1516                 if bitw.band(lnSensorType,RFXConstants.sensorTypeRain) ~= 0  
    1517                         and tostring(v.device_type) == "urn:schemas-micasaverde-com:device:RainSensor:1"  
    1518                         and v.id == fullId then 
    1519                     for lsKey,lsValue in string.gmatch(lsCmd, "(%w+)=([%w%.%-_]+)") do 
    1520                         -- luup.log("Key=" ..  lsKey .. ";Value=" .. lsValue .. ";") 
    1521                         if lsKey == "rain" then 
    1522                             luup.log("SET CURRENTRAIN from " .. lsID .. " with value " .. lsValue) 
    1523                             luup.variable_set("urn:upnp-org:serviceId:RainSensor1", "CurrentRain", lsValue, device) 
    1524                         elseif lsKey == "train" then 
    1525                             luup.log("SET CURRENTTRAIN from " .. lsID .. " with value " .. lsValue) 
    1526                             luup.variable_set("urn:upnp-org:serviceId:RainSensor1", "CurrentTRain", lsValue, device)  
    1527                         elseif lsKey == "battery" then 
    1528                             luup.log("SET BATTERY from " .. lsID .. " with value " .. lsValue) 
    1529                             luup.variable_set("urn:micasaverde-com:serviceId:HaDevice1", "BatteryLevel", lsValue, device) 
    1530                         end 
    1531                     end 
    1532                 end 
    1533                 fullId = "AS/" .. lsID 
    1534                 if bitw.band(lnSensorType,RFXConstants.sensorTypeWindDirection) ~= 0 
    1535                         and tostring(v.device_type) == "urn:schemas-upnp-org:device:EZAnemoSensor:1" 
    1536                         and v.id == fullId then 
    1537                     for lsKey,lsValue in string.gmatch(lsCmd, "(%w+)=([%w%.%-_]+)") do 
    1538                         luup.log("Key=" ..  lsKey .. ";Value=" .. lsValue .. ";") 
    1539                         if lsKey == "direction" then 
    1540                             luup.log("SET DIRECTION from " .. lsID .. " with value " .. lsValue) 
    1541                             luup.variable_set("urn:upnp-org:serviceId:EZAnemoSensor1", "Direction", lsValue, device) 
    1542                         end 
    1543                         if lsKey == "speed" then 
    1544                             luup.log("SET SPEED from " .. lsID .. " with value " .. lsValue) 
    1545                             luup.variable_set("urn:upnp-org:serviceId:EZAnemoSensor1", "Speed", lsValue, device) 
    1546                         end 
    1547                         if lsKey == "speedav" then 
    1548                             luup.log("SET SPEEDAV from " .. lsID .. " with value " .. lsValue) 
    1549                             luup.variable_set("urn:upnp-org:serviceId:EZAnemoSensor1", "SpeedAv", lsValue, device) 
    1550                         end 
    1551                         if lsKey == "battery" then 
    1552                             luup.log("SET BATTERY from " .. lsID .. " with value " .. lsValue) 
    1553                             luup.variable_set("urn:upnp-org:serviceId:EZAnemoSensor1", "Battery", lsValue, device) 
    1554                         end 
    1555                     end 
    1556                 end 
    1557             end 
    1558              
    1559         end  
    1560  
    1561     end 
     1180    ---------------------------------------------------------------------------------------- 
     1181    -- If we can't find a device and we have autocreate active, setup a new tree with child devices 
     1182    -- and add a new child with the correct type and ID 
     1183    ---------------------------------------------------------------------------------------- 
     1184    if device == nil and luup.variable_get("urn:upnp-esweb-nl:serviceId:rfxcom1", "AutoCreate", lul_device) == "1" then 
     1185      luup.log( "device not found and autocreate enabled..." )  
     1186       
     1187      local child_devices = luup.chdev.start(THIS_DEVICE);   
     1188 
     1189      ------------------------------------------------------------------------------------       
     1190      -- First add all 'old' children to the tree 
     1191      -- Start with door sensors 
     1192      ------------------------------------------------------------------------------------       
     1193      for teller = 1, RFXCountDS do 
     1194        luup.chdev.append(THIS_DEVICE, child_devices, RFXDevicesDS[teller], "RFX DS sensor #" .. RFXDevicesDS[teller] ,  
     1195        "urn:schemas-micasaverde-com:device:DoorSensor:1","D_DoorSensor1.xml","","",false) 
     1196      end 
     1197      ------------------------------------------------------------------------------------       
     1198      -- Next with motion sensors 
     1199      ------------------------------------------------------------------------------------       
     1200      for teller = 1, RFXCountMS do 
     1201        luup.chdev.append(THIS_DEVICE, child_devices, RFXDevicesMS[teller], "RFX MS sensor #" .. RFXDevicesMS[teller] ,  
     1202        "urn:schemas-micasaverde-com:device:MotionSensor:1","D_MotionSensor1.xml","","",false) 
     1203      end      
     1204      ------------------------------------------------------------------------------------       
     1205      -- Next with keyfobs 
     1206      ------------------------------------------------------------------------------------       
     1207      for teller = 1, RFXCountKF do 
     1208        luup.chdev.append(THIS_DEVICE, child_devices, RFXDevicesKF[teller], "RFX KF sensor #" .. RFXDevicesKF[teller] ,  
     1209        "urn:schemas-ActiveRFID:device:RFIDTransmitter:1","D_RFIDTransmitter.xml","","",false) 
     1210      end      
     1211      ------------------------------------------------------------------------------------       
     1212      -- Next with light switches 
     1213      ------------------------------------------------------------------------------------       
     1214      for teller = 1, RFXCountLS do 
     1215        luup.chdev.append(THIS_DEVICE, child_devices, RFXDevicesLS[teller], "RFX LS #" .. RFXDevicesLS[teller] ,  
     1216        "urn:schemas-upnp-org:device:BinaryLight:1","D_BinaryLight1.xml","","",false) 
     1217      end      
     1218      ------------------------------------------------------------------------------------       
     1219      -- Next Harrison window covering 
     1220      ------------------------------------------------------------------------------------       
     1221      for teller = 1, RFXCOM_HAR_Count do 
     1222        luup.chdev.append(THIS_DEVICE, child_devices, RFXCOM_HAR_Devices[teller],  
     1223          "Harrison #" .. RFXCOM_HAR_Devices[teller] , "urn:schemas-micasaverde-com:device:WindowCovering:1", 
     1224          "D_RFXCOMHarrison.xml","","",false) 
     1225      end 
     1226      ------------------------------------------------------------------------------------       
     1227      -- Next with temperature devices 
     1228      ------------------------------------------------------------------------------------       
     1229      for teller = 1, RFXCountTS2 do 
     1230        luup.log("debut temp:" .. RFXDevicesTS2[teller]) 
     1231        luup.chdev.append(THIS_DEVICE, child_devices, RFXDevicesTS2[teller], "RFX TS #" .. RFXDevicesTS2[teller] ,  
     1232        "urn:schemas-micasaverde-com:device:TemperatureSensor:1", "D_TemperatureSensor1.xml","","",false) 
     1233      end      
     1234      ------------------------------------------------------------------------------------       
     1235      -- Next with humidity devices 
     1236      ------------------------------------------------------------------------------------       
     1237      for teller = 1, RFXCountHS2 do 
     1238        luup.chdev.append(THIS_DEVICE, child_devices, RFXDevicesHS2[teller], "RFX HS #" .. RFXDevicesHS2[teller] ,  
     1239        "urn:schemas-micasaverde-com:device:HumiditySensor:1", "D_HumiditySensor1.xml","","",false) 
     1240      end  
     1241      ------------------------------------------------------------------------------------       
     1242      -- Next with barometer devices 
     1243      ------------------------------------------------------------------------------------       
     1244      for teller = 1, RFXCountBS2 do 
     1245        luup.chdev.append(THIS_DEVICE, child_devices, RFXDevicesBS2[teller], "RFX BS #" .. RFXDevicesBS2[teller] ,  
     1246        "urn:schemas-micasaverde-com:device:BarometerSensor:1", "D_BarometerSensor1.xml","","",false) 
     1247      end  
     1248       
     1249      ------------------------------------------------------------------------------------       
     1250      -- Next with rain devices 
     1251      ------------------------------------------------------------------------------------       
     1252      for teller = 1, RFXCountRS2 do 
     1253        luup.chdev.append(THIS_DEVICE, child_devices, RFXDevicesRS2[teller], "RFX RS #" .. RFXDevicesRS2[teller] ,  
     1254        "urn:schemas-micasaverde-com:device:RainSensor:1", "D_RainSensor1.xml","","",false) 
     1255      end  
     1256       
     1257      ------------------------------------------------------------------------------------ 
     1258      -- Next with EZAnemoSensor devices 
     1259      ------------------------------------------------------------------------------------ 
     1260      for teller = 1, RFXCountAS2 do 
     1261        luup.chdev.append(THIS_DEVICE, child_devices, RFXDevicesAS2[teller], "RFX AS #" .. RFXDevicesAS2[teller] , 
     1262        "urn:schemas-upnp-org:device:EZAnemoSensor:1", "D_EZAnemoSensor.xml","","",false) 
     1263      end 
     1264 
     1265      ------------------------------------------------------------------------------------       
     1266      -- Now add the new device to the tree depending on the sensor type 
     1267      ------------------------------------------------------------------------------------       
     1268 
     1269      luup.log("Tree buildup, now add " .. lsID .. " with sensortype " .. lnSensorType) 
     1270 
     1271      ------------------------------------------------------------------------------------       
     1272      -- Add if doorsensor 
     1273      ------------------------------------------------------------------------------------       
     1274      if bitw.band(lnSensorType,RFXConstants.sensorTypeDoor) ~= 0 then 
     1275        luup.log("CREATING CHILD DEVICE DOOR SENSOR WITH ID " .. lsID) 
     1276        luup.chdev.append(THIS_DEVICE, child_devices, lsID, "RFX DS sensor #" .. lsID ,  
     1277          "urn:schemas-micasaverde-com:device:DoorSensor:1","D_DoorSensor1.xml","","",false) 
     1278        RFXCountDS = RFXCountDS + 1 
     1279        RFXDevicesDS[RFXCountDS] = lsID 
     1280      end 
     1281      ------------------------------------------------------------------------------------       
     1282      -- Add if motionsensor 
     1283      ------------------------------------------------------------------------------------       
     1284      if bitw.band(lnSensorType,RFXConstants.sensorTypeMotion) ~= 0 then 
     1285        luup.log("CREATING CHILD DEVICE MOTION SENSOR WITH ID " .. lsID) 
     1286        luup.chdev.append(THIS_DEVICE, child_devices, lsID, "RFX MS sensor #" .. lsID ,  
     1287          "urn:schemas-micasaverde-com:device:MotionSensor:1","D_MotionSensor1.xml","","",false) 
     1288        RFXCountMS = RFXCountMS + 1 
     1289        RFXDevicesMS[RFXCountMS] = lsID 
     1290      end 
     1291      ------------------------------------------------------------------------------------       
     1292      -- Add if keybof 
     1293      ------------------------------------------------------------------------------------       
     1294      if bitw.band(lnSensorType,RFXConstants.sensorTypeKeyFob) ~= 0 then 
     1295        luup.log("CREATING CHILD DEVICE KEYFOB WITH ID " .. lsID) 
     1296        luup.chdev.append(THIS_DEVICE, child_devices, "KF/" .. lsID, "RFX KF sensor #" .. lsID ,  
     1297          "urn:schemas-ActiveRFID:device:RFIDTransmitter:1","D_RFIDTransmitter.xml","","",false) 
     1298        RFXCountKF = RFXCountKF + 1 
     1299        RFXDevicesKF[RFXCountKF] = "KF/" .. lsID 
     1300      end 
     1301      ------------------------------------------------------------------------------------       
     1302      -- Add if lightswitch 
     1303      ------------------------------------------------------------------------------------       
     1304      if bitw.band(lnSensorType,RFXConstants.sensorTypeControler) ~= 0 then 
     1305        luup.log("CREATING CHILD DEVICE LIGHT SWITCH WITH ID " .. lsID) 
     1306        luup.chdev.append(THIS_DEVICE, child_devices, lsID, "RFX LS #" .. lsID ,  
     1307          "urn:schemas-upnp-org:device:BinaryLight:1","D_BinaryLight1.xml","","",false) 
     1308        RFXCountLS = RFXCountLS + 1 
     1309        RFXDevicesLS[RFXCountLS] = lsID 
     1310      end      
     1311      ------------------------------------------------------------------------------------       
     1312      -- Add if Harrison window covering 
     1313      ------------------------------------------------------------------------------------       
     1314      if bitw.band(lnSensorType,RFXConstants.sensorTypeWindowCovering) ~= 0 then 
     1315        luup.chdev.append(THIS_DEVICE, child_devices, lsID, "Harrison #" .. lsID , 
     1316          "urn:schemas-micasaverde-com:device:WindowCovering:1","D_RFXCOMHarrison.xml","","",false)      
     1317        RFXCOM_HAR_Count = RFXCOM_HAR_Count + 1 
     1318        RFXCOM_HAR_Devices[RFXCOM_HAR_Count] = lsID 
     1319      end  
     1320      ------------------------------------------------------------------------------------       
     1321      -- Add temperature sensor 
     1322      ------------------------------------------------------------------------------------       
     1323      if bitw.band(lnSensorType,RFXConstants.sensorTypeTemperature) ~= 0 then 
     1324          luup.chdev.append(THIS_DEVICE, child_devices, "TS/" .. lsID, "RFX TS #" .. lsID, 
     1325          "urn:schemas-micasaverde-com:device:TemperatureSensor:1", "D_TemperatureSensor1.xml","","",false) 
     1326        RFXCountTS2 = RFXCountTS2 + 1 
     1327        RFXDevicesTS2[RFXCountTS2] = "TS/" .. lsID 
     1328      end  
     1329      ------------------------------------------------------------------------------------       
     1330      -- Add humidity sensor 
     1331      ------------------------------------------------------------------------------------       
     1332      if bitw.band(lnSensorType,RFXConstants.sensorTypeHumidity) ~= 0 then 
     1333          luup.chdev.append(THIS_DEVICE, child_devices, "HS/" .. lsID, "RFX HS #" .. lsID, 
     1334          "urn:schemas-micasaverde-com:device:HumiditySensor:1", "D_HumiditySensor1.xml","","",false) 
     1335        RFXCountHS2 = RFXCountHS2 + 1 
     1336        RFXDevicesHS2[RFXCountHS2] = "HS/" .. lsID 
     1337      end  
     1338      ------------------------------------------------------------------------------------       
     1339      -- Add barometer sensor 
     1340      ------------------------------------------------------------------------------------       
     1341      if bitw.band(lnSensorType,RFXConstants.sensorTypeBarometer) ~= 0 then 
     1342          luup.chdev.append(THIS_DEVICE, child_devices, "BS/" .. lsID, "RFX BS #" .. lsID, 
     1343          "urn:schemas-micasaverde-com:device:BarometerSensor:1", "D_BarometerSensor1.xml","","",false) 
     1344        RFXCountBS2 = RFXCountBS2 + 1 
     1345        RFXDevicesBS2[RFXCountBS2] = "BS/" .. lsID 
     1346      end  
     1347       
     1348      ------------------------------------------------------------------------------------       
     1349      -- Add rain sensor 
     1350      ------------------------------------------------------------------------------------       
     1351      if bitw.band(lnSensorType,RFXConstants.sensorTypeRain) ~= 0 then 
     1352          luup.chdev.append(THIS_DEVICE, child_devices, "RS/" .. lsID, "RFX RS #" .. lsID, 
     1353          "urn:schemas-micasaverde-com:device:RainSensor:1", "D_RainSensor1.xml","","",false) 
     1354        RFXCountRS2 = RFXCountRS2 + 1 
     1355        RFXDevicesRS2[RFXCountRS2] = "RS/" .. lsID 
     1356      end  
     1357       
     1358      ------------------------------------------------------------------------------------ 
     1359      -- Add D_EZAnemoSensor sensor 
     1360      ------------------------------------------------------------------------------------ 
     1361      if bitw.band(lnSensorType,RFXConstants.sensorTypeWindDirection) ~= 0 then 
     1362        luup.chdev.append(THIS_DEVICE, child_devices, "AS/" .. lsID, "RFX AS #" .. lsID, 
     1363        "urn:schemas-upnp-org:device:EZAnemoSensor:1", "D_EZAnemoSensor.xml","","",false) 
     1364        RFXCountAS2 = RFXCountAS2 + 1 
     1365        RFXDevicesAS2[RFXCountAS2] = "AS/" .. lsID 
     1366      end 
     1367       
     1368       
     1369       
     1370      luup.log("Tree buildup with number child devices:") 
     1371      luup.log("       door sensors: " .. RFXCountDS) 
     1372      luup.log("     motion sensors: " .. RFXCountMS) 
     1373      luup.log("     keyfob sensors: " .. RFXCountKF) 
     1374      luup.log("     light switches: " .. RFXCountLS) 
     1375      luup.log("   window coverings: " .. RFXCOM_HAR_Count) 
     1376      luup.log("temperature sensors: " .. RFXCountTS2) 
     1377      luup.log("   humidity sensors: " .. RFXCountHS2) 
     1378      luup.log("   pressure sensors: " .. RFXCountBS2) 
     1379      luup.log("       rain sensors: " .. RFXCountRS2) 
     1380      luup.log("      Anemo sensors: " .. RFXCountAS2) 
     1381       
     1382      -- Synch the new tree with the old three 
     1383      luup.log("Start sync") 
     1384      luup.chdev.sync(THIS_DEVICE, child_devices) 
     1385      luup.log("End sync")        
     1386    end 
     1387     
     1388    ------------------------------------------------------------------------------ 
     1389    -- Deliver message to all devices with the correct altid (multiple 
     1390    -- devices can have the correct altid!) 
     1391    ------------------------------------------------------------------------------ 
     1392     
     1393    local fullId = "" 
     1394    for k, v in pairs(luup.devices) do 
     1395      -- Check if we have a device with the correct parent (THIS_DEVICE) and  
     1396      -- altid we are looking for (lsID) 
     1397      if (v.device_num_parent == THIS_DEVICE) then 
     1398        device = k 
     1399 
     1400        -- luup.log("Device found or created, now processing command " .. lsCmd .. " for " .. lsID) 
     1401        -- luup.log("Device Number: " .. k .. 
     1402        --    " v.device_type: " .. tostring(v.device_type) .. 
     1403        --    " v.device_num_parent: " .. tostring(v.device_num_parent) .. 
     1404        --    " v.id: " .. tostring(v.id) 
     1405        -- ) 
     1406       
     1407        -- Execution door sensors same as motion sensors 
     1408        if bitw.band(lnSensorType,RFXConstants.sensorTypeDoor) ~= 0 or bitw.band(lnSensorType,RFXConstants.sensorTypeMotion) ~= 0  
     1409          and v.id == lsId then 
     1410          local TrippedValue = "0" 
     1411          if lsCmd == RFXConstants.cmdAlert then TrippedValue = "1" end 
     1412          luup.log( "Set tripped value " .. TrippedValue .. " to device " .. lsID )  
     1413          luup.variable_set("urn:micasaverde-com:serviceId:SecuritySensor1", "Tripped", TrippedValue, device) 
     1414        end 
     1415 
     1416        -- Execution for keyfobs 
     1417        -- Arm or Disarm all door and motion sensors when keyfob armed or disarmed 
     1418        -- Set trip state keyfob as indication home or away 
     1419        fullId = "KF/" .. lsID 
     1420        if bitw.band(lnSensorType,RFXConstants.sensorTypeKeyFob) ~= 0  
     1421            and v.id == fullId then 
     1422          luup.log("KeyFob action: " .. lsCmd)  
     1423           
     1424          if lsCmd == RFXConstants.cmdArmAway or lsCmd == RFXConstants.cmdArmHome or lsCmd == RFXConstants.cmdDisarm then 
     1425            ArmDisarmAll(lsCmd) 
     1426          --  local newArmedValue = "1" -- armed 
     1427          --  local newTrippedValue = "1" -- home 
     1428     
     1429          --  if lsCmd == RFXConstants.cmdDisarm then  
     1430          --  newArmedValue = "0"  
     1431          --  newTrippedValue = "0" 
     1432          --  end 
     1433          --  if lsCmd == RFXConstants.cmdArmAway then newTrippedValue = "0" end 
     1434          --    luup.log( "Set ArmedValue value " .. newArmedValue .. " to device " .. device )  
     1435          --  luup.variable_set("urn:micasaverde-com:serviceId:SecuritySensor1","Armed",newArmedValue, device) 
     1436          --  luup.variable_set("urn:micasaverde-com:serviceId:SecuritySensor1", "Tripped", newTrippedValue, device) 
     1437          elseif lsCmd == RFXConstants.cmdPanic then 
     1438            -- not implemented 
     1439          elseif lsCmd == RFXConstants.cmdOn  then 
     1440            luup.log("urn:ActiveRFID:serviceId:RFIDTransmitter1  Action:On:Portail" .. fullId)  
     1441            local Pstatus = luup.variable_get("urn:upnp-org:serviceId:SwitchPower1","Status",26) 
     1442            if Pstatus == "0" then 
     1443              luup.variable_set("urn:upnp-org:serviceId:SwitchPower1","Status","1",26) 
     1444            else 
     1445              luup.variable_set("urn:upnp-org:serviceId:SwitchPower1","Status","0",26) 
     1446            end 
     1447          elseif lsCmd == RFXConstants.cmdOff then 
     1448            luup.log("urn:ActiveRFID:serviceId:RFIDTransmitter1  Action:Off:Garage" .. fullId)  
     1449            local Pstatus = luup.variable_get("urn:upnp-org:serviceId:SwitchPower1","Status",27) 
     1450            if Pstatus == "0" then 
     1451              luup.variable_set("urn:upnp-org:serviceId:SwitchPower1","Status","1",27) 
     1452            else 
     1453              luup.variable_set("urn:upnp-org:serviceId:SwitchPower1","Status","0",27) 
     1454            end 
     1455          end      
     1456        end  
     1457 
     1458        -- Execution light switches.  
     1459        -- Set Status based on cmdOn or cmdOff 
     1460        if bitw.band(lnSensorType,RFXConstants.sensorTypeControler) ~= 0  
     1461           and v.id == lsID then 
     1462          local newTargetValue = "0" 
     1463           
     1464          if lsCmd == RFXConstants.cmdOn then newTargetValue = "1" end 
     1465          if lsCmd == RFXConstants.cmdGroupOn then newTargetValue = "1" end 
     1466           
     1467          luup.log("SET TARGET from " .. lsID .. " with value " .. newTargetValue) 
     1468          luup.variable_set("urn:upnp-org:serviceId:SwitchPower1","Status",newTargetValue,device) 
     1469        end      
     1470         
     1471        -- Execution for temperature sensor 
     1472        -- Set temperature to command 
     1473        fullId = "TS/" .. lsID 
     1474        if bitw.band(lnSensorType,RFXConstants.sensorTypeTemperature) ~= 0  
     1475            and tostring(v.device_type) == "urn:schemas-micasaverde-com:device:TemperatureSensor:1"  
     1476            and v.id == fullId then 
     1477            for lsKey,lsValue in string.gmatch(lsCmd, "(%w+)=([%w%.%-_]+)") do 
     1478            -- luup.log("Key=" ..  lsKey .. ";Value=" .. lsValue .. ";") 
     1479            if lsKey == "temperature" then 
     1480              luup.log("SET CURRENTTEMPERATURE from " .. lsID .. " with value " .. lsValue) 
     1481              luup.variable_set("urn:upnp-org:serviceId:TemperatureSensor1", "CurrentTemperature", 
     1482                lsValue, device) 
     1483            elseif lsKey == "battery" then 
     1484              luup.log("SET BATTERY from " .. lsID .. " with value " .. lsValue) 
     1485              luup.variable_set("urn:micasaverde-com:serviceId:HaDevice1", "BatteryLevel", lsValue, device) 
     1486            end 
     1487          end 
     1488        end 
     1489 
     1490        -- Execution for humidity sensor 
     1491        -- Set humidity to command  
     1492        fullId = "HS/" .. lsID 
     1493        if bitw.band(lnSensorType,RFXConstants.sensorTypeHumidity) ~= 0  
     1494            and tostring(v.device_type) == "urn:schemas-micasaverde-com:device:HumiditySensor:1"  
     1495            and v.id == fullId then 
     1496            for lsKey,lsValue in string.gmatch(lsCmd, "(%w+)=([%w%.%-_]+)") do 
     1497            -- luup.log("Key=" ..  lsKey .. ";Value=" .. lsValue .. ";") 
     1498            if lsKey == "humidity" then 
     1499              luup.log("SET CURRENTLEVEL from " .. lsID .. " with value " .. lsValue) 
     1500              luup.variable_set("urn:micasaverde-com:serviceId:HumiditySensor1", "CurrentLevel", 
     1501                lsValue, device) 
     1502            elseif lsKey == "battery" then 
     1503              luup.log("SET BATTERY from " .. lsID .. " with value " .. lsValue) 
     1504              luup.variable_set("urn:micasaverde-com:serviceId:HaDevice1", "BatteryLevel", lsValue, device) 
     1505            end 
     1506          end 
     1507        end  
     1508 
     1509        -- Execution for pressure sensor 
     1510        -- Set pressure to command 
     1511        fullId = "BS/" .. lsID 
     1512        if bitw.band(lnSensorType,RFXConstants.sensorTypeBarometer) ~= 0  
     1513            and tostring(v.device_type) == "urn:schemas-micasaverde-com:device:BarometerSensor:1"  
     1514            and v.id == fullId then 
     1515            for lsKey,lsValue in string.gmatch(lsCmd, "(%w+)=([%w%.%-_]+)") do 
     1516            -- luup.log("Key=" ..  lsKey .. ";Value=" .. lsValue .. ";") 
     1517            if lsKey == "pressure" then 
     1518              luup.log("SET CURRENTPRESSURE from " .. lsID .. " with value " .. lsValue) 
     1519              luup.variable_set("urn:upnp-org:serviceId:BarometerSensor1", "CurrentPressure", 
     1520                lsValue, device) 
     1521            elseif lsKey == "battery" then 
     1522              luup.log("SET BATTERY from " .. lsID .. " with value " .. lsValue) 
     1523              luup.variable_set("urn:micasaverde-com:serviceId:HaDevice1", "BatteryLevel", lsValue, device) 
     1524            end 
     1525          end 
     1526        end 
     1527        fullId = "RS/" .. lsID 
     1528        if bitw.band(lnSensorType,RFXConstants.sensorTypeRain) ~= 0  
     1529            and tostring(v.device_type) == "urn:schemas-micasaverde-com:device:RainSensor:1"  
     1530            and v.id == fullId then 
     1531            for lsKey,lsValue in string.gmatch(lsCmd, "(%w+)=([%w%.%-_]+)") do 
     1532            -- luup.log("Key=" ..  lsKey .. ";Value=" .. lsValue .. ";") 
     1533            if lsKey == "rain" then 
     1534              luup.log("SET CURRENTRAIN from " .. lsID .. " with value " .. lsValue) 
     1535              luup.variable_set("urn:upnp-org:serviceId:RainSensor1", "CurrentRain", lsValue, device) 
     1536            elseif lsKey == "train" then 
     1537              luup.log("SET CURRENTTRAIN from " .. lsID .. " with value " .. lsValue) 
     1538              luup.variable_set("urn:upnp-org:serviceId:RainSensor1", "CurrentTRain", lsValue, device)  
     1539            elseif lsKey == "battery" then 
     1540              luup.log("SET BATTERY from " .. lsID .. " with value " .. lsValue) 
     1541              luup.variable_set("urn:micasaverde-com:serviceId:HaDevice1", "BatteryLevel", lsValue, device) 
     1542            end 
     1543          end 
     1544        end 
     1545        fullId = "AS/" .. lsID 
     1546        if bitw.band(lnSensorType,RFXConstants.sensorTypeWindDirection) ~= 0 
     1547            and tostring(v.device_type) == "urn:schemas-upnp-org:device:EZAnemoSensor:1" 
     1548            and v.id == fullId then 
     1549            for lsKey,lsValue in string.gmatch(lsCmd, "(%w+)=([%w%.%-_]+)") do 
     1550              luup.log("Key=" ..  lsKey .. ";Value=" .. lsValue .. ";") 
     1551              if lsKey == "direction" then 
     1552                luup.log("SET DIRECTION from " .. lsID .. " with value " .. lsValue) 
     1553                luup.variable_set("urn:upnp-org:serviceId:EZAnemoSensor1", "Direction", lsValue, device) 
     1554              end 
     1555              if lsKey == "speed" then 
     1556                luup.log("SET SPEED from " .. lsID .. " with value " .. lsValue) 
     1557                luup.variable_set("urn:upnp-org:serviceId:EZAnemoSensor1", "Speed", lsValue, device) 
     1558              end 
     1559              if lsKey == "speedav" then 
     1560                luup.log("SET SPEEDAV from " .. lsID .. " with value " .. lsValue) 
     1561                luup.variable_set("urn:upnp-org:serviceId:EZAnemoSensor1", "SpeedAv", lsValue, device) 
     1562              end 
     1563              if lsKey == "battery" then 
     1564                luup.log("SET BATTERY from " .. lsID .. " with value " .. lsValue) 
     1565                luup.variable_set("urn:upnp-org:serviceId:EZAnemoSensor1", "Battery", lsValue, device) 
     1566              end 
     1567            end 
     1568        end 
     1569      end 
     1570       
     1571    end  
     1572 
     1573  end 
    15621574  
    1563     ------------------------------------------------------------------------ 
    1564     -- Arm or Disarm 
    1565     --  
    1566     -- Arm or Disarm all motion and door sensors based on command given. 
    1567     -- KeyFob sensor gets armed or disarmed also and trip state  
    1568     -- is used to indicate home or away status (tripped = home) 
    1569     ------------------------------------------------------------------------ 
    1570      
    1571     function ArmDisarmAll(lsCmd) 
    1572         local newArmedValue = "1" -- armed 
    1573         local newTrippedValue = "1" -- home 
    1574          
    1575         if lsCmd == RFXConstants.cmdDisarm then  
    1576             newArmedValue = "0"  
    1577             newTrippedValue = "0" 
    1578         end 
    1579         if lsCmd == RFXConstants.cmdArmAway then  
    1580           newTrippedValue = "0"  
    1581         end 
    1582          
    1583         -- Iterate all door sensors and set armed value  
    1584         for teller = 1, RFXCountDS do 
    1585             local lsOtherDevices = findChild(THIS_DEVICE, RFXDevicesDS[teller] ) 
    1586              
    1587             luup.log("Action::SetArmed: " .. newArmedValue .. " for door sensor device " .. RFXDevicesDS[teller] .. "/" .. lsOtherDevices) 
    1588             luup.variable_set("urn:micasaverde-com:serviceId:SecuritySensor1","Armed",newArmedValue,lsOtherDevices) 
    1589       end 
    1590  
    1591         -- Iterate all  motion sensors and set armed value  
    1592         for teller = 1, RFXCountMS do 
    1593             local lsOtherDevices = findChild(THIS_DEVICE, RFXDevicesMS[teller] ) 
    1594              
    1595             luup.log("Action::SetArmed: " .. newArmedValue .. " for motion sensor device " .. RFXDevicesMS[teller] .. "/" .. lsOtherDevices) 
    1596             luup.variable_set("urn:micasaverde-com:serviceId:SecuritySensor1","Armed",newArmedValue,lsOtherDevices) 
    1597          
    1598          end     
    1599  
    1600         -- Iterate all keyfobs and set armed value and  
    1601         -- set tripped value based on armed-home (1) or armed-away (0) 
    1602         for teller = 1, RFXCountKF do 
    1603             local lsOtherDevices = findChild(THIS_DEVICE, RFXDevicesKF[teller] ) 
    1604              
    1605             luup.log("Action::SetArmed: " .. newArmedValue .. "for KeyFob device " .. RFXDevicesKF[teller] .. "/" .. lsOtherDevices) 
    1606             luup.variable_set("urn:micasaverde-com:serviceId:SecuritySensor1","Armed",newArmedValue,lsOtherDevices) 
    1607  
    1608             luup.log("Action::SetTripped: " .. newTrippedValue .. "for KeyFob device " .. RFXDevicesKF[teller] .. "/" .. lsOtherDevices) 
    1609             luup.variable_set("urn:micasaverde-com:serviceId:SecuritySensor1","Tripped",newTrippedValue,lsOtherDevices)          
    1610         end              
    1611     end 
    1612      
    1613      
     1575  ------------------------------------------------------------------------ 
     1576  -- Arm or Disarm 
     1577  --  
     1578  -- Arm or Disarm all motion and door sensors based on command given. 
     1579  -- KeyFob sensor gets armed or disarmed also and trip state  
     1580  -- is used to indicate home or away status (tripped = home) 
     1581  ------------------------------------------------------------------------ 
     1582   
     1583  function ArmDisarmAll(lsCmd) 
     1584    local newArmedValue = "1" -- armed 
     1585    local newTrippedValue = "1" -- home 
     1586     
     1587    if lsCmd == RFXConstants.cmdDisarm then  
     1588      newArmedValue = "0"  
     1589      newTrippedValue = "0" 
     1590    end 
     1591    if lsCmd == RFXConstants.cmdArmAway then  
     1592      newTrippedValue = "0"  
     1593    end 
     1594     
     1595    -- Iterate all door sensors and set armed value  
     1596    for teller = 1, RFXCountDS do 
     1597      local lsOtherDevices = findChild(THIS_DEVICE, RFXDevicesDS[teller] ) 
     1598       
     1599      luup.log("Action::SetArmed: " .. newArmedValue .. " for door sensor device " .. RFXDevicesDS[teller] .. "/" .. lsOtherDevices) 
     1600      luup.variable_set("urn:micasaverde-com:serviceId:SecuritySensor1","Armed",newArmedValue,lsOtherDevices) 
     1601    end 
     1602 
     1603    -- Iterate all  motion sensors and set armed value  
     1604    for teller = 1, RFXCountMS do 
     1605      local lsOtherDevices = findChild(THIS_DEVICE, RFXDevicesMS[teller] ) 
     1606       
     1607      luup.log("Action::SetArmed: " .. newArmedValue .. " for motion sensor device " .. RFXDevicesMS[teller] .. "/" .. lsOtherDevices) 
     1608      luup.variable_set("urn:micasaverde-com:serviceId:SecuritySensor1","Armed",newArmedValue,lsOtherDevices) 
     1609     
     1610     end   
     1611 
     1612    -- Iterate all keyfobs and set armed value and  
     1613    -- set tripped value based on armed-home (1) or armed-away (0) 
     1614    for teller = 1, RFXCountKF do 
     1615      local lsOtherDevices = findChild(THIS_DEVICE, RFXDevicesKF[teller] ) 
     1616       
     1617      luup.log("Action::SetArmed: " .. newArmedValue .. "for KeyFob device " .. RFXDevicesKF[teller] .. "/" .. lsOtherDevices) 
     1618      luup.variable_set("urn:micasaverde-com:serviceId:SecuritySensor1","Armed",newArmedValue,lsOtherDevices) 
     1619 
     1620      luup.log("Action::SetTripped: " .. newTrippedValue .. "for KeyFob device " .. RFXDevicesKF[teller] .. "/" .. lsOtherDevices) 
     1621      luup.variable_set("urn:micasaverde-com:serviceId:SecuritySensor1","Tripped",newTrippedValue,lsOtherDevices)      
     1622    end        
     1623  end 
     1624   
     1625   
    16141626  
    16151627    -- Thanks to guessed for this snippet. 
    16161628    function findChild(parentDevice, label) 
    16171629      for k, v in pairs(luup.devices) do 
    1618            if (v.device_num_parent == parentDevice and string.find(v.id, label .. "$", 1) ~= nil) then 
    1619                return k 
     1630         if (v.device_num_parent == parentDevice and string.find(v.id, label .. "$", 1) ~= nil) then 
     1631           return k 
    16201632        end 
    16211633      end 
     
    16581670 
    16591671  <startup>mainStartup</startup> 
    1660     <actionList> 
    1661         <action> 
    1662             <serviceId>urn:upnp-org:serviceId:SwitchPower1</serviceId> 
    1663             <name>SetTarget</name> 
    1664             <run> 
    1665                 local ls_id = luup.devices[lul_device].id 
    1666                 local lul_reverse = luup.variable_get("urn:micasaverde-com:serviceId:HaDevice1","ReverseOnOff",lul_device) 
    1667                  
    1668                 luup.log("SET TARGET from " .. ls_id .. " with value " .. lul_settings.newTargetValue) 
    1669                  
    1670                 if ( lul_settings.newTargetValue == "1"  or (lul_settings.newTargetValue=="0" and lul_reverse=="1") ) then 
    1671                     SendCmd( ls_id,RFXConstants.cmdOn ) 
    1672                 else 
    1673                     SendCmd( ls_id,RFXConstants.cmdOff ) 
    1674                 end 
    1675                 luup.variable_set("urn:upnp-org:serviceId:SwitchPower1","Status",lul_settings.newTargetValue,lul_device) 
    1676             </run> 
    1677         </action> 
    1678  
    1679         <action>  
    1680             <serviceId>urn:micasaverde-com:serviceId:SecuritySensor1</serviceId>  
    1681             <name>SetArmed</name>  
    1682             <run>  
    1683                 --debug("Action::SetArmed:"..lul_settings.newArmedValue)  
    1684                 luup.variable_set("urn:micasaverde-com:serviceId:SecuritySensor1", "Armed", lul_settings.newArmedValue, lul_device)  
    1685             </run>  
    1686         </action>  
    1687  
    1688         <action>  
    1689             <serviceId>urn:micasaverde-com:serviceId:HaDevice1</serviceId>  
    1690             <name>ToggleState</name>  
    1691             <run>  
    1692                 --debug("Action::ToggleState:" .. lul_device)  
    1693             </run>  
    1694         </action>  
    1695            
    1696  
    1697         <action> 
    1698             <serviceId>urn:upnp-esweb-nl:serviceId:rfxcom1</serviceId> 
    1699             <name>AddHarrisonCurtain</name> 
    1700             <run> 
    1701                 local lsHouseCode = lul_settings.HouseCode 
    1702                 actOnMessage(RFXConstants.sensorTypeWindowCovering,lsHouseCode,"") 
    1703             </run> 
    1704         </action> 
    1705         <action> 
    1706             <serviceId>urn:upnp-org:serviceId:WindowCovering1</serviceId> 
    1707             <name>Stop</name> 
    1708             <run> 
    1709               local har_id = luup.devices[lul_device].id 
    1710               SendHarrisonCmd( har_id, "STOP") 
    1711             </run> 
    1712           </action> 
    1713           <action>      
    1714             <serviceId>urn:upnp-org:serviceId:WindowCovering1</serviceId> 
    1715             <name>Up</name> 
    1716             <run> 
    1717               luup.variable_set("urn:upnp-esweb-nl:serviceId:RFXCOMHarrison1","CurtainStatus",0,lul_device) 
    1718               local har_id = luup.devices[lul_device].id 
    1719               SendHarrisonCmd( har_id, "OPEN") 
    1720             </run> 
    1721           </action> 
    1722           <action> 
    1723             <serviceId>urn:upnp-org:serviceId:WindowCovering1</serviceId> 
    1724             <name>Down</name> 
    1725             <run> 
    1726               luup.variable_set("urn:upnp-esweb-nl:serviceId:RFXCOMHarrison1","CurtainStatus",1,lul_device) 
    1727               local har_id = luup.devices[lul_device].id 
    1728               SendHarrisonCmd( har_id, "CLOSE") 
    1729             </run> 
    1730           </action> 
    1731  
    1732           <action> 
    1733             <serviceId>urn:upnp-esweb-nl:serviceId:RFXCOMHarrison1</serviceId> 
    1734             <name>SetStatus</name> 
    1735             <run> 
    1736                luup.variable_set("urn:upnp-esweb-nl:serviceId:RFXCOMHarrison1","CurtainStatus",lul_settings.newStatusValue,lul_device) 
    1737                local har_id = luup.devices[lul_device].id 
    1738                if ( lul_settings.newStatusValue == "1" ) then 
    1739                 SendHarrisonCmd( har_id, "OPEN") 
    1740                else 
    1741                 SendHarrisonCmd( har_id, "CLOSE") 
    1742                end 
    1743             </run> 
    1744           </action> 
    1745           <action> 
    1746             <serviceId>urn:upnp-esweb-nl:serviceId:RFXCOMHarrison1</serviceId> 
    1747             <name>GetStatus</name> 
    1748             <run> 
    1749                lul_settings.ResultStatus = luup.variable_get("urn:upnp-esweb-nl:serviceId:RFXCOMHarrison1","CurtainStatus",lul_device) 
    1750             </run> 
    1751           </action> 
    1752     </actionList> 
     1672  <actionList> 
     1673    <action> 
     1674      <serviceId>urn:upnp-org:serviceId:SwitchPower1</serviceId> 
     1675      <name>SetTarget</name> 
     1676      <run> 
     1677        local ls_id = luup.devices[lul_device].id 
     1678        local lul_reverse = luup.variable_get("urn:micasaverde-com:serviceId:HaDevice1","ReverseOnOff",lul_device) 
     1679         
     1680        luup.log("SET TARGET from " .. ls_id .. " with value " .. lul_settings.newTargetValue) 
     1681         
     1682        if ( lul_settings.newTargetValue == "1"  or (lul_settings.newTargetValue=="0" and lul_reverse=="1") ) then 
     1683          SendCmd( ls_id,RFXConstants.cmdOn ) 
     1684        else 
     1685          SendCmd( ls_id,RFXConstants.cmdOff ) 
     1686        end 
     1687        luup.variable_set("urn:upnp-org:serviceId:SwitchPower1","Status",lul_settings.newTargetValue,lul_device) 
     1688      </run> 
     1689    </action> 
     1690 
     1691    <action>  
     1692      <serviceId>urn:micasaverde-com:serviceId:SecuritySensor1</serviceId>  
     1693      <name>SetArmed</name>  
     1694      <run>  
     1695        --debug("Action::SetArmed:"..lul_settings.newArmedValue)  
     1696        luup.variable_set("urn:micasaverde-com:serviceId:SecuritySensor1", "Armed", lul_settings.newArmedValue, lul_device)  
     1697      </run>  
     1698    </action>  
     1699 
     1700    <action>  
     1701      <serviceId>urn:micasaverde-com:serviceId:HaDevice1</serviceId>  
     1702      <name>ToggleState</name>  
     1703      <run>  
     1704        --debug("Action::ToggleState:" .. lul_device)  
     1705      </run>  
     1706    </action>  
     1707       
     1708 
     1709    <action> 
     1710      <serviceId>urn:upnp-esweb-nl:serviceId:rfxcom1</serviceId> 
     1711      <name>AddHarrisonCurtain</name> 
     1712      <run> 
     1713        local lsHouseCode = lul_settings.HouseCode 
     1714        actOnMessage(RFXConstants.sensorTypeWindowCovering,lsHouseCode,"") 
     1715      </run> 
     1716    </action> 
     1717    <action> 
     1718      <serviceId>urn:upnp-org:serviceId:WindowCovering1</serviceId> 
     1719      <name>Stop</name> 
     1720      <run> 
     1721        local har_id = luup.devices[lul_device].id 
     1722        SendHarrisonCmd( har_id, "STOP") 
     1723      </run> 
     1724      </action> 
     1725      <action>      
     1726      <serviceId>urn:upnp-org:serviceId:WindowCovering1</serviceId> 
     1727      <name>Up</name> 
     1728      <run> 
     1729        luup.variable_set("urn:upnp-esweb-nl:serviceId:RFXCOMHarrison1","CurtainStatus",0,lul_device) 
     1730        local har_id = luup.devices[lul_device].id 
     1731        SendHarrisonCmd( har_id, "OPEN") 
     1732      </run> 
     1733      </action> 
     1734      <action> 
     1735      <serviceId>urn:upnp-org:serviceId:WindowCovering1</serviceId> 
     1736      <name>Down</name> 
     1737      <run> 
     1738        luup.variable_set("urn:upnp-esweb-nl:serviceId:RFXCOMHarrison1","CurtainStatus",1,lul_device) 
     1739        local har_id = luup.devices[lul_device].id 
     1740        SendHarrisonCmd( har_id, "CLOSE") 
     1741      </run> 
     1742      </action> 
     1743 
     1744      <action> 
     1745      <serviceId>urn:upnp-esweb-nl:serviceId:RFXCOMHarrison1</serviceId> 
     1746      <name>SetStatus</name> 
     1747      <run> 
     1748         luup.variable_set("urn:upnp-esweb-nl:serviceId:RFXCOMHarrison1","CurtainStatus",lul_settings.newStatusValue,lul_device) 
     1749         local har_id = luup.devices[lul_device].id 
     1750         if ( lul_settings.newStatusValue == "1" ) then 
     1751        SendHarrisonCmd( har_id, "OPEN") 
     1752         else 
     1753        SendHarrisonCmd( har_id, "CLOSE") 
     1754         end 
     1755      </run> 
     1756      </action> 
     1757      <action> 
     1758      <serviceId>urn:upnp-esweb-nl:serviceId:RFXCOMHarrison1</serviceId> 
     1759      <name>GetStatus</name> 
     1760      <run> 
     1761         lul_settings.ResultStatus = luup.variable_get("urn:upnp-esweb-nl:serviceId:RFXCOMHarrison1","CurtainStatus",lul_device) 
     1762      </run> 
     1763      </action> 
     1764  </actionList> 
    17531765</implementation> 
Note: See TracChangeset for help on using the changeset viewer.