serkri / SmartEVSE-3

Smart Electric Vehicle Charging Station (EVSE)
MIT License
67 stars 28 forks source link

Added availability_topic to the announce message #194

Closed marcovaneck closed 11 months ago

marcovaneck commented 11 months ago

By setting the Birth / Last-Will messages and adding the topic to the Home Assistant discovery messages. Home Assistant will show unavailable when the EVSE is offline.

Note: this doesn't solve the concurrency issue with MQTTclient.publish

dingo35 commented 11 months ago

Thanks for your contribution!

VelocityMaximus commented 11 months ago

By setting the Birth / Last-Will messages and adding the topic to the Home Assistant discovery messages. Home Assistant will show unavailable when the EVSE is offline.

The 'last will' works except that there is no timed update to refresh the connected state. When the MQTT broker is restarted, the 'online' state gets no update and thus being offline. You can add a timer that updates the 'connected' state or update it when there is another value to publish.

dingo35 commented 11 months ago

As far as I understand the MQTT broker should retain last will messages, even after restart. Wouldnt make sense to have to have all these clients this logic built in.

Are you sure you configured your broker correctly (especially the persistence part)?

VelocityMaximus commented 11 months ago

It is not a retained message, that is something different.

A 'last will' message needs to be updated in a manner of time. This is a watchdog mechanism that needs to be updated within a certain time. If there is no update at all, the MQTT 'last will' revert automatic to offline. A more in depth explanation is at https://www.hivemq.com/blog/mqtt-essentials-part-9-last-will-and-testament/

Please do not assume that my broker does not work well. There are 47 IoT devices connected that use MQTT with this mechanism to make it reliable.

marcovaneck commented 11 months ago

My MQTT broker (mosquitto) is stable, running since the last power-outage. I think when the connection is reestablished the "last will" is not passed again to the broker (in the connect-message if my memory is correct). I will look into the used MQTT library, if it stores the "last will" and use it again in the reconnect. After a reconnect the subscriptions, should be made again. So probably that's also broken.

I'll have a look at reconnecting logic.

Ps. should we move this to an issue to have more visibility?

dingo35 commented 11 months ago

Your retain flags are all set, so it seems the calls to the library are all ok...

dingo35 commented 11 months ago

The default keepalive_interval for mosquitto is 60s; if you have a mainsmeter and/or an EVmeter installed through modbus it will update its currents every 2s, so the keepalive should not be a problem.

dingo35 commented 11 months ago

I still can't reproduce @VelocityMaximus's problem.

If the SmartEVSE is online it says: SmartEVSE-51446/connected online

If I restart mosquitto, it says: SmartEVSE-51446/connected offline ...which corresponds to the webinterface, which says MQTT: Disconnected

Then after one or two seconds MQTT reconnects, so webinterface says: MQTT: Connected ...and the broker reports: SmartEVSE-51446/connected offline

@VelocityMaximus can you please post your http:/smartevse/settings and your /etc/mosquitto contents, as far as they are deviating from the default?

VelocityMaximus commented 11 months ago

I restarted the EVSE and then blocked the WIFI connection and it did not went 'offline' at that time. When I reflashed the firmware (1.7.3) including the spiffs, then it started to work as @marcovaneck described. The same behavior existed to the second EVSE module and now works as expected after reflashing.