AnotherDaniel / smahub

SMAHub is a flexible and modular solution for collecting data from SMA photovoltaic (PV) products and publishing it to various output channels, such as MQTT.
GNU General Public License v2.0
31 stars 11 forks source link

MQTT reconnect #27

Closed jardleex closed 9 months ago

jardleex commented 9 months ago

Hello @AnotherDaniel,

thanks for this awesome tool. I'm running it with an SMA Tripower X 25 with MQTT in iobroker providing data for evcc.

One thing I noticed is that smahub does not seem to reconnect when it lost it's MQTT connection. For example if I update my iobroker container, and thus my MQTT broker, smahub does not reconnect. I've set the depends_on setting in my docker-compose file but it seems Docker restarts smahub to quick before MQTT in iobroker is ready.

Would it be possible to put a reconnect loop into the MQTT (and maybe other) sinks?

AFAIK it's not possible to have additional/multiple health checks in a docker-compose file. I had the idea to extend the health check of my iobroker to give a proper health check but it seems not possible. Also there is no health check for smahub, which I assume would be hard to integrate when using multiple sources or sinks.

Thanks in advance.

AnotherDaniel commented 9 months ago

Hi @jardleex , that is a great idea actually! I will look into that, will let you know once I have something. Should you get to it first, PRs are always welcome ;-)

AnotherDaniel commented 9 months ago

Alright - we have smahub v1.5.4, check it out - brings auto-reconnect for the MQTT sink plugin, with a currently hard-coded max wait time (5 mins) and exponential back-off. If necessary, these parameters might be made configurable...

jardleex commented 9 months ago

Thanks for the quick handling! I had to perform docker updated today and thus restart all my containers incl. iobroker (mqtt broker). It seemed to have worked better this time. May it be possible to get a log output when smahub lost it's connection to any source/sink? This would allow troubleshooting after an incident to find out when a connection was lost. For example evcc also logs that in his stdtout and attempts a reconnect each X minutes/seconds and also prints that on stdout. Maybe put that behind SMAHUB_VERBOSE: true, maybe not everybody want's to see this.

AnotherDaniel commented 9 months ago

You will get some debug output on connection loss/reconnect with the 'verboser' mode (-V) - but I can see that you might want reconnect info without the other big-V clutter, so fair point. Did the change, new release might take a couple of days while I look at the tls request.

AnotherDaniel commented 9 months ago

Update: v1.5.5 released, includes log-level adjustments to the reconnect output.

jardleex commented 9 months ago

Awesome works great, thank you! I just stopped the MQTT instance in my iobroker and got the expected output in smahub.

2024-02-07 07:37:35 INFO     SHM2 plugin disabled
2024-02-07 07:37:35 INFO     Starting Tripower X source
2024-02-07 07:37:35 INFO     EV Charger plugin disabled
2024-02-07 07:37:35 INFO     demo source plugin disabled
2024-02-07 07:37:35 INFO     Starting MQTT sink
2024-02-07 07:37:35 INFO     HA-MQTT sink plugin disabled
2024-02-07 07:37:35 INFO     gen_ha_sensors sink plugin disabled
2024-02-07 07:37:35 INFO     demo sink plugin disabled
2024-02-07 07:37:35 INFO     Connected to MQTT Broker
2024-02-07 07:38:11 WARNING  Disconnected from MQTT Broker
2024-02-07 07:38:12 WARNING  Disconnected from MQTT Broker
2024-02-07 07:38:12 WARNING  Attempting to reconnect to MQTT Broker...
2024-02-07 07:38:12 WARNING  Reconnected to the MQTT broker
2024-02-07 07:38:14 WARNING  Disconnected from MQTT Broker
2024-02-07 07:38:14 WARNING  Attempting to reconnect to MQTT Broker...
2024-02-07 07:38:14 WARNING  Reconnected to the MQTT broker
2024-02-07 07:38:18 WARNING  Disconnected from MQTT Broker
2024-02-07 07:38:18 WARNING  Attempting to reconnect to MQTT Broker...
2024-02-07 07:38:18 WARNING  Reconnected to the MQTT broker
2024-02-07 07:38:26 INFO     Connected to MQTT Broker