17.10.2025, 12:54
(This post was last modified: 17.10.2025, 12:55 by gjniewenhuijse.)
(16.10.2025, 10:11)admin Wrote: If you need multiple websockets then create a separate script for every connection. A user library can be helpful to avoid copy/pasting the same code.
timeout error can be ignored, it simply means that there's no data to be read at this moment. You can increase the timeout value if the data is guaranteed to be sent at least once every X seconds.
Something like this?
Resident script
Code:
require('user.nit_homewizard')
clientHandler(1)
User library nit_homewizard
Code:
--https://api-documentation.homewizard.com/docs/v2/websocket
debug = false
-- all homewizard devices (client, ip, token)
local cDevices = { {nil, 'YOURIP', 'YOURTOKEN'} }
function clientHandler(iDevice)
if not cDevices[iDevice][1] then
ws = require('user.websocket')
json = require('json')
local ip = cDevices[iDevice][2]
local token = cDevices[iDevice][3]
local url = 'wss://' .. ip .. '/api/ws'
cDevices[iDevice][1], err = ws.client('sync', 10)
res, err = cDevices[iDevice][1]:connect(url)
if res then
-- send authorization
cDevices[iDevice][1]:send(json.encode({type = 'authorization',data = token}))
-- identify
-- cDevices[iDevice][1]:send(json.encode({type = 'identify'}))
-- polling data once
cDevices[iDevice][1]:send(json.encode({type = 'system'}))
-- (un)subscribe
cDevices[iDevice][1]:send(json.encode({type = 'subscribe',data = '*'}))
-- subscribe op device info
--cDevices[iDevice][1]:send(json.encode({type = 'subscribe',data = 'device'}))
-- subscribe op system info
--cDevices[iDevice][1]:send(json.encode({type = 'subscribe',data = 'system'}))
-- (un)subscribe op measurement
--cDevices[iDevice][1]:send(json.encode({type = 'subscribe',data = 'measurement'}))
cDevices[iDevice][1]:send(json.encode({type = 'unsubscribe',data = 'measurement'}))
-- subscribe op batteries (werkt niet, onbekend)
--cDevices[iDevice][1]:send(json.encode({type = 'subscribe',data = 'batteries'}))
-- setting data
--{ "type": "system", "data": { "cloud_enabled": false } }
else
if debug then
log('connection failed: ' .. tostring(err))
end
cDevices[iDevice][1]:close()
cDevices[iDevice][1] = nil
end
else
data, _, _, opcode, err = cDevices[iDevice][1]:receive()
if data then
data = json.pdecode(data)
if data then
log(data)
end
else
if debug then
log('receive failed: ' .. tostring(err))
end
cDevices[iDevice][1]:close()
cDevices[iDevice][1] = nil
end
end
end