LWT Offline... help wakeup mqtt status

Hi everyone, I have a python script that reads the values ​​from my gps and publishes them via MQTT on HA. Everything works fine until I try to unplug the gps antenna and the HA status becomes unavailable. Via MqttExplorer I have LWT = Offline and the status doesn’t change when i reconnect the gps antenna.

Then I tried to execute the script manually from the command line (putty ssh) I noticed that disconnecting the GPS antenna, the data stops but immediately resumes as soon as the hang up…
in HA and MqttExplorer instead no activity … how to “awaken” this status ???
if I stop the command and run it again, the data in HA and MqttExplorer reappear correctly.

my sensor is:

  - platform: mqtt
    name: "pymea_latitude"
    state_topic: "tele/pymeagps/SENSOR"
    value_template: "{{ value_json.latitude | round(6) }}"
    availability_topic: "tele/pymeagps/LWT"
    unit_of_measurement: "°"
    payload_available: "Online"
    payload_not_available: "Offline"

  - platform: mqtt
    name: "pymea_longitude"
    state_topic: "tele/pymeagps/SENSOR"
    value_template: "{{ value_json.longitude | round(6) }}"
    availability_topic: "tele/pymeagps/LWT"
    unit_of_measurement: "°"
    payload_available: "Online"
    payload_not_available: "Offline"

this in the script file for reading from gps

#!/usr/bin/env python3
import serial
import time
import string
import pynmea2
import paho.mqtt.client as mqtt
from gps3 import agps3
import coordTransform_py.coordTransform_utils as transform 
broker="171.151.1.190"
port=1883
username="mqtt_user"
password="passwordmqtt"
topicLWT="tele/pymeagps/LWT"
topicSENSOR="tele/pymeagps/SENSOR"
payloadSENSOR=""
####create client object
mqttClient = mqtt.Client("pymea")
#### Set LWT  
mqttClient.will_set(topicLWT, "Offline")             
#### Set broker credentials
mqttClient.username_pw_set(username, password)
#### Establish connection...
mqttClient.connect(broker,port)
#### Set status Online
mqttClient.publish(topicLWT, "Online")
#GPSDSocket creates a GPSD socket connection & request/retrieve GPSD output.
gps_socket = agps3.GPSDSocket()
#DataStream unpacks the streamed gpsd data into python dictionaries.
data_stream = agps3.DataStream()
gps_socket.connect()
gps_socket.watch()
gcj02_lng_lat = [0.0,0.0]
bd09_lng_lat = [0.0,0.0]
for new_data in gps_socket:
    if new_data:
        data_stream.unpack(new_data)
        if data_stream.lat != 'n/a' and data_stream.lon != 'n/a':
            gcj02_lng_lat = transform.wgs84_to_gcj02(float(data_stream.lon),float(data_stream.lat))
            bd09_lng_lat = transform.wgs84_to_bd09(float(data_stream.lon),float(data_stream.lat))
            print('google lon.lat = %.6f,%.6f' %(gcj02_lng_lat[1],gcj02_lng_lat[0])) 
            print('')
            payloadSENSOR = '{"latitude":' + str(round(gcj02_lng_lat[1],6)) + ', "longitude":' + str(round(gcj02_lng_lat[0],6)) + '}'
            mqttClient.publish(topicSENSOR,payloadSENSOR) 

thanks a lot.

for new_data in gps_socket

Will this loop exit? Put a print after the for loop.

The LWT only updates if you disconnect from the MQTT server ungracefully. A normal mqtt.disconnect, it is expected that you update your status yourself.

It is possible that the python app is disconnecting gracefully when the loop is done. The only way I see that being an ‘ungraceful’ disconnect is if an exception is thrown on gps removal.