albertnis / mqcontrol

Cross-platform utility to execute commands remotely using MQTT
https://albert.nz/remote-computer-control-mqtt/
14 stars 2 forks source link

MQTT replies #13

Open concifederico opened 11 months ago

concifederico commented 11 months ago

Albert, Great software for home automation. The question is if there would be a way to post a message to a topic on the MQTT server when the command is activated, like a reply or a status change on a topic. Regards,

Federico

albertnis commented 11 months ago

Hi Federico, glad you're enjoying mqcontrol! Currently, "replies" are not a feature, but I would certainly be interested to learn more about your use-case. Could you describe how you imagine these replies functioning? Would the reply depend on the output or status of command execution? Or would it be a static message confirming the command was executed?

If you're simply after greater assurance that your message was published successfully by the emitting client to the server, you could look in MQTT Quality of Service. But I suspect you're after something more.

concifederico commented 11 months ago

Hi Albert, thank you for your fast reply. You asked, I'll answer what I want: to know if my son has turned on the PC to play games. So I would turn it off remotely when he get stuck on it hehehe.. but for that I need to know the PC status. Mine is not plugged to a smart switch like yours. I have few of them laying around but trust me all of them need to be installed on specific lights! Thanks you and regards!

albertnis commented 11 months ago

Thanks for the explanation. Do you think what you're attempting could be achieved with MQTT's Last Will and Testament (LWT) feature?

Using this feature, you could run an MQTT client on the PC (in the background and separately to mqcontrol) which publishes a simple status message (e.g. "on" -> /topic/path/pc) every few minutes or seconds. The client would specify a LWT of "off" -> /topic/path/pc. That means that when the client disconnects unexpectedly (such as computer turning off or sleeping), the server will publish "off" -> /topic/path/pc on behalf of the PC, which would be seen by any subscribers. Then, when the PC is back on, it will resume publishing "on" messages.

There might be a way to achieve this with the mosquitto_pub and mosquitto_sub CLI tools. Look into the --will-topic and --will-payload flags. You might run mosquitto_pub on a schedule to publish on messages, then run mosquitto_sub constantly to stay connected to the client with an LWT if it's disconnected.

As for implementing this behaviour in mqcontrol: Currently mqcontrol works in a "receive-only" mode so adding outgoing messages requires some thought. I think LWT behaviour would be appropriate in mqcontrol, given that this is a long-running client. But I'm less sure about what other outgoing messages would look like. I'll have a bit more of a think. If you have any specific ideas about how this functionality might be incorporated into mqcontrol, I'm keen to hear them.