Posts: 477
Threads: 100
Joined: Jun 2015
Reputation:
6
In the new Homewizard v2 api there's a websocket to receive device updates, see:
WebSocket | HomeWizard Energy API Documentation
From the forum i used an example, but i received an error:
Code: * table:
["type"]
* string: authorization_requested
["data"]
* table:
["api_version"]
* string: 2.0.1
* string: receive failed: error:0A000126:SSL routines::unexpected eof while reading
My code
Code: if not client then
ws = require('user.websocket')
json = require('json')
local ip = 'MYIP'
local token = 'MYTOKEN'
local url = 'wss://' .. ip .. '/api/ws'
client, err = ws.client('sync', 10)
res, err = client:connect(url)
if res then
-- send authorization
client:send(json.encode({
type = 'authorization',
payload = 'data=' .. token,
}))
-- subscribe op batteries
--client:send(json.encode({
--type = 'subscribe',
--data = 'batteries'
--}))
else
log('connection failed: ' .. tostring(err))
client:close()
client = nil
end
else
data, _, _, opcode, err = client:receive()
if data then
data = json.pdecode(data)
if data then
log(data)
end
else
log('receive failed: ' .. tostring(err))
client:close()
client = nil
end
end
Posts: 8397
Threads: 45
Joined: Jun 2015
Reputation:
481
Try sending auth this way:
Code: client:send(json.encode({
type = 'authorization',
data = token,
}))
You might need to wait for authorization_requested to be sent by the server fist before sending the authorization command.
Posts: 477
Threads: 100
Joined: Jun 2015
Reputation:
6
16.10.2025, 09:39
(This post was last modified: 16.10.2025, 09:50 by gjniewenhuijse.)
and how to setup the code to connect to multiple devices (+- 10 devices)?
(16.10.2025, 09:39)admin Wrote: Try sending auth this way:
Code: client:send(json.encode({
type = 'authorization',
data = token,
}))
You might need to wait for authorization_requested to be sent by the server fist before sending the authorization command.
It works, i see data but also:
Code: * string: receive failed: protocol error
and
* string: connection failed: timeout
Posts: 8397
Threads: 45
Joined: Jun 2015
Reputation:
481
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.
Posts: 477
Threads: 100
Joined: Jun 2015
Reputation:
6
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
Posts: 8397
Threads: 45
Joined: Jun 2015
Reputation:
481
A simpler way:
Code: function clientHandler(ip, token)
if not client then
ws = require('user.websocket')
json = require('json')
local url = 'wss://' .. ip .. '/api/ws'
client, err = ws.client('sync', 10)
res, err = client:connect(url)
...
else
data, _, _, opcode, err = client:receive()
...
end
end
Code: require('user.nit_homewizard')
clientHandler('192.168.192.168', '123456')
|