[OH3] HTTP binding equivalent to MQTT's formatBeforePublish?

I’m messing around with functionality.

One of my devices requires POSTing a JSON string.

I can do this in the HTTP Binding using a MAP transform, but this is a little tedious (never mind requiring another file, and I don’t think works for dynamic stuff, such as colours).

Am I missing an obvious (combination of) feature(s) in the HTTP Binding which would allow me to do something like the MQTT Binding’s formatBeforePublish feature?


My current configuration:

Things

Thing http:url:wled "WLED " [
	baseURL = "http://192.168.1.126/json",
	refresh = "10",
	ignoreSSLErrors = "true",
	commandMethod = "POST",
	contentType= "application/json"
]
{
	Channels:
		Type switch : on "Power" [
			mode = "READWRITE",
			stateTransformation = "JSONPATH:$.state.on",
			commandTransformation = "MAP:wledonoff.map",
			onValue = "true",
			offValue = "false"
		]
}

MAP

false={"on":false}
true={"on":true}

What would be awesome is if I could get rid of the MAP transformation (and file), and do this directly within the Thing. If I pretend that formatBeforePublish is available in the binding, my Thing would look like:

Thing http:url:wled "WLED " [
	baseURL = "http://192.168.1.126/json",
	refresh = "10",
	ignoreSSLErrors = "true",
	commandMethod = "POST",
	contentType= "application/json"
]
{
	Channels:
		Type switch : on "Power" [
			mode = "READWRITE",
			stateTransformation = "JSONPATH:$.state.on",
			formatBeforePublish = "{\"on\":%s}",
			onValue = "true",
			offValue = "false"
		]
}

If this really is for WLED have you not tried the WLED binding?

I’m using WLED as an example because it has multiple control interfaces allowing me to explore control options. One is of course the Binding, another is MQTT, and yet another is the JSON API.

I’m fairly new to the HTTP Binding, so am exploring the possibilities of its use with the JSON API. With WLEDs’ other interfaces I can easily check that my states and commands are doing what they’re supposed to.

So I guess in short this thread isn’t about WLED itself, but more a feature of the HTTP Binding that would be great to have - if it doesn’t already exist…

You do know that other transform types are available? The JS javascript transform allows virtually unlimited manipulation of your single input command to whatever single output string you desire.

The features in the MQTT binding are really only shortcuts to get similar behaviours.

I don’t know but you may be able to

			onValue = `{\"on\":true}`,
			offValue = `{\"on\":false}`

Sure, but that’s another file again, though it does take care of dynamic content.

But very convenient, hence this thread!

onValue and offValue are transforms for both inbound and outbound information. The inbound message is a much more complete JSON string, so you could maybe extract the exact portion using REGEX instead of JSONPATH. Might be a work around for static stuff, though still lacks the dynamic feature.

I agree that would be nice to have. Please enter an issue in GitHub. Thanks.

2 Likes