openHAB2 : REST API : websockets: notification on item state change

Are there any examples of connecting to openHAB2 REST API using python + websockets? I’m interested in getting item state change notifications without polling item states.

I tried the python notification example (see with no luck (it’s the exact same example, I just enabled tracelog, changed IP and item name):

import websocket    #need to run pip install websocket-client to get this module

class WSClient():
    def __init__(self):
        websocket.enableTrace(True) #True for debugging
    def connect(self, url):
        print("connecting to %s" % url)
        = websocket.WebSocketApp(url,
        on_message = self.on_message,
        on_error = self.on_error,
        on_close = self.on_close) = self.on_open
    def on_message(self, ws, message):
    def on_error(self, ws, error):
    def on_close(self, ws):
        print("connection closed")
    def on_open(self, ws):
    def close(self, ws):
if __name__ == "__main__":
        client = WSClient()
    except (KeyboardInterrupt, SystemExit):
        print("System exit Received - Exiting program")
        print(" **** Program Ended ****")

It gives me the following output:

connecting to ws://
--- request header ---
GET /rest/items/switch_tvOn/state?Accept=application/json HTTP/1.1
Upgrade: websocket
Connection: Upgrade
Sec-WebSocket-Key: hEBfauXHBMLur7tkyxZg8g==
Sec-WebSocket-Version: 13

--- response header ---
HTTP/1.1 200 OK
Content-Type: text/plain
Content-Length: 4
Server: Jetty(9.3.22.v20171030)
Handshake status 200
connection closed

The function on_message is never called.

Looking at the wireshark log I see the following:


State information for item switch_hm_motionDetection_lowBat (OFF) is transferred, but is not received from my websocket client.

I tried different examples using socketIO-client and now I have seen every possible error (at least it feal’s like it)…

Another attempt maybe worth mentioning:

import websocket as ws
if __name__ == "__main__":
    ohsocket = ws.WebSocket()
    ohsocket.connect("ws://", header={"X-Atmosphere-Transport":"websocket", "Accept":"application/json"})

gives me the following ouput:

--- request header ---
GET /rest/items/ HTTP/1.1
Upgrade: websocket
Connection: Upgrade
Sec-WebSocket-Key: bIMaDJx431bYUKLNKK1h3A==
Sec-WebSocket-Version: 13
X-Atmosphere-Transport: websocket
Accept: application/json

--- response header ---
HTTP/1.1 200 OK
Content-Type: application/json
Transfer-Encoding: chunked
Server: Jetty(9.3.22.v20171030)
Traceback (most recent call last):
  File "/home/seven/dev/workspace_eclipse/openhabExt/src/", line 100, in <module>
    ohsocket.connect("ws://", header={"X-Atmosphere-Transport":"websocket", "Accept":"application/json"})
  File "/usr/local/lib/python3.4/dist-packages/websocket/", line 216, in connect
    self.handshake_response = handshake(self.sock, *addrs, **options)
  File "/usr/local/lib/python3.4/dist-packages/websocket/", line 69, in handshake
    status, resp = _get_resp_headers(sock)
  File "/usr/local/lib/python3.4/dist-packages/websocket/", line 135, in _get_resp_headers
    raise WebSocketBadStatusException("Handshake status %d", status)
websocket._exceptions.WebSocketBadStatusException: Handshake status 200

Do you have an idea how to use python websockets with openHAB2?


PS: The following website did not bring light into darkness either…