[SOLVED] Best way to connect to another REST API (deCONZ on localhost)

In order to interface my Xiaomi Mi cubes, I have set up a deCONZ instance on the same host I am running openHAB. It has a REST API.

What would be the best way for openHAB to get the buttonevent whenever the lastupdated state changes?
http://192.168.1.2:8080/api/CB9C782AB7/sensors/2 GET
“state”: {
“buttonevent”: 7007,
“lastupdated”: “2018-06-09T00:33:16”
},

Setting up a http binding with a polling scheme or something else?

That’s the answer!
And use the JSONPATH transform to retrieve your values

Or there is xiaomi binding…

Started up with that and the Xiaomi Gateway only to find out that it only reported gestures, not which side was facing up. deCONZ from Dresden was recommended. deCONZ require the ConBee USB Gateway so I bought that one. The ConBee is not supported by the openHAB Zigbee binding, only the TI USB stick. Also unsure if the ZigBee binding supports the Mi/Aqara cubes.

For further ‘which face up’ reading, head over here.

Use the http binding with cache
You can pull as often as you want but don’t do it too often or you’ll clog up your LAN.

http binding cached item conf:

In your case:

Thing:

XiaomiCubeCache.url=http://192.168.1.2:8080/api/CB9C782AB7/sensors/2
XiaomiCubeCache.updateInterval=60000

Items:

Number XiaomiCubeButtonEvent { http="<[XiaomiCubeCache:10000:JSONPATH($.state.buttonevent)]" }
String XiaomiCudeLastUpdated { http="<[XiaomiCubeCache:10000:JSONPATH($.state.lastupdated]" }

You’ll have to play with the timings to accommodate your use case

1 Like

Shouldn’t be a problem on localhost :wink:

I’m familiar with the http binding cache, but JSON is a first for me.
Thanks for posting examples.

Now that I have deCONZ working on Windows, I need to figure out how to move to my Ubuntu Server openHAB box. Don’t have a desktop environment there, but I can run X11 applications.
Also interesting to se if the ConBee paired Cube follows over from Win to Lin. I think it will do, as the pairing should be persisted in the ConBee USB stick? Also need to move the deCONZ REST API away from port 8080.

An alternative just came up.

Turned out the Hue-Binding couldn’t find deCONZ when it was on a non-standard port.
Ended up using the http binding:

Here are the tested files:
http.cfg

# deCONZ Cube Pink1 side/gestures sensor 2
MiCubePink1GestureCache.url=http://127.0.0.1:8090/api/0365EEAA94/sensors/2
MiCubePink1GestureCache.updateInterval=500

# deCONZ Cube Pink1 rotation sensor 3
MiCubePink1RotationCache.url=http://127.0.0.1:8090/api/0365EEAA94/sensors/3
MiCubePink1RotationCache.updateInterval=500

mi.items

String Pink1CubeButtonLastUpdated { http="<[MiCubePink1GestureCache:500:JSONPATH($.state.lastupdated)]" }
Number Pink1CubeButtonEvent { http="<[MiCubePink1GestureCache:500:JSONPATH($.state.buttonevent)]" }

String Pink1CubeRotAngleLastUpdated { http="<[MiCubePink1RotationCache:500:JSONPATH($.state.lastupdated)]" }
Number Pink1CubeRotAngleEvent { http="<[MiCubePink1RotationCache:500:JSONPATH($.state.buttonevent)]" }

mi.rules

rule "Xiaomi Cube action"
when
    Item Pink1CubeButtonLastUpdated changed
then
    Thread::sleep(20)
    var actionName = Pink1CubeButtonEvent.state

    var name = "Cube P1" 
    logInfo(name, "Event: " + actionName)
end

rule "Xiaomi Cube angle"
when
    Item Pink1CubeRotAngleLastUpdated changed
then
    var Cstate = Pink1CubeRotAngleEvent.state

    var name = "Cube P1" 
    logInfo(name, "Rotation: " + Cstate)
end

log example

2018-06-09 20:47:28.996 [INFO ] [lipse.smarthome.model.script.Cube P1] - Event: 6000
2018-06-09 20:50:16.084 [INFO ] [lipse.smarthome.model.script.Cube P1] - Event: 7000
2018-06-09 20:50:17.087 [INFO ] [lipse.smarthome.model.script.Cube P1] - Event: 5006
2018-06-09 20:50:19.084 [INFO ] [lipse.smarthome.model.script.Cube P1] - Rotation: 9020

The big upside to using this approach instead of the Xiaomi Gateway & Binding, is that deCONZ reports which side is facing up when flipped, tapped or moved. It also reports what side was facing up before the flip.

deCONZ has 2 outstanding issues: (due in beta .29)
Issue 1
Issue 2

Also note the Thread:sleep(10) needed to ensure buttonevent had the latest value after lastupdated changes.
Have asked doCONZ about this.

1 Like

Many solutions can be improved. :slight_smile:
It hit me that having many sensors the http cache should get the whole /sensors list on each polling.
The items could then include the sensor number in the JSON path.

This works fine:

services/http.cfg

# deCONZ sensors
MiCubePink1GestureCache.url=http://127.0.0.1:8090/api/0365EEAA94/sensors
MiCubePink1GestureCache.updateInterval=500

items/mi.items

Number Pink1CubeButtonEvent { http="<[MiCubePink1GestureCache:600:JSONPATH($.2.state.buttonevent)]" }
String Pink1CubeButtonLastUpdated { http="<[MiCubePink1GestureCache:600:JSONPATH($.2.state.lastupdated)]" }

Number Pink1CubeRotAngleEvent { http="<[MiCubePink1GestureCache:600:JSONPATH($.3.state.buttonevent)]" }
String Pink1CubeRotAngleLastUpdated { http="<[MiCubePink1GestureCache:600:JSONPATH($.3.state.lastupdated)]" }