My outlet control is next on the list to fix up.
What it was: (can you say nasty)
What I need is a way to tell which kind of message came in. Lets see what they look like.
So if I can test for the existence of msg._session and msg.topic, I should be able to know which message came in and where to send out out. So I took my existing function and added a new section, based off the fact if it is a MQTT or WS message. Here is my function code now.
if (msg._session) {
var jMsg = JSON.parse(msg.payload);
console.log(jMsg);
var nMsg = { payload: "0" }
if (jMsg.state == "on")
nMsg.payload = "1";
if (jMsg.outlet == "outlet1") {
return [null,null,null,null,null,null,null,nMsg];
}
if (jMsg.outlet == "outlet2") {
return [null,null,null,null,null,null,nMsg,null];
}
if (jMsg.outlet == "outlet3") {
return [null,null,null,null,null,nMsg,null,null];
}
if (jMsg.outlet == "outlet4") {
return [null,null,null,null,nMsg,null,null,null];
}
if (jMsg.outlet == "outlet5") {
return [null,null,null,nMsg,null,null,null,null];
}
if (jMsg.outlet == "outlet6") {
return [null,null,nMsg,null,null,null,null,null];
}
if (jMsg.outlet == "outlet7") {
return [null,nMsg,null,null,null,null,null,null];
}
if (jMsg.outlet == "outlet8") {
return [nMsg,null,null,null,null,null,null,null];
}
}
if (msg.topic) {
var topic = msg.topic;
var nMsg = { payload: "0" }
if (msg.payload == "ON")
nMsg.payload = "1";
if (topic == "tower/outlet0001/control") {
return [null,null,null,null,null,null,null,nMsg];
}
if (topic == "tower/outlet0002/control") {
return [null,null,null,null,null,null,nMsg,null];
}
if (topic == "tower/outlet0003/control") {
return [null,null,null,null,null,nMsg,null,null];
}
if (topic == "tower/outlet0004/control") {
return [null,null,null,null,nMsg,null,null,null];
}
if (topic == "tower/outlet0005/control") {
return [null,null,null,nMsg,null,null,null,null];
}
if (topic == "tower/outlet0006/control") {
return [null,null,nMsg,null,null,null,null,null];
}
if (topic == "tower/outlet0007/control") {
return [null,nMsg,null,null,null,null,null,null];
}
if (topic == "tower/outlet0008/control") {
return [nMsg,null,null,null,null,null,null,null];
}
}
And now it looks like this.
So much better. In 12-2 I wondered if HASS would do MQTT JSON switches and the answer is no. Also Home Assistant can use Web Sockets, so you could use Node-Red for your Home Assistant automation. This is nice, because HASS's automation is clunky, limited, and you have to reload the config after each change.



Top Comments