Using multiple transforms

I’m using ESPEasy on some Ecoplugs and controlling them via MQTT. I have some rules set up to get the state of the plugs when OH starts. I have an extra item for each plug that holds the returned status, as well as several lines of rules. I could eliminate all but a simple publish command to each plug at startup, except for the way that ESPEasy returns the status.

The status is returned as a 1 or 0 in a JSON array.

/ol4/status {
"log": "GPIO 15 Set to 0",
"plugin": 1,
"pin": 15,
"mode": "output",
"state": 0

Obviously I can extract the 1 or 0. And I can map them to the expected ON or OFF. But is it possible to use both transforms?

I realize the simple answer would be to use persistence, but I try to make sure my devices still function whether or not OH is available so I would like any changes that may have happened to be reflected.

Try a JS transform to extract the JSON value and convert it to ON/OFF using JavaScript.

Thank you for that hint. I think I managed to get something working. Had some odd behavior pop up with a completely unrelated map file that has been working fine… but I think I still have some hair left.

I even almost understand what I wrote. :laughing: I know I learn better from examples, so here’s what I came up with in case it helps anyone in the future.

(function(i){
	var parsed = JSON.parse(i);
	return parsed.state === 1 ? "ON" : "OFF";
})(input);
1 Like