Raspberry Pi 3B+ running OpenHabian current version
I have some Sonoff devices configured with Tasmato to both issue ON/OFF commands and consume status messages correctly. Here’s a line from my Items file.
This all works fine except when Openhab goes out of sync with the devices (reboot etc) of the device changes state without sending a power state change message. The next command to change the state does sort the problem but my wife gets a little freaked when I’m away and I toggle the lights via OH App to be sure they’re off!.
The question - Can I parse the regular telemetry MQTT messages like these
Vincent I got that working except I didn’t 100% describe what I needed
I actually need the opposite of the result being returned. So if POWER3 is “ON” I actually need to translate this as “OFF”. You can see from this more complete example that I’m flipping the “cmnd” and “stat” MQTT messages.
I spent this evening trying to see if this was possible with JSONPATH and have concluded it is not. I think that leaves me with JavaScript I’ve tried to craft a script and come up with this. I’ve tested it using JavaScript Tester online and it seems to do what I need:
function x (json,i) {
var xx = "x";
xx = json[i];
if(xx == "OFF"){
xx ="ON"
} else if (xx == "ON") {
xx = "OFF"
}
return xx;
}
var json = {"Time":"2018-12-10T23:29:02","Uptime":"1T01:30:21","Vcc":3.439,"POWER1":"ON","POWER2":"ON","POWER3":"ON","POWER4":"ON","Wifi":{"AP":1,"SSId":"9E63","RSSI":58,"APMac":"90:EF:68:48:FF:DF"}}
x(json,"POWER3")
BTW - That’s my first JS ever. Awesome!
Questions
Am I on the right track?
Can I use JS transform with 2 parameters or will I need to do a script in transformation directory for each of POWER1,2,3 etc.?
How would I call this from the items file?
(function(powerJson) {
var data = JSON.parse(powerJson);
var power = data.POWER1;
if (power == 'ON') {
power = 'OFF';
} else {
power = 'ON';
}
return power;
})(input)
Apologies for the no response to this. I’ve been travelling for work since Monday. Vincent Thanks for all this help. I’m going to try it this week once I clear the job the “Boss” has stored up for me:)
Hii
You can also use https://onlinexmltools.org/ for online xml converter like XML to JSON,YAML,CSV,BASE64,TSV,PlainText
XML to Minfiy XML to Beautify.