Expected behavior of MQTT Plug-ins during App Refresh

My project uses some MQTT Plug-ins based on your example. For most part, it all works without issue but sometimes when I pick up my tablet and wake up the screen, it is like the MQTT data will not populate. So I press “App Refresh” button and usually it will refresh but about 30% of the time, I see toast message stating “Failed to stop name_of_MQTT_plugin” and then similar message for each MQTT plugin. I added a 2nd button similar to App Refresh but does an App Reload. Sometimes this will refresh the data but sometimes not requiring me to close out HR and restart HR. Restarting HR seems to work 100% of the time.

Any idea why these plug-ins will not refresh with the other devices at times? Below is a typical plug-in used for sending IR commands to a specific device.

Thanks

plugin.Name = “LED_TV”;
plugin.OnChangeRequest = onChangeRequest;
plugin.OnConnect = onConnect;
plugin.OnDisconnect = onDisconnect;
plugin.OnPoll = onPoll;
plugin.OnSynchronizeDevices = onSynchronizeDevices;
plugin.PollingInterval = 500;
plugin.DefaultSettings = { “Host”: “”, “Port”: “”, “Username”: “”, “Password”: “” };

var mqtt = new MQTTClient();
var subscribed = false;

var commands = {
“Power” : ‘{“Protocol”:“NEC”,“Bits”:32,“Data”:0x57E3E817,“DataLSB”:0xEAC717E8,“Repeat”:0}’,
“ChannelUp” : ‘{“Protocol”:“NEC”,“Bits”:32,“Data”:0x57E39867,“DataLSB”:0xEAC719E6,“Repeat”:0}’,
“ChannelDown” : ‘{“Protocol”:“NEC”,“Bits”:32,“Data”:0x57E3CC33,“DataLSB”:0xEAC733CC,“Repeat”:0}’,
“Back” : ‘{“Protocol”:“NEC”,“Bits”:32,“Data”:0x57E36699,“DataLSB”:0xEAC76699,“Repeat”:0}’,
“Home” : ‘{“Protocol”:“NEC”,“Bits”:32,“Data”:0x57E3C03F,“DataLSB”:0xEAC703FC,“Repeat”:0}’,
“Mute” : ‘{“Protocol”:“NEC”,“Bits”:32,“Data”:0x57E304FB,“DataLSB”:0xEAC720DF,“Repeat”:0}’,
“DirUp” : ‘{“Protocol”:“NEC”,“Bits”:32,“Data”:0x57E39867,“DataLSB”:0xEAC719E6,“Repeat”:0}’,
“DirDown” : ‘{“Protocol”:“NEC”,“Bits”:32,“Data”:0x57E3CC33,“DataLSB”:0xEAC733CC,“Repeat”:0}’,
“DirLeft” : ‘{“Protocol”:“NEC”,“Bits”:32,“Data”:0x57E37887,“DataLSB”:0xEAC71EE1,“Repeat”:0}’,
“DirRight” : ‘{“Protocol”:“NEC”,“Bits”:32,“Data”:0x57E3B44B,“DataLSB”:0xEAC72DD2,“Repeat”:0}’,
“DirEnter” : ‘{“Protocol”:“NEC”,“Bits”:32,“Data”:0x57E354AB,“DataLSB”:0xEAC72AD5,“Repeat”:0}’,
“VolumeUp” : ‘{“Protocol”:“NEC”,“Bits”:32,“Data”:0x57E3F00F,“DataLSB”:0xEAC70FF0,“Repeat”:0}’,
“VolumeDown” : ‘{“Protocol”:“NEC”,“Bits”:32,“Data”:0x57E308F7,“DataLSB”:0xEAC710EF,“Repeat”:0}’,
“Sleep” : ‘{“Protocol”:“NEC”,“Bits”:32,“Data”:0x57E346B9,“DataLSB”:0xEAC7629D,“Repeat”:0}’,
“Previous” : ‘{“Protocol”:“NEC”,“Bits”:32,“Data”:0x57E31EE1,“DataLSB”:0xEAC77887,“Repeat”:0}’,
“Options” : ‘{“Protocol”:“NEC”,“Bits”:32,“Data”:0x57E38679,“DataLSB”:0xEAC7619E,“Repeat”:0}’,
}

function onChangeRequest(device, attribute, value) {
if (attribute != “MediaCommand”)
throw “Invalid attribute”;

var command = commands[value];
if (!command)
    throw "Invalid command";

 	mqtt.publish("cmnd/ledTV/IRSEND", (command), {retain: false, qos: 1});
 	console.log(command);
 	console.log(value);

}

function onConnect() {
mqtt.connect(“mqtt://” + plugin.Settings[“Username”] + “:” + plugin.Settings[“Password”] + “@” + plugin.Settings[“Host”] + “:” + plugin.Settings[“Port”]);
subscribed = false;

	var irDevice = plugin.Devices["LED_TV"];
if (irDevice != null) {
    irDevice.SupportedMediaCommands = Object.keys(commands);
}

}

function onDisconnect() {
mqtt.disconnect();
}

function onPoll() {
if (!subscribed) {
mqtt.subscribe(“stat/ledTV/POWER”);
subscribed = true;
}
while (true) {
var message = mqtt.readMessage();
var payloadString = message.payload.toString();
var switchDeviceId = “LED_TV”;
var switchDevice = plugin.Devices[switchDeviceId];
switchDevice.Switch = ((payloadString == “ON”) ? “On” : “Off”);
}
}

function onSynchronizeDevices() {
var irDevice = new Device();
irDevice.Id = “LED_TV”;
irDevice.DisplayName = ‘LED TV’;
irDevice.Capabilities = [“MediaControl”,“Switch”];
irDevice.Icon = “TV”;
irDevice.Attributes = [];
plugin.Devices[irDevice.Id] = irDevice;
}

How long are you waiting after waking up the screen? It really sounds like the network adapter is asleep & the online status hasn’t been broadcast yet. Don’t just click the refresh button right away. Wait 30 seconds or so. If it never automatically comes back online, then we know we have an issue.

Still an issue after the network controller wakes up. I run a MQTT Client device as well as 4 MQTT plugins, all similar to one above. The MQTT Client connects shortly after the tablet wakes up as I can see that data populate. Other NON- MQTT devices populate as well I looked at my MQTT Broker log, but nothing of value.

One other thing, I also run similar project with MQTT Plugins on 2 display tablets that are “always on”. These can maintain the MQTT connection for days provided no network interruptions; however, when they do act up - pressing App Refresh gives me similar “Failed to Stop MQTT Plugin” messages requiring me to restart the app. Not a deal breaker, just curious if anyone else sees this message.

Did you upgrade to 3.6.0? It was published late yesterday so you might not have it yet. I did make
a few changes to help correct this issue.

Yes, I did the upgrade. Will let you know if I see a change.

Just to follow up, the upgrade to 3.6 did seem to help the problem. When it does happen now, “App Reload” seems to get everything back on track. Thanks.

Good to hear. Does it have to be App Reload? Does App Refresh not fix it?

Reload resets everything, UI & devices. Refresh only does devices. So Refresh would be the better option if it fixes your issue.

Bill, after watching things for another week, here is what I can offer:

  1. MQTT connections do not disconnect as often as before.
  2. When they do, App Refresh really does not do anything. Not even many if any toast messages.
  3. App Reload does seem to work most of the time.
  4. I have confirmed similar response on 2 different tablets 1 of which is “always on”.

App Refresh used to work for me. Odd that it is acting different now.
Thanks