[RestAPI] How to use events

Hi all,

I’m starting with using the RestAPI. Getting and changing item state works fine already. Unfortunately I’m struggling with getting informed when an item state changes, hence events.

What I want to do:
I have a working switch (itemname: KG_Buero_Licht_Schalten) and I want to get informed via RestAPI when switch status changes. e.g. When switch is turned on by MainUI the RestAPI client should get the “ON” information.

What I did so far:
In RestAPI documentation I learned that this kind of functinality can be done by subscribing to events. From Event Bus dokumentation I got the information that the topic for my case is “openhab/items/{itemName}/statechanged” so I created the topic for my switch:
openhab/items/KG_Buero_Licht_Schalten/statechanged

Where is my problem
I want to try this RestAPI point, so I switch to the API Explorer. In Category events-> GET /events I added the topic from above in the corresponding input field and press Execute button. Now I see the Loading circle and nothing else happens (also when I use the switch from another browser). The loading never disappears/stops.

Now I’m a bit confused about what I am doing wrong. Or if I’m thinking completely in wrong direction how this mechanism works.

Maybe someone can bring me back on track.

Thanks a lot
Philipp

  • openHAB version: 4.1.1 release

OH supports a Server-Sent-Events (SSE) stream or I believe it also supports a [WebSocket}(WebSocket - Web APIs | MDN). I don’t think you can exercise either of these using just the browser and the API explorer page. You’ll need to add an SSE or WebSocket library to your client and configure it to subscribe for and filter on the topic you care about. It’s not going to be a simple http GET.

https://github.com/rkoshak/sensorReporter/blob/main/openhab_rest/rest_conn.py has some Python code you could use as an example.

Hello,

thanks for the hint.
I was expecting that it works with the API explorer since it is listed there and I wanted to make sure with it that my topic is correct (to exclude errors on my side when implementing it in my program).

Trying it with curl works like a charm.

curl "http:// {openHAB_IP}:8080/rest/events?topics=openhab/items/KG_Buero_Licht_Schalten/statechanged"

Also with SSE from my program everything is fine.

Thanks again

Philipp