Hi,
I have physical device with two A/C relays which can be controlled through MQTT (the device runs Espurna firmware).
That device gives status updates with a JSON payload:
'{"relay/1":"1","relay/0":"0","vcc":"3145","time":"2020-05-04 23:38:31","id":444192}'
The relay/x
part indicates if that relay is on (1) or off (0). Status updates may contain only relay/0
, only relay/1
or both or none of them (depends when a relay had its last activity).
I have set up my switches as things this:
Type switch : switch05_0 "ESP Switch 05 Relay 0" [ stateTopic="wohnung/switch5/data", commandTopic="wohnung/switch5/relay/0/set", transformationPattern="JS:EspurnaRelay0.js", allowedStates="ON,OFF" ]
Type switch : switch05_1 "ESP Switch 05 Relay 1" [ stateTopic="wohnung/switch5/data", commandTopic="wohnung/switch5/relay/1/set", transformationPattern="JS:EspurnaRelay1.js", allowedStates="ON,OFF" ]
The JavaScript transform allows me to see if the JSON contains the relay/0
respectively relay/1
and to react on that.
(function(i) {
var jsonElementToFind = "relay/0";
var returnIfNotFound = "UNDEF";
var arrOffOn = ["OFF", "ON"];
var jsonData = JSON.parse(i);
var hits = jsonData[jsonElementToFind];
if (hits != null)
return arrOffOn[hits[0]];
//else
//return returnIfNotFound;
})(input)
My problem: I do not know how to handle the case, that a JSON was received, but does not contain information which would allow me to update the switch state. With the JS above, I tried to return "UNDEF"
, ""
(empty string), nothing - but all of that results in an error:
2020-05-05 10:03:32.894 [WARN ] [ab.binding.mqtt.generic.ChannelState] - Command 'null' not supported by type 'OnOffValue': No enum constant org.eclipse.smarthome.core.library.types.OnOffType.null