Advertisement
Guest User

Untitled

a guest
Mar 17th, 2018
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.02 KB | None | 0 0
  1. return {
  2.     on = {
  3.         timer = {'every minute'},
  4.         devices = {'Xiaomi Power Strip'}
  5.     },
  6.     logging = {
  7.         level = domoticz.LOG_ERROR
  8.     },
  9.     execute = function(dz, item)
  10.  
  11.          local Device        = dz.devices('Xiaomi Power Strip')               -- dummy switch - type:Switch (On/Off)
  12.          local UsageDevice   = dz.devices('Xiaomi Power Strip - Usage')       -- dummy switch - type:Electric (Instant+Counter)
  13.          local CurrentDevice = dz.devices('Xiaomi Power Strip - Current')     -- dummy switch - type:Ampere (1-phase)
  14.          local VoltageDevice = dz.devices('Xiaomi Power Strip - Voltage')     -- dummy switch - type:Voltage
  15.          local TempDevice    = dz.devices('Xiaomi Power Strip - Temp')        -- dummy switch - type:Temperature
  16.          local Path   = "/home/pi/php-miio/"                                  -- php-miio path
  17.          local IP     = '192.168.1.50'                                        -- Device IP
  18.          local Token  = 'a1b2a1b2a1b2a1b2a1b2a1b2a1b2a1b2'                    -- token
  19.          local DZurl  = "http://username:password@domoticz_ip:domoticz_port"
  20.          local t      = dz.time
  21.  
  22.  function SendCommand()
  23.     cmd = "cd "..Path.." && php miio-cli.php --ip "..IP.." --token "..Token.." --sendcmd '{\"id\":1,\"method\":\""..method.."\",\"params\":"..param.."}'"
  24.  end
  25.  
  26.  function UpdateDevice()
  27.     upd = 'curl -s "'..DZurl..'/json.htm?type=command&dparam=udevice&idx='..UsageDevice.idx..'&nvalue=0&svalue='..power..';'..current..'"'
  28.     dz.utils.osExecute('('..upd..' > /dev/null)&')
  29.     CurrentDevice.updateCurrent(current)
  30.     VoltageDevice.updateVoltage(dz.utils.round(voltage, 1))
  31.     TempDevice.updateTemperature(dz.utils.round(temp, 1))
  32.  end
  33.  
  34.         if (item.isDevice) then
  35.             method = "set_power"
  36.                if (item.state == 'On') then
  37.                    param = "[\"on\"]"
  38.                else
  39.                    param = "[\"off\"]"
  40.                end
  41.             SendCommand()
  42.             dz.utils.osExecute('('..cmd..' > /dev/null)&')
  43.         end
  44.         if (item.isTimer) then
  45.             method = "get_prop"
  46.             param  = "[\"power\", \"temperature\", \"current\", \"power_consume_rate\"]"
  47.             SendCommand()
  48.             local h        = io.popen(cmd)
  49.             local response = h:read("*a")
  50.             h:close()
  51.             local devOffline = "Устройство "..IP.." не доступно или не отвечает"
  52.             local devOnline  = "Устройство "..IP.." доступно и ответило"
  53.                if (string.find(response, devOffline)) then
  54.                    print('<font color="red">'..devOffline..'</font>')
  55.                    Device.switchOff().checkFirst().silent()
  56.                    temp, current, power, voltage = 0, 0, 0, 0
  57.                    UpdateDevice()
  58.                end
  59.                if (string.find(response, devOnline)) then
  60.                       if (string.find(response, "on")) then
  61.                           Device.switchOn().checkFirst().silent()
  62.                       end
  63.                       if (string.find(response, "off")) then
  64.                           Device.switchOff().checkFirst().silent()
  65.                       end
  66.                       if (string.find(response, "null")) then
  67.                           _, _, num = string.find(response, "%[(.-)%]")
  68.                           _, _, num1, num2 = string.find(num, "(%d+)[^%d]+(%d+)")
  69.                           temp = tonumber(num1.."."..num2)
  70.                           current, power, voltage = 0, 0, 0
  71.                           UpdateDevice()
  72.                           return
  73.                       end
  74.                    _, _, num = string.find(response, "%[(.-)%]")
  75.                    _, _, num1, num2, num3, num4, num5, num6 = string.find(num, "(%d+)[^%d]+(%d+)[^%d]+(%d+)[^%d]+(%d+)[^%d]+(%d+)[^%d]+(%d+)")
  76.                    temp    = tonumber(num1.."."..num2)
  77.                    current = tonumber(num3.."."..num4)
  78.                    power   = tonumber(num5.."."..num6)
  79.                       if (power ~= 0 and current ~= 0) then
  80.                           voltage = power/current
  81.                       else
  82.                           voltage = 0
  83.                       end
  84.                     --  
  85.                       if (voltage > 0 and voltage < 50) then
  86.                           power   = power*10
  87.                           voltage = power/current  
  88.                       end
  89.                     --  
  90.                    UpdateDevice()
  91.               end
  92.               --[[if (t.matchesRule('at 18:00-02:00 on mon, tue, wed, thu, fri'))
  93.                   or (t.matchesRule('at 09:00-02:00 o on sat, sun')) then
  94.                       Device.switchOn().checkFirst()
  95.               end
  96.               if (t.matchesRule('at 02:00-18:00 on mon, tue, wed, thu, fri'))
  97.                   or (t.matchesRule('at 02:00-09:00 o on sat, sun')) then
  98.                       Device.switchOff().checkFirst()
  99.               end]]--
  100.               if (t.matchesRule('at 18:00-02:00 on mon, tue, wed, thu, fri'))
  101.                   or (t.matchesRule('at 09:00-02:00 o on sat, sun')) then
  102.                       Device.switchOn().checkFirst()
  103.               else
  104.                   Device.switchOff().checkFirst()
  105.               end
  106.         end
  107.     end
  108. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement