Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- return {
- on = {
- timer = {'every minute'},
- devices = {'Xiaomi Power Strip'}
- },
- logging = {
- level = domoticz.LOG_ERROR
- },
- execute = function(dz, item)
- local Device = dz.devices('Xiaomi Power Strip') -- dummy switch - type:Switch (On/Off)
- local UsageDevice = dz.devices('Xiaomi Power Strip - Usage') -- dummy switch - type:Electric (Instant+Counter)
- local CurrentDevice = dz.devices('Xiaomi Power Strip - Current') -- dummy switch - type:Ampere (1-phase)
- local VoltageDevice = dz.devices('Xiaomi Power Strip - Voltage') -- dummy switch - type:Voltage
- local TempDevice = dz.devices('Xiaomi Power Strip - Temp') -- dummy switch - type:Temperature
- local Path = "/home/pi/php-miio/" -- php-miio path
- local IP = '192.168.1.50' -- Device IP
- local Token = 'a1b2a1b2a1b2a1b2a1b2a1b2a1b2a1b2' -- token
- local DZurl = "http://username:password@domoticz_ip:domoticz_port"
- local t = dz.time
- function SendCommand()
- cmd = "cd "..Path.." && php miio-cli.php --ip "..IP.." --token "..Token.." --sendcmd '{\"id\":1,\"method\":\""..method.."\",\"params\":"..param.."}'"
- end
- function UpdateDevice()
- upd = 'curl -s "'..DZurl..'/json.htm?type=command&dparam=udevice&idx='..UsageDevice.idx..'&nvalue=0&svalue='..power..';'..current..'"'
- dz.utils.osExecute('('..upd..' > /dev/null)&')
- CurrentDevice.updateCurrent(current)
- VoltageDevice.updateVoltage(dz.utils.round(voltage, 1))
- TempDevice.updateTemperature(dz.utils.round(temp, 1))
- end
- if (item.isDevice) then
- method = "set_power"
- if (item.state == 'On') then
- param = "[\"on\"]"
- else
- param = "[\"off\"]"
- end
- SendCommand()
- dz.utils.osExecute('('..cmd..' > /dev/null)&')
- end
- if (item.isTimer) then
- method = "get_prop"
- param = "[\"power\", \"temperature\", \"current\", \"power_consume_rate\"]"
- SendCommand()
- local h = io.popen(cmd)
- local response = h:read("*a")
- h:close()
- local devOffline = "Устройство "..IP.." не доступно или не отвечает"
- local devOnline = "Устройство "..IP.." доступно и ответило"
- if (string.find(response, devOffline)) then
- print('<font color="red">'..devOffline..'</font>')
- Device.switchOff().checkFirst().silent()
- temp, current, power, voltage = 0, 0, 0, 0
- UpdateDevice()
- end
- if (string.find(response, devOnline)) then
- if (string.find(response, "on")) then
- Device.switchOn().checkFirst().silent()
- end
- if (string.find(response, "off")) then
- Device.switchOff().checkFirst().silent()
- end
- if (string.find(response, "null")) then
- _, _, num = string.find(response, "%[(.-)%]")
- _, _, num1, num2 = string.find(num, "(%d+)[^%d]+(%d+)")
- temp = tonumber(num1.."."..num2)
- current, power, voltage = 0, 0, 0
- UpdateDevice()
- return
- end
- _, _, num = string.find(response, "%[(.-)%]")
- _, _, num1, num2, num3, num4, num5, num6 = string.find(num, "(%d+)[^%d]+(%d+)[^%d]+(%d+)[^%d]+(%d+)[^%d]+(%d+)[^%d]+(%d+)")
- temp = tonumber(num1.."."..num2)
- current = tonumber(num3.."."..num4)
- power = tonumber(num5.."."..num6)
- if (power ~= 0 and current ~= 0) then
- voltage = power/current
- else
- voltage = 0
- end
- --
- if (voltage > 0 and voltage < 50) then
- power = power*10
- voltage = power/current
- end
- --
- UpdateDevice()
- end
- --[[if (t.matchesRule('at 18:00-02:00 on mon, tue, wed, thu, fri'))
- or (t.matchesRule('at 09:00-02:00 o on sat, sun')) then
- Device.switchOn().checkFirst()
- end
- if (t.matchesRule('at 02:00-18:00 on mon, tue, wed, thu, fri'))
- or (t.matchesRule('at 02:00-09:00 o on sat, sun')) then
- Device.switchOff().checkFirst()
- end]]--
- if (t.matchesRule('at 18:00-02:00 on mon, tue, wed, thu, fri'))
- or (t.matchesRule('at 09:00-02:00 o on sat, sun')) then
- Device.switchOn().checkFirst()
- else
- Device.switchOff().checkFirst()
- end
- end
- end
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement