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;
}