This forum uses cookies
This forum makes use of cookies to store your login information if you are registered, and your last visit if you are not. Cookies are small text documents stored on your computer; the cookies set by this forum can only be used on this website and pose no security risk. Cookies on this forum also track the specific topics you have read and when you last read them. Please confirm that you accept these cookies being set.

Homewizard websocket
#5
(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
Reply


Messages In This Thread
Homewizard websocket - by gjniewenhuijse - 16.10.2025, 09:20
RE: Homewizard websocket - by admin - 16.10.2025, 09:39
RE: Homewizard websocket - by gjniewenhuijse - 16.10.2025, 09:39
RE: Homewizard websocket - by admin - 16.10.2025, 10:11
RE: Homewizard websocket - by gjniewenhuijse - 17.10.2025, 12:54
RE: Homewizard websocket - by admin - 17.10.2025, 12:58

Forum Jump: