uniPi I/O hardware script for LUA
Posted: Tuesday 08 September 2015 7:25
by spudgunman
so I am not a good enough coder to make a change to the domoticz source for a new hardware type (I couldnt figure out where to start really) so I just hacked up a huge LUA script that makes the uniPi hardware work.. this script is in beta still but most all functions work minus the one wire (I just use OWFS so havent gone to the trouble yet in this script) and the Analog output isnt 100% awesome functionality.. I will update post when there is worthy updates..
I would love some feedback and testing if you have the hardware - its good hardware to make your home security system with.
I also hope the code paste works [Expand the spoiler below to see code, did this to keep thread looking neat]
I would love some feedback and testing if you have the hardware - its good hardware to make your home security system with.
I also hope the code paste works [Expand the spoiler below to see code, did this to keep thread looking neat]
- Spoiler: show
Code: Select all
--uniPi Domoticz LUA Script b0.98 ------------------------------------------------------------------------------------ -- Setup Instructions -- -- Required that to use this script you have Domoticz fully working on a raspberryPi -- Additionally you will need the uniPi hardware module installed and the evok uniPI API working -- you can use the installer or my manual tutorial here http://unipi.technology/forum/viewtopic.php?f=6&t=95 -- it is mandatory that evok API/website is working correctly before this script will function! -- -- This script also requires json for lua http://regex.info/blog/lua/json -- rough install guide create the required directory 'sudo mkdir /usr/local/share/lua/5.2/' -- put the json script in the directory 'sudo cp json.lua /usr/local/share/lua/5.2/' -- -- In Domoticz you will need to create virtual hardware device and name it something like uniPi -- In the Domoticz hardware tab click setup for uniPi and add 8 virtual 'switch' devices for the relays -- name the switch devices whatever you want this script assumes the name "uniPi Relay1","uniPi Relay2" etc -- Go to the device tab in Domoticz and make note of the idX number (the far left column near the icons) -- Now update the user settings below for virtual device names and IDx values to match your config in domoticz -- -- Do the same for the remaining devices (for analog input use voltage devices, for digital inputs use switches -- for analog output use a slider/dimmer switch -- -- beta not working for one wire therm via this script yet either in beta yet -- -- -- ------------------------------------- -- Evok API hostname and port -- -- normally just need port changed -- ------------------------------------- evokurl = "localhost:8181" ------------------------------------- -- Domoticz JSON hostname:port -- ------------------------------------- domoticzjson = "localhost:8080" ---------------------------------------------------------------- -- User Settings Below for Virtual Device Names and IDx values-- ---------------------------------------------------------------- --the following is the IDx for the relay switches with uniPi there are 8 relays create as normal switches idxr1 = 40 relay1name = "uniPi Relay1" idxr2 = 41 relay2name = "uniPi Relay2" idxr3 = 42 relay3name = "uniPi Relay3" idxr4 = 43 relay4name = "uniPi Relay4" idxr5 = 44 relay5name = "uniPi Relay5" idxr6 = 45 relay6name = "uniPi Relay6" idxr7 = 46 relay7name = "uniPi Relay7" idxr8 = 47 relay8name = "uniPi Relay8" --the following is the IDx for the analog inputs with uniPi there are 2 inputs you should create them as virtual voltage meters --to use these you also need to create a user variable as a floating variable and name it 'ai1_value' assign a default level of zero --and respectivly 'ai2_value' for the 2nd sensor also floating and also starting with zero idxai1 = 48 ai1name = "uniPi AnalogIn1" idxai2 = 49 ai2name = "uniPi AnalogIn2" --the following is the IDx for the analog outputs with uniPi there is 1 output create as a normal switch idxao1 = 52 ao1name = "uniPi AnalogOut1" --the following is the IDx for the digital inputs with uniPi there are 12 inputs create as normal switches idxdi1 = 51 di1name = "uniPi Digital Input1" idxdi2 = 65 di2name = "uniPi Digital Input2" idxdi3 = 66 di3name = "uniPi Digital Input3" idxdi4 = 67 di4name = "uniPi Digital Input4" idxdi5 = 68 di5name = "uniPi Digital Input5" idxdi6 = 69 di6name = "uniPi Digital Input6" idxdi7 = 70 di7name = "uniPi Digital Input7" idxdi8 = 71 di8name = "uniPi Digital Input8" idxdi9 = 72 di9name = "uniPi Digital Input9" idxdi10 = 601 di10name = "uniPi Digital Input10" idxdi11 = 611 di11name = "uniPi Digital Input11" idxdi12 = 621 di12name = "uniPi Digital Input12" ------------------------------------------------------------------------------ --the following is the one wire tempature sensors, required that you update --with your SN unique to your one wire device, this can be obtained by issuing --the following to the uniPI API http://your.system.ip:PORT/rest/all --then figure out which devices in the dev.temp you want to monitor. --there is no limit to the devices here ------------------------------------------------------------------------------ OWSN1 = "2814228A060000F1" OWSN2 = "updateMe" OWSN3 = "updateMe" OWSN4 = "updateMe" ------------------------------------------------------------------------------------------ -- uniPi Domoticz Lua Script ------------------------------------------------------------------------------------------ local n=0 json = require("json") --reads all the data from the uniPi API System for parsing (this could be moved to a time script) local unipi_json_data = io.popen("wget -qO- 'http://".. evokurl .. "/rest/all'") raw_unipi_data = unipi_json_data:read("*a") unipiRead = json:decode(raw_unipi_data) unipi_json_data:close() for key,value in pairs(unipiRead) do n=n+1 -----------input device types---------------------- if unipiRead[n].dev == "input" then if unipiRead[n].circuit == "1" then digital1 =unipiRead[n].value end if unipiRead[n].circuit == "2" then digital2 =unipiRead[n].value end if unipiRead[n].circuit == "3" then digital3 =unipiRead[n].value end if unipiRead[n].circuit == "4" then digital4 =unipiRead[n].value end if unipiRead[n].circuit == "5" then digital5 =unipiRead[n].value end if unipiRead[n].circuit == "6" then digital6 =unipiRead[n].value end if unipiRead[n].circuit == "7" then digital7 =unipiRead[n].value end if unipiRead[n].circuit == "8" then digital8 =unipiRead[n].value end if unipiRead[n].circuit == "9" then digital9 =unipiRead[n].value end if unipiRead[n].circuit == "10" then digital10 =unipiRead[n].value end if unipiRead[n].circuit == "11" then digital11 =unipiRead[n].value end if unipiRead[n].circuit == "12" then digital12 =unipiRead[n].value end end -----------temp device types----------------------- if unipiRead[n].dev == "temp" then if unipiRead[n].circuit == OWSN1 then temp1 = unipiRead[n].value end if unipiRead[n].circuit == OWSN2 then temp2 = unipiRead[n].value end if unipiRead[n].circuit == OWSN3 then temp3 = unipiRead[n].value end if unipiRead[n].circuit == OWSN4 then temp4 = unipiRead[n].value end end -----------analog output device types--------------- if unipiRead[n].dev == "ao" then if unipiRead[n].circuit == "1" then ao1 = unipiRead[n].value end end -----------analog input device types--------------- if unipiRead[n].dev == "ai" then if unipiRead[n].circuit == "1" then ai1 = unipiRead[n].value end if unipiRead[n].circuit == "2" then ai2 = unipiRead[n].value end end -----------relay device types----------------------- if unipiRead[n].dev == "relay" then if unipiRead[n].circuit == "1" then relay1 = unipiRead[n].value end if unipiRead[n].circuit == "2" then relay2 = unipiRead[n].value end if unipiRead[n].circuit == "3" then relay3 = unipiRead[n].value end if unipiRead[n].circuit == "4" then relay4 = unipiRead[n].value end if unipiRead[n].circuit == "5" then relay5 = unipiRead[n].value end if unipiRead[n].circuit == "6" then relay6 = unipiRead[n].value end if unipiRead[n].circuit == "7" then relay7 = unipiRead[n].value end if unipiRead[n].circuit == "8" then relay8 = unipiRead[n].value end end ----------------------------- end --ends the read main loop ----------------------------- commandArray = {} -----------------------Relay One---------------------------- if devicechanged[relay1name] then if ((otherdevices[relay1name] == 'Off') ) then print("Sending Power OFF to uniPi relay one") io.popen("wget -qO- http://".. evokurl .. "/rest/relay/1 --post-data='value=0'") end if ((otherdevices[relay1name] == 'On') ) then io.popen("wget -qO- http://".. evokurl .. "/rest/relay/1 --post-data='value=1'") print("uniPi: Sending Power ON to uniPi relay one") end else if relay1 == 1 then if ((otherdevices[relay1name] == 'Off') ) then commandArray[1] = {['UpdateDevice'] = idxr1 .. '|1|On'} end elseif relay1 == 0 then if ((otherdevices[relay1name] == 'On') ) then commandArray[1] = {['UpdateDevice'] = idxr1 .. '|0|Off'} end else print("uniPi relay one error") end end -----------------------Relay Two---------------------------- if devicechanged[relay2name] then if ((otherdevices[relay2name] == 'Off') ) then print("Sending Power OFF to uniPi relay two") io.popen("wget -qO- http://".. evokurl .. "/rest/relay/2 --post-data='value=0'") end if ((otherdevices[relay2name] == 'On') ) then io.popen("wget -qO- http://".. evokurl .. "/rest/relay/2 --post-data='value=1'") print("uniPi: Sending Power ON to uniPi relay two") end else if relay2 == 1 then if ((otherdevices[relay2name] == 'Off') ) then commandArray[2] = {['UpdateDevice'] = idxr2 .. '|1|On'} end elseif relay2 == 0 then if ((otherdevices[relay2name] == 'On') ) then commandArray[2] = {['UpdateDevice'] = idxr2 .. '|0|Off'} end else print("uniPi relay two error") end end -----------------------Relay Three---------------------------- if devicechanged[relay3name] then if ((otherdevices[relay3name] == 'Off') ) then print("Sending Power OFF to uniPi relay three") io.popen("wget -qO- http://".. evokurl .. "/rest/relay/3 --post-data='value=0'") end if ((otherdevices[relay3name] == 'On') ) then io.popen("wget -qO- http://".. evokurl .. "/rest/relay/3 --post-data='value=1'") print("uniPi: Sending Power ON to uniPi relay three") end else if relay3 == 1 then if ((otherdevices[relay3name] == 'Off') ) then commandArray[3] = {['UpdateDevice'] = idxr3 .. '|1|On'} end elseif relay3 == 0 then if ((otherdevices[relay3name] == 'On') ) then commandArray[3] = {['UpdateDevice'] = idxr3 .. '|0|Off'} end else print("uniPi relay three error") end end -----------------------Relay Four---------------------------- if devicechanged[relay4name] then if ((otherdevices[relay4name] == 'Off') ) then print("Sending Power OFF to uniPi relay four") io.popen("wget -qO- http://".. evokurl .. "/rest/relay/4 --post-data='value=0'") end if ((otherdevices[relay4name] == 'On') ) then io.popen("wget -qO- http://".. evokurl .. "/rest/relay/4 --post-data='value=1'") print("uniPi: Sending Power ON to uniPi relay four") end else if relay4 == 1 then if ((otherdevices[relay4name] == 'Off') ) then commandArray[4] = {['UpdateDevice'] = idxr4 .. '|1|On'} end elseif relay4 == 0 then if ((otherdevices[relay4name] == 'On') ) then commandArray[4] = {['UpdateDevice'] = idxr4 .. '|0|Off'} end else print("uniPi relay four error") end end -----------------------Relay Five---------------------------- if devicechanged[relay5name] then if ((otherdevices[relay5name] == 'Off') ) then print("Sending Power OFF to uniPi relay five") io.popen("wget -qO- http://".. evokurl .. "/rest/relay/5 --post-data='value=0'") end if ((otherdevices[relay5name] == 'On') ) then io.popen("wget -qO- http://".. evokurl .. "/rest/relay/5 --post-data='value=1'") print("uniPi: Sending Power ON to uniPi relay five") end else if relay5 == 1 then if ((otherdevices[relay5name] == 'Off') ) then commandArray[5] = {['UpdateDevice'] = idxr5 .. '|1|On'} end elseif relay5 == 0 then if ((otherdevices[relay5name] == 'On') ) then commandArray[5] = {['UpdateDevice'] = idxr5 .. '|0|Off'} end else print("uniPi relay five error") end end -----------------------Relay Six---------------------------- if devicechanged[relay6name] then if ((otherdevices[relay6name] == 'Off') ) then print("Sending Power OFF to uniPi relay six") io.popen("wget -qO- http://".. evokurl .. "/rest/relay/6 --post-data='value=0'") end if ((otherdevices[relay6name] == 'On') ) then io.popen("wget -qO- http://".. evokurl .. "/rest/relay/6 --post-data='value=1'") print("uniPi: Sending Power ON to uniPi relay six") end else if relay6 == 1 then if ((otherdevices[relay6name] == 'Off') ) then commandArray[6] = {['UpdateDevice'] = idxr6 .. '|1|On'} end elseif relay6 == 0 then if ((otherdevices[relay6name] == 'On') ) then commandArray[6] = {['UpdateDevice'] = idxr6 .. '|0|Off'} end else print("uniPi relay six error") end end -----------------------Relay Seven---------------------------- if devicechanged[relay7name] then if ((otherdevices[relay7name] == 'Off') ) then print("Sending Power OFF to uniPi relay seven") io.popen("wget -qO- http://".. evokurl .. "/rest/relay/7 --post-data='value=0'") end if ((otherdevices[relay7name] == 'On') ) then io.popen("wget -qO- http://".. evokurl .. "/rest/relay/7 --post-data='value=1'") print("uniPi: Sending Power ON to uniPi relay seven") end else if relay7 == 1 then if ((otherdevices[relay7name] == 'Off') ) then commandArray[7] = {['UpdateDevice'] = idxr7 .. '|1|On'} end elseif relay7 == 0 then if ((otherdevices[relay7name] == 'On') ) then commandArray[7] = {['UpdateDevice'] = idxr7 .. '|0|Off'} end else print("uniPi relay seven error") end end -----------------------Relay Eight---------------------------- if devicechanged[relay8name] then if ((otherdevices[relay8name] == 'Off') ) then print("uniPi: Sending Power OFF to uniPi relay eight") io.popen("wget -qO- http://".. evokurl .. "/rest/relay/8 --post-data='value=0'") end if ((otherdevices[relay8name] == 'On') ) then io.popen("wget -qO- http://".. evokurl .. "/rest/relay/8 --post-data='value=1'") print("uniPi: Sending Power ON to uniPi relay eight") end else if relay8 == 1 then if ((otherdevices[relay8name] == 'Off') ) then commandArray[8] = {['UpdateDevice'] = idxr8 .. '|1|On'} end elseif relay8 == 0 then if ((otherdevices[relay8name] == 'On') ) then commandArray[8] = {['UpdateDevice'] = idxr8 .. '|0|Off'} end else print("uniPi relay eight error") end end -----------------------Analog Output One---------------------------- --this is currently only one way value settings, also no adjustment for the 11-15 range on the domoticz slider its all considered 100% if devicechanged[ao1name] then ao1_current_slider = tonumber(otherdevices_svalues[ao1name]) print("uniPi: Sending value " .. ao1_current_slider .. " to Analog Out") io.popen("wget -qO- http://".. evokurl .. "/rest/ao/1 --post-data='value=" .. ao1_current_slider .. "'") end -----------------------Analog Input One----------------------------- ai1data = "http://" .. domoticzjson .. "/json.htm?type=command¶m=udevice&idx=" .. idxai1 .. "&nvalue=0&svalue=" .. string.format("%.3f", ai1) ai1_last_value = uservariables["ai1_value"] ai1_current_value = string.format("%.1f", ai1) if ai1_current_value == "0.0" then ai1_current_value = tonumber(ai1_current_value) end if ai1_last_value == nil then print("uniPi Error: User variable not set for : Analog Input One 'ai1_value'") end if tostring(ai1_current_value) == tostring(ai1_last_value) then --nothing changed else commandArray['OpenURL'] = ai1data commandArray['Variable:ai1_value'] = ai1_current_value end -----------------------Analog Input Two---------------------------- local ai2data = "http://" .. domoticzjson .. "/json.htm?type=command&param=udevice&idx=" .. idxai2 .. "&nvalue=0&svalue=" .. string.format("%.3f", ai2) ai2_last_value = uservariables["ai2_value"] ai2_current_value = string.format("%.1f", ai2) if ai2_current_value == "0.0" then ai2_current_value = tonumber(ai2_current_value) end if ai2_last_value == nil then print("uniPi Error: User variable not set for : Analog Input Two 'ai2_value'") end if tostring(ai2_current_value) == tostring(ai2_last_value) then --nothing changed else commandArray['OpenURL'] = ai2data commandArray['Variable:ai2_value'] = ai2_current_value end -----------------------Digital Input One---------------------------- if digital1 == 1 then if ((otherdevices[di1name] == 'Off') ) then commandArray[21] = {['UpdateDevice'] = idxdi1 .. '|1|On'} end elseif digital1 == 0 then if ((otherdevices[di1name] == 'On') ) then commandArray[21] = {['UpdateDevice'] = idxdi1 .. '|0|Off'} end else print("uniPi digital input one error") end -----------------------Digital Input Two---------------------------- if digital2 == 1 then if ((otherdevices[di2name] == 'Off') ) then commandArray[22] = {['UpdateDevice'] = idxdi2 .. '|1|On'} end elseif digital2 == 0 then if ((otherdevices[di2name] == 'On') ) then commandArray[22] = {['UpdateDevice'] = idxdi2 .. '|0|Off'} end else print("uniPi digital input two error") end -----------------------Digital Input Three---------------------------- if digital3 == 1 then if ((otherdevices[di3name] == 'Off') ) then commandArray[23] = {['UpdateDevice'] = idxdi3 .. '|1|On'} end elseif digital3 == 0 then if ((otherdevices[di3name] == 'On') ) then commandArray[23] = {['UpdateDevice'] = idxdi3 .. '|0|Off'} end else print("uniPi digital input three error") end -----------------------Digital Input Four---------------------------- if digital4 == 1 then if ((otherdevices[di4name] == 'Off') ) then commandArray[24] = {['UpdateDevice'] = idxdi4 .. '|1|On'} end elseif digital4 == 0 then if ((otherdevices[di4name] == 'On') ) then commandArray[24] = {['UpdateDevice'] = idxdi4 .. '|0|Off'} end else print("uniPi digital input four error") end -----------------------Digital Input Five---------------------------- if digital5 == 1 then if ((otherdevices[di5name] == 'Off') ) then commandArray[24] = {['UpdateDevice'] = idxdi5 .. '|1|On'} end elseif digital5 == 0 then if ((otherdevices[di5name] == 'On') ) then commandArray[24] = {['UpdateDevice'] = idxdi5 .. '|0|Off'} end else print("uniPi digital input Five error") end -----------------------Digital Input Six---------------------------- if digital6 == 1 then if ((otherdevices[di6name] == 'Off') ) then commandArray[24] = {['UpdateDevice'] = idxdi6 .. '|1|On'} end elseif digital6 == 0 then if ((otherdevices[di6name] == 'On') ) then commandArray[24] = {['UpdateDevice'] = idxdi6 .. '|0|Off'} end else print("uniPi digital input Six error") end -----------------------Digital Input Seven---------------------------- if digital7 == 1 then if ((otherdevices[di7name] == 'Off') ) then commandArray[24] = {['UpdateDevice'] = idxdi7 .. '|1|On'} end elseif digital7 == 0 then if ((otherdevices[di7name] == 'On') ) then commandArray[24] = {['UpdateDevice'] = idxdi7 .. '|0|Off'} end else print("uniPi digital input Seven error") end -----------------------Digital Input Eight---------------------------- if digital8 == 1 then if ((otherdevices[di8name] == 'Off') ) then commandArray[24] = {['UpdateDevice'] = idxdi8 .. '|1|On'} end elseif digital8 == 0 then if ((otherdevices[di8name] == 'On') ) then commandArray[24] = {['UpdateDevice'] = idxdi8 .. '|0|Off'} end else print("uniPi digital input Eight error") end -----------------------Digital Input Nine---------------------------- if digital9 == 1 then if ((otherdevices[di9name] == 'Off') ) then commandArray[24] = {['UpdateDevice'] = idxdi9 .. '|1|On'} end elseif digital9 == 0 then if ((otherdevices[di9name] == 'On') ) then commandArray[24] = {['UpdateDevice'] = idxdi9 .. '|0|Off'} end else print("uniPi digital input Nine error") end -----------------------Digital Input Ten---------------------------- if digital10 == 1 then if ((otherdevices[di10name] == 'Off') ) then commandArray[24] = {['UpdateDevice'] = idxdi10 .. '|1|On'} end elseif digital10 == 0 then if ((otherdevices[di10name] == 'On') ) then commandArray[24] = {['UpdateDevice'] = idxdi10 .. '|0|Off'} end else print("uniPi digital input Ten error") end -----------------------Digital Input Eleven---------------------------- if digital11 == 1 then if ((otherdevices[di11name] == 'Off') ) then commandArray[24] = {['UpdateDevice'] = idxdi11 .. '|1|On'} end elseif digital11 == 0 then if ((otherdevices[di11name] == 'On') ) then commandArray[24] = {['UpdateDevice'] = idxdi11 .. '|0|Off'} end else print("uniPi digital input Eleven error") end -----------------------Digital Input Twelve---------------------------- if digital12 == 1 then if ((otherdevices[di12name] == 'Off') ) then commandArray[24] = {['UpdateDevice'] = idxdi12 .. '|1|On'} end elseif digital12 == 0 then if ((otherdevices[di12name] == 'On') ) then commandArray[24] = {['UpdateDevice'] = idxdi12 .. '|0|Off'} end else print("uniPi digital input Twelve error") end return commandArray