Page 1 of 1
Subscribe to changes in Domoticz switches, Possible?
Posted: Thursday 11 May 2023 19:56
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.
Re: Subscribe to changes in Domoticz switches, Possible?
Posted: Thursday 11 May 2023 22:00
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
Re: Subscribe to changes in Domoticz switches, Possible?
Posted: Thursday 11 May 2023 22:01
by heggink
Mqtt should work. Domoticz will post all changes to domoticz/out topic...
Re: Subscribe to changes in Domoticz switches, Possible?
Posted: Friday 12 May 2023 10:37
by PierreT
What's wrong with on/off action script entry?
Re: Subscribe to changes in Domoticz switches, Possible?
Posted: Friday 12 May 2023 13:22
by waltervl
You can also use the push HTTPlink option if your application supports it.
https://www.domoticz.com/wiki/HttpLink
Re: Subscribe to changes in Domoticz switches, Possible?
Posted: Friday 12 May 2023 16:24
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.
Re: Subscribe to changes in Domoticz switches, Possible?
Posted: Friday 12 May 2023 18:42
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
Re: Subscribe to changes in Domoticz switches, Possible?
Posted: Friday 12 May 2023 20:08
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...
Re: Subscribe to changes in Domoticz switches, Possible?
Posted: Friday 12 May 2023 21:44
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?
Re: Subscribe to changes in Domoticz switches, Possible?
Posted: Saturday 13 May 2023 12:03
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.
Re: Subscribe to changes in Domoticz switches, Possible?
Posted: Saturday 13 May 2023 12:30
by Lokonli
You have to define the protocol. Add ['domoticz'] as second WebSocket parameter.
Re: Subscribe to changes in Domoticz switches, Possible?
Posted: Saturday 13 May 2023 13:10
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);
};
Re: Subscribe to changes in Domoticz switches, Possible?
Posted: Saturday 13 May 2023 14:55
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
Re: Subscribe to changes in Domoticz switches, Possible?
Posted: Saturday 13 May 2023 14:56
by Lokonli
In Chrome DevTools (F12), Network tab, you should see the websocket connection, and the messages that are sent and received.
Re: Subscribe to changes in Domoticz switches, Possible?
Posted: Sunday 14 May 2023 0:16
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.
Re: Subscribe to changes in Domoticz switches, Possible?
Posted: Wednesday 17 May 2023 16:26
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.