How to access Button values/events using REST API

So, I have a simple ZigBee Network in my Astronomical Observatory where the various devices are essentially independent. I just use the network to turn Smart Plugs on/off leading to specific equipment that I what to control from my own Control program or where I need the capability to remotely reboot equipment when there’s a glitch. I successfully do this using REST API calls. Similarly, I use REST API calls to get the open/close status of certain openable items like the Observatory door and Dome’s Shutter. I don’t use Scenes, Stored Rules or Groups as I haven’t found a need for them up to now (where there are logic rules like Dehumidifier shouldn’t be on when Door or Shutter are open) I control this using my own Control program where I have access to other information not in the Network.

Now I’ve ordered a 4 button zigbee smart remote controller (NEDIS ZBRC10WT) that I’m hoping I can utilise for sending particular Commands through to my Control Program, that for example might move the Observatory from one mode of operation to another without having to log onto a computer or tablet to effect the change.

  • I’m trusting/hoping that Phoscon will accept the 4 -button controller to the Network (but no guarantee, it’s not on the Verified Device list). I think it will go in as a sensor with type ZHASwitch, but that’s a guess at this stage.
  • I accept that I might need to cheat by linking the buttons temporarily to my Auxillary Light & maybe create a dummy scene or something.
  • I realise that I will need to establish what button event code numbers correspond to the buttons using a sniffing program or by some try and error (most system seem to use numbers like 1001, 2001 etc)
  • After that I’m really totally clueless on how I would use REST API calls to establish whether a button had been pressed and which one. With my existing REST API calls I’m pulling data from deCONZ to get the current on/off or open/closed states. But with Button Events the events are in a way pushed out and around the network. Do I need to have some form of socket to receive the events ? Or can the events me made to somehow set virtual CLIP Sensors within the network from where I can pull the info that a ‘button’ was pressed and its ‘lastupdated’ time. With these two pieces of data I can establish if the button press is recent and is one that my program hasn’t yet responded to and then act.

If anyone can offer some firm examples where they’ve had to do something like this or have some suggestions, I would be grateful for the assistance.

Hello the synthax is X00Y, X is the button number and Y the event Supported Devices · dresden-elektronik/deconz-rest-plugin Wiki · GitHub

What is your “third app” that use the API, deconz use websocket to send state change. It depend of language but no so hard to find websocket connection sample in all language, it’s something classic.
But better to use websocket than spamming REST Request

Replying to Smanar.

My “third app” is my own VB.Net application. I have a small amount of experience in adapting examples using sockets - I’ve them working for client/server sockets for reading TCP/IP data from a separate computer on my LAN, for reading data from an inline web resource, and I have a program that listens for events (Gamma Ray Busters from space) from an online data network, so I have some basis to work from. It will just be about making it work in practice. The REST API are good for get basic data, initial states and for setting state like turning on a Smart Plug, but for spotting changes in the open/close sensor and in the future from button events I can see how a web-socket would indeed be better than spamming lots of REST requests (I polling every 2.5s at moment) where the vast majority of responses will indicate no change.

I will try out websocket connections approach in due course. I’m hopeful to get something eventually working, but at the moment my zigbee network is down as I’ve managed to completely corrupt my ConBee II device and I’m unable to successfully flash firmware back on to it)

Don’t worry, it’s almost impossible to break it, try using command line Update deCONZ manually · dresden-elektronik/deconz-rest-plugin Wiki · GitHub
And never on virtual machine

I managed to get my ConBee II device back. Either though flashing the RPI file was giving write error, flashing the ConBeeII file was successful, and I was able to open my device in deCONZ and thankfully see my network again. Thanks

I’ve now built a working WebSockets listener in VB.Net and can extract when a particular light or sensor changes, and in future can use it for recognising and reacting to button presses on a 4 button remote controller (waiting on delivery) in a few days). I put the basic code below:

Although I’ve done my own Sockets listener from standard .Net it was too difficult to do it for WebSockets, there was very few/if any examples for VB (the examples there are all used 3rd party libraries), and the C examples I found were just too complex to even begin to convert. So I opted to go with a Nuget package from “WebSockets4Net” which made the task very simpler:


using WebSocket4Net

Public Class Form1

Private websocket As WebSocket4Net.WebSocket

Sub SetUpWebSocket()
websocket = New WebSocket4Net.WebSocket(“ws://{URL}:443”)
AddHandler websocket.Opened, Sub(s, e) socketOpened(s, e)
AddHandler websocket.MessageReceived, Sub(s, e) socketMessage(s, e)
AddHandler websocket.Closed, Sub(s, e) socketClosed(s, e)
AddHandler websocket.Error, Sub(s, e) socketError(s, e)

        websocket.Open()
        Console.WriteLine(websocket.State)
        websocket.AutoSendPingInterval = 10
End Sub

Sub socketOpened(s As Object, e As EventArgs)
    Console.WriteLine("Socket Opened")
    Console.WriteLine(websocket.State)      
End Sub

Sub socketMessage(s As Object, e As WebSocket4Net.MessageReceivedEventArgs)
Console.WriteLine ("Socket Message: " + e.Message)

   // Parse e.message

   // Do something with the parse message / event

End Sub

Sub socketClosed(s As Object, e As EventArgs)
Console.WriteLine(“Socket Closed”)
End Sub

Sub socketError(s As Object, e As SuperSocket.ClientEngine.ErrorEventArgs)
Console.WriteLine("Socket Error: " + e.Exception.ToString)
End Sub

End Class

Is the purpose to write your own system? Otherwise you can use one of the ready implementations to control deconz

In regard to ‘ready implementations’ are you referring to products like Home Assistant / Google Home ? (or something different ?). Neither would be applicable to my needs. Let me explain :

I have an Observatory at ‘home’ which I use for automated astronomical observing, which is controlled by my own VB.Net application. I click a button at dusk and it runs all night and collects images from different targets with automated control of Dome/Telescope/Camera/Focusers, dealing with changing weather conditions as appropriate. My original need started with a pair of smart plugs that I could remotely power reset devices which happened to developed a glitch and which needed to be restarted (as there occurrence is a showstopper). I originally used a HUE app from my IPAD to do this (to avoid a trip to the observatory). I had to go to kitchen to do this as I still needed to be in range of the Observatory and of course this needed to have happened before I went to bed. (zigbee/deconz has better range by the way than the simple HUE app).

But for full automation I need my VB.Net control program to make its own executive decision to power reset a suspected device and carefully resume the night’s observing schedule. Not a task that ‘ready implementations’ can do as it needs to include data not in the zigbee mesh network itself.

I also use my application to control a Smart Plug enabled dehumidifier. In theory this could probably be controlled by ready implementations (with reference to shutter (window), door, humidity and temperature sensors ) but at the moment I use external (non-mesh) sensors for Temp/Humidity, I haven’t acquired a zigbee Temp/Humidity sensor as yet.

Whilst my dome’s driver informs my system whether the shutter of the dome is open or closed, I have just introduced a zigbee open/close sensor which my application can use as additional assurance that the shutter has fully closed when it gets cloudy or starts to rain.

I’m getting a 4-button remote control as I want to experiment with using it to make quick simple communication to my observatory control program. The controller + zigbee network offers the possibility of doing specific tasks without needing to log on to a PC / IPAD and then open a vnc connection to the observatory). I’m not sure what functions I want it ultimately perform that’s part of the learning experience.

The following picture shows what I have in my zigbee mesh network at the moment:

Sound like a fun project!