Subscribe to changes in Domoticz switches, Possible?

Topics (not sure which fora)
when not sure where to post, post here and mods will move it to right forum.

Moderators: leecollings, remb0

Post Reply
rgroothuis
Posts: 347
Joined: Friday 03 April 2015 17:09
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Subscribe to changes in Domoticz switches, Possible?

Post by rgroothuis »

I'm polling the Domoticz system via API calls (http...../json.htm......) to get the latest status of the switches. Polling is not efficient, status only updated according the polling speed, extra data traffic, etc. Is there a way to subscribe to Domoticz and be informed if the status of a Switch changes?
In other words, my app is being informed by Domoticz if there is a status change in one or more switches.

Looking forward to your feedback and suggestions.
Lokonli
Posts: 2262
Joined: Monday 29 August 2016 22:40
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Subscribe to changes in Domoticz switches, Possible?

Post by Lokonli »

You can open a websocket. Changes in devices are pushed by Domoticz via the websocket to clients.

This mechanism is used by the Domoticz web client and by Dashticz.

Or use mqtt
User avatar
heggink
Posts: 972
Joined: Tuesday 08 September 2015 21:44
Target OS: Raspberry Pi / ODroid
Domoticz version: 12451
Location: NL
Contact:

Re: Subscribe to changes in Domoticz switches, Possible?

Post by heggink »

Mqtt should work. Domoticz will post all changes to domoticz/out topic...
Docker in Truenas scale, close to latest beta
DASHTICZ 🙃
RFXCOM, zwavejs2mqtt, zigbee2mqtt,
P1 meter & solar panel
Google home, Wifi Cams motion detection
Geofence iCloud, Bluetooth & Wifi ping
Harmony hub, Nest, lots more :-)
PierreT
Posts: 49
Joined: Wednesday 03 May 2023 10:12
Target OS: NAS (Synology & others)
Domoticz version:
Contact:

Re: Subscribe to changes in Domoticz switches, Possible?

Post by PierreT »

What's wrong with on/off action script entry?
User avatar
waltervl
Posts: 5149
Joined: Monday 28 January 2019 18:48
Target OS: Linux
Domoticz version: 2024.7
Location: NL
Contact:

Re: Subscribe to changes in Domoticz switches, Possible?

Post by waltervl »

You can also use the push HTTPlink option if your application supports it. https://www.domoticz.com/wiki/HttpLink
Domoticz running on Udoo X86 (on Ubuntu)
Devices/plugins: ZigbeeforDomoticz (with Xiaomi, Ikea, Tuya devices), Nefit Easy, Midea Airco, Omnik Solar, Goodwe Solar
rgroothuis
Posts: 347
Joined: Friday 03 April 2015 17:09
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Subscribe to changes in Domoticz switches, Possible?

Post by rgroothuis »

PierreT wrote: Friday 12 May 2023 10:37 What's wrong with on/off action script entry?
I've thought about that as well indeed. I'm playing with a mobile app. I don't know the IP address of the mobile device and also I don't know within Domoticz if the App is running on the mobile or not. Apart from that I don't want to make much config changes within Domoticz as that makes the installation process for a wider audience more complex. Hence my idea/question if it is possible when the App is running to subscribe to switch changes in Domoticz and that Domoticz then sends message to the mobile App.
grecof973
Posts: 24
Joined: Wednesday 26 July 2017 6:41
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Subscribe to changes in Domoticz switches, Possible?

Post by grecof973 »

Lokonli wrote: Thursday 11 May 2023 22:00 You can open a websocket. Changes in devices are pushed by Domoticz via the websocket to clients.

This mechanism is used by the Domoticz web client and by Dashticz.

Or use mqtt
Greetings!! Can you give me some more info on using the websocket???
Or some links about it
Lokonli
Posts: 2262
Joined: Monday 29 August 2016 22:40
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Subscribe to changes in Domoticz switches, Possible?

Post by Lokonli »

grecof973 wrote: Friday 12 May 2023 18:42
Lokonli wrote: Thursday 11 May 2023 22:00 You can open a websocket. Changes in devices are pushed by Domoticz via the websocket to clients.

This mechanism is used by the Domoticz web client and by Dashticz.

Or use mqtt
Greetings!! Can you give me some more info on using the websocket???
Or some links about it
I can give a link to the js code:
https://github.com/Dashticz/dashticz/bl ... i.js#LL182

The code is a bit messy...
rgroothuis
Posts: 347
Joined: Friday 03 April 2015 17:09
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Subscribe to changes in Domoticz switches, Possible?

Post by rgroothuis »

The websocket sounds really interesting. The only thing left is to find out how this is working. Any more info available apart from the source code which is difficult to read how it works?
rgroothuis
Posts: 347
Joined: Friday 03 April 2015 17:09
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Subscribe to changes in Domoticz switches, Possible?

Post by rgroothuis »

Somebody a suggestion on how to make a websocket connection to Domoticz? I'm playing around with this for a couple of hours but without success.

React native example I tried (not a complete list):

var ws = new WebSocket('ws://192.168.0.100/')
var ws = new WebSocket('wss://192.168.0.100/')
var ws = new WebSocket('ws://192.168.0.100/json')
var ws = new WebSocket('wss://192.168.0.100/json')
var ws = new WebSocket('ws://192.168.0.100/json'/)
var ws = new WebSocket('wss://192.168.0.100/json'/)
var ws = new WebSocket('wss://xxxxxx:[email protected]/json')
and many more variations but I cannot get a successful connection.

When using the socket.io module within the Postman application I also cannot get a connection.

Is there a specific configuration required in Domoticz? I'm running the latest version so websockets should be support.

Also I don't know how otherwise to debug this! All support is more then welcome.
Lokonli
Posts: 2262
Joined: Monday 29 August 2016 22:40
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Subscribe to changes in Domoticz switches, Possible?

Post by Lokonli »

You have to define the protocol. Add ['domoticz'] as second WebSocket parameter.
rgroothuis
Posts: 347
Joined: Friday 03 April 2015 17:09
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Subscribe to changes in Domoticz switches, Possible?

Post by rgroothuis »

Lokonli wrote: Saturday 13 May 2023 12:30 You have to define the protocol. Add ['domoticz'] as second WebSocket parameter.
Excellent, thanks. That was the trick.

This is not working for me:

var ws = new WebSocket('https://192.168.0.100/json/', ["domoticz"])

ws.onopen = () => {
// connection opened
ws.send('something'); // send a message
};
ws.onmessage = (e) => {
// a message was received
console.log(e.data);
};
ws.onerror = (e) => {
// an error occurred
console.log(e.message);
};
Lokonli
Posts: 2262
Joined: Monday 29 August 2016 22:40
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Subscribe to changes in Domoticz switches, Possible?

Post by Lokonli »

rgroothuis wrote: Saturday 13 May 2023 13:10
Lokonli wrote: Saturday 13 May 2023 12:30 You have to define the protocol. Add ['domoticz'] as second WebSocket parameter.
Excellent, thanks. That was the trick.

This is not working for me:

var ws = new WebSocket('https://192.168.0.100/json/', ["domoticz"])

ws.onopen = () => {
// connection opened
ws.send('something'); // send a message
};
ws.onmessage = (e) => {
// a message was received
console.log(e.data);
};
ws.onerror = (e) => {
// an error occurred
console.log(e.message);
};
Is the connection opening? (is the onopen function called?)

You have to remove the ending slash of the url (/json, not /json/)

In the onopen callback you can send your first message. Something like:

Code: Select all

          var msg = {
            event: 'request',
            requestid: requestid,
            query: myquery,
          };
          requestid = (requestid + 1) % 1000;
          try {
            socket.send(JSON.stringify(msg));
          } catch (ev) {
          	...
          }
 
Example for myquery:

Code: Select all

myquery='type=devices&filter=all&used=true&order=Name'
You will receive a reply via the onmessage callback.

Only after sending a first query via the websocket to Domoticz, Domoticz will send device updates via the websocket to the client.
These device updates will have a requestid -1
Lokonli
Posts: 2262
Joined: Monday 29 August 2016 22:40
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Subscribe to changes in Domoticz switches, Possible?

Post by Lokonli »

In Chrome DevTools (F12), Network tab, you should see the websocket connection, and the messages that are sent and received.
rgroothuis
Posts: 347
Joined: Friday 03 April 2015 17:09
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Subscribe to changes in Domoticz switches, Possible?

Post by rgroothuis »

Sorry. Typo in my text above. Should have been: it is NOW working!! The onopgemerkt is called and without sending data to Domoticz I already get data delivered from Domoticz. This is looking promising. Next is to under the message coming in, how to parse them.
PierreT
Posts: 49
Joined: Wednesday 03 May 2023 10:12
Target OS: NAS (Synology & others)
Domoticz version:
Contact:

Re: Subscribe to changes in Domoticz switches, Possible?

Post by PierreT »

rgroothuis wrote: Friday 12 May 2023 16:24 I've thought about that as well indeed. I'm playing with a mobile app. I don't know the IP address of the mobile device and also I don't know within Domoticz if the App is running on the mobile or not. Apart from that I don't want to make much config changes within Domoticz as that makes the installation process for a wider audience more complex. Hence my idea/question if it is possible when the App is running to subscribe to switch changes in Domoticz and that Domoticz then sends message to the mobile App.
Okay, seems I misunderstood the objective.
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest