hjelev / rpi-mqtt-monitor

Raspberry Pi MQTT Monitor gathers system information and sends it to a MQTT server.
GNU General Public License v3.0
155 stars 41 forks source link

Can HA send MQTT messages back to the Pi? #95

Closed RaceNJason closed 2 months ago

RaceNJason commented 4 months ago

Is it possible to send various (MQTT) messages from Home Assistant back to the Pi?

hjelev commented 3 months ago

Yes system restart and shutdown are sending messages back to the Pi.

RaceNJason commented 3 months ago

That makes sense. Is it possible then to create custom messages? Maybe with optional data packets like states of various devices? I haven't taken a deep dive into any of the code (just a quick look is all), but if the framework is already in place this might not take much more to implement. I'm a long time C++ programmer, but I can program in Python when needed (just takes me longer). I'll assume that this would require modifications to both the code on the Linux machine as well as the code running on HA. Even though my Python isn't great...my YAML is worse lol. So this would definitely take some stumbling through for me. If you have any pointers, suggestions or recommendations I'll gladly accept them.

hjelev commented 3 months ago

What is the use case you have in mind? W/o more details I am not sure what to suggest. I am 💬 ng about adding support for custom commands, if hass sends some message payyload, rpi monitor executes a configurable command ...

RaceNJason commented 3 months ago

The simplest thing I'm thinking of is IO control of a remote RPi's pins. Have the ability to set a pin to In, Out or Out PWM. And have the ability to query any pin regardless if it is an input or output. This obviously could only be done on a RPi and not a SBC Linux that doesn't have exposed IO pins...but, I would think most people are installing this API on a RPi.

I have seen IO control of a local RPi's pins (i.e. the one that HA is running on) even though it is a little troublesome to setup. I'm interested in IO control of a remote RPi's pins. Of course if this is possible then use case explodes.

To be able to do actual text/binary messages would require the API to handle add-ons such that an end user can create an add-on that is ran on the RPi and that handles their particular attached module and it's communication protocol. Then this API simply passes through the messages to/from X-AddOn. That would be far more complicated since it would require the ability to pass messages to/from some other running program. Being way more familiar with Windows (and C++) there are additional steps to pass messages to/from other programs which I am unfamiliar with how to do on a Linux machine (so I'm basing what I think is complicated on my own experience on Windows programs, but maybe this is something easy to do on a Linux machine??).

RaceNJason commented 3 months ago

With Windows the easiest way to do add-ons would be to have end users create COM modules and your program loads the modules (via a specific directory it looks in on startup) and then calls any loaded modules via the specific exposed function as well as links to them (via the callback). Only those 2 functions are exposed and used. Although I don't see many programmers these days making COM modules, it really isn't as hard to do them once you've done a few. Most programming I've done with Linux has been with Python and scripts. I'm scratching my head how you would convert a script into some form of a add-on like what I describe above...

RaceNJason commented 3 months ago

Well, as always with home assistant, more digging found pigpio that can be configured (manually via yaml) to do what I was asking as one use case. So, not the best use case if it is already done. Surprising too is that it says it is only being used by 22 active users. Seems awfully low for a feature like that. I plan to try and get it working myself to test. I'll have to see if I can come up with something else far more exciting then sending a pi a notification message.