REST API Eventstream filtering

  • Platform information:
    • Hardware: Intel NUC i3 16Gb RAM 500Gb SSD
    • OS: Ubuntu 20.04LTS
    • Java Runtime Environment: Zulu JRE11
    • openHAB version:3.1.0

Issue of the topic:
I’m building a React UI which uses the REST API event streams in a CQRS manner in that the UI only ever reflects the state of the system. Commands sent from the UI only result in changes to the UI after openHAB emits an event relating to the completion of the requested command.

I receive most of the needed information from

const eventUrl="https://xxx.xxx.xxx/rest/events/states";

But in order to ensure that elements of my home system which go offline are properly displayed in the UI I am also subscribing to the status stream at

const statusUrl = 'https://xxx.xxx.xxx/rest/events?topic=smarthome/things/status'

This stream is fine for startup but once the current status of the home items has been established I would like to use a “statuschanged” stream so that I am only having to interpret a small number of events e.g. when lights get switched off at the wall or sensors are unplugged.

I have tried the following url variations to achieve what I want but with no success.

const changedUrl = 'https://xxx.xxx.xxx/rest/events?topic=smarthome/things/*/statuschanged'
const changedUrl = 'https://xxx.xxx.xxx/rest/events?topic=smarthome/things/statuschanged'
const changedUrl = 'https://xxx.xxx.xxx/rest/events?topic=openhab/things/*/statuschanged'
const changedUrl = 'https://xxx.xxx.xxx/rest/events?topic=open/things/statuschanged'
const changedUrl = 'https://xxx.xxx.xxx/rest/events?topics=smarthome/things/*/statuschanged'
const changedUrl = 'https://xxx.xxx.xxx/rest/events?topics=smarthome/things/statuschanged'
const changedUrl = 'https://xxx.xxx.xxx/rest/events?topics=openhab/things/*/statuschanged'
const changedUrl = 'https://xxx.xxx.xxx/rest/events?topics=open/things/statuschanged'

I all cases the stream I receive seems to contain all events for state and status rather than a filtered stream.

I am clearly missing something in the url syntax.

Can anyone point me in the right direction please?

Many thanks in advance
regards
Ian Carson

Answering my own question:

The correct URL form for filtering on ThingStatusInfoChangedEvent is

https://xxx.xxx.xxx/rest/events?topics=openhab/things/*/statuschanged

Unlike my many other unsuccessful attempts the proper call needs to

  • use “topics” not “topic”
  • start the topic with “openhab” not “smarthome”
  • use a wildcard of “*” to substitute for any “{thingUID}” in that place in the topic

All working nicely now.

Hopefully this little nugget of information will be useful to someone else as well

Regards
Ian

1 Like