Samsung Windfree AC control with http binding

Hi guys,

after upgrade my OH from 2.5 to 3, my ac python script is not working anymore.
So i decided to change to the http binding, because it is now possible to control the ac with the samsung things rest api.

With postman everything works fine.
But i got stuck with the http binding, because i wasn´t able to send the right json body to the rest api.

There are two different URL´s, one for status, and one for control.
So i made two things which have their own items - one with status items, and one with control items.

The status rest api works well:

Thing http:url:Klima_WZ_Status "Klima WZ Status" [
	baseURL="https://api.smartthings.com/v1/devices/<my device>/status",
	headers="Authorization=Bearer <my bearer>", "Content-Type=application/json",
	refresh=60] {
		Channels:
			Type string : Betriebsart 		"Betriebsart" 		[stateTransformation="jsonpath:$.components.main.airConditionerMode.airConditionerMode.value", commandTransformation="jsonpath:$.components.main.airConditionerMode.airConditionerMode.value"]
			Type switch : Schalter 			"Ein/Aus" 			[stateTransformation="jsonpath:$.components.main.switch.switch.value", commandTransformation="jsonpath:$.components.main.switch.switch.value", onValue="on", offValue="off"]
			Type string : Ventilator 		"Ventilator_Modus" 	[stateTransformation="jsonpath:$.components.main.airConditionerFanMode.fanMode.value", commandTransformation="jsonpath:$.components.main.airConditionerFanMode.fanMode.value"]
			Type number : Temperatur_Ist 	"Ist Temperatur" 	[stateTransformation="jsonpath:$.components.main.temperatureMeasurement.temperature.value", commandTransformation="jsonpath:$.components.main.temperatureMeasurement.temperature.value"]
			Type number : Temperatur_Soll	"Soll Temperatur" 	[stateTransformation="jsonpath:$.components.main.thermostatCoolingSetpoint.coolingSetpoint.value", commandTransformation="jsonpath:$.components.main.thermostatCoolingSetpoint.coolingSetpoint.value"]

	}

With that state informations, my state itmes triggers - with rules - the corntrol items.
according to the logs, Rules, triggers and Items states also works as expected.

Here is my control thing,

Thing http:url:Klima_WZ_Control "Klima WZ Control" [
	baseURL="https://api.smartthings.com/v1/devices/<my device>/commands",
	headers="Authorization=<my bearer>", "Content-Type=application/json",
	commandMethod="POST",
	stateMethod="POST",
	refresh=86400] {
		Channels:
			Type string : Betriebsart_Control 		"Betriebsart Control" 		[commandTransformation="jsonpath:$.component.main.capability.airConditionerMode.command.value"]
			Type switch : Schalter_Control			"Ein/Aus Control" 			[commandTransformation="JINJA:{\"component\":\"main\", \"capability\":\"switch\",\"command\":\"{{value}}\"}", onValue="on", offValue="off"]
			Type string : Ventilator_Control 		"Ventilator_Modus Control" 	[commandTransformation="JINJA:{\"component\":\"main\", \"capability\":\"airConditionerFanMode\",\"command\":\"{{value}}\"}"]																						
			Type number : Temperatur_Soll_Control	"Soll Temperatur Control" 	[commandTransformation="JINJA:{\"component\":\"main\", \"capability\":\"thermostatCoolingSetpoint\",\"command\":\"{{value}}\"}"]  
	}

My Problem is, that i am not able to create the right json body.
i tried with jsonpath - that only works for incomming calls - if i understand it right.
And with JINJA, that should work for post commands, but that also doesn´t work.

in fact it should be quite smiple, but i didn´t find, which parser i had to use and how the string had to look like.

To turn on the AC, the Body in raw format, to the command url above, has to like like this:

[{"component":"main","capability":"switch","command":"on"}]

i hope you can give me some advices

BR
vaillan

The secret is in this part of the docs

What you are trying to do is insert your variable command in a fixed string, xxxx + command + yyyy. No-one cares if that makes it look ike JSON, it’s just string manipulation.
So you want to define commandExtension text, using special code %2$ to insert the command text

Ok, that helps but, i still don´t get it, with the doc´s

only the command is changing in this this json string:

[{"component":"main","capability":"switch","command":"on"}]

[{"component":"main","capability":"switch","command":"off"}]

how does the commandExtension has to look like?

What i actully wanted to do, is to set my item e.g to on, and the http binding post the whole string to the url with the on body above.

the curl looks like this:

curl --location --request POST 'https://api.smartthings.com/v1/devices/<my device>/commands' \
--header 'Authorization: <my bearer>\
--header 'Content-Type: text/plain' \
--data-raw '[{"component":"main","capability":"switch","command":"off"}]'

But i don´t want to work with such long command string in my rules.
Hope get it to work with the http binding.

BR
Vaillan

So you want the on / off part to be substituted with the variable command, so basically
[{"component":"main","capability":"switch","command":"%2$"}]
except that I think you’ll have to encode the quotes as they are part of your string, not the delimters.
[{/"component/":/"main/",/"capability/":/"switch/",/"command/":/"%2$/"}]

i tried both, but this is not working.
seems that the colons are a problem in this case.
Didn´t find another example with colons, so i´m stuck.

tried this:

[commandExtension=[{/"component/":/"main/",/"capability/":/"switch/",/"command/":/"%2$/"}]]

getting this error:

2022-11-05 17:17:50.345 [WARN ] [el.core.internal.ModelRepositoryImpl] - Configuration model 'Klima_Control.things' has errors, therefore ignoring it: [9,76]: no viable alternative at input '['
[9,101]: mismatched input '/' expecting RULE_ID
[9,127]: mismatched input '/' expecting RULE_ID
[9,146]: extraneous input '}' expecting ']'
[9,148]: mismatched input ']' expecting '}'
[10,39]: mismatched input '"Ventilator_Modus Control"' expecting ':'
[11,4]: missing EOF at 'Type'

trying this:

[commandExtension=[{"component":"main","capability":"switch","command":"%2$"}]

getting this error:

2022-11-05 17:24:08.672 [WARN ] [el.core.internal.ModelRepositoryImpl] - Configuration model 'Klima_Control.things' has errors, therefore ignoring it: [9,76]: no viable alternative at input '['
[9,109]: mismatched input ':' expecting ']'
[9,135]: missing EOF at ']'

BR
Vaillan

should be \ instead of / to escape the characters …
assume that were typos

1 Like

Good point.
Also missing basic string delimiters
Not

commandExtension=[{/"component ...

but

commandExtension="[{\"component ... "
1 Like

With the right format, i´m not getting config errors anymore.
But it´s still noch working.
I´m posting the right values, but the string seems to be not right.

I have set the http binding on debug mode in karaf, but only getting this in openhab log:

2022-11-06 09:05:16.574 [WARN ] [nding.http.internal.HttpThingHandler] - Creating request for 'https://api.smartthings.com/v1/devices/xxxx/commands/[{"component":"main","capability":"switch","command":"%2$"}]' failed: Conversion = '2'
2022-11-06 09:05:21.574 [WARN ] [nding.http.internal.HttpThingHandler] - Creating request for 'https://api.smartthings.com/v1/devices/xxxxx/commands/[{"component":"main","capability":"airConditionerMode","command":"setAirConditionerMode","arguments":"%2$"}]' failed: Conversion = '2'

Otherwise i can make a wireshark trace.

this is my thing now:

Thing http:url:Klima_WZ_Control "Klima WZ Control" [
	baseURL="https://api.smartthings.com/v1/devices/<my device>/commands",
	headers="Authorization=<my Bearer>", 
    Content-Type="application/json",
	commandMethod="POST",
	stateMethod="POST",
	refresh=86400] {
		Channels:
			Type string : Betriebsart_Control 		"Betriebsart Control" 		[commandExtension="[{\"component\":\"main\",\"capability\":\"airConditionerMode\",\"command\":\"setAirConditionerMode\",\"arguments\":\"%2$\"}]"]	
			Type string : Schalter_Control			"Ein/Aus Control" 			[commandExtension="[{\"component\":\"main\",\"capability\":\"switch\",\"command\":\"%2$\"}]"]
			Type string : Ventilator_Control 		"Ventilator_Modus Control" 	[commandExtension="[{\"component\":\"main\",\"capability\":\"airConditionerFanMode\",\"command\":\"setFanMode\",\"arguments\":\"%2$\"}]"]																			
			Type string : Temperatur_Soll_Control	"Soll Temperatur Control" 	[commandExtension="[{\"component\":\"main\",\"capability\":\"thermostatCoolingSetpoint\",\"command\":\"setCoolingSetpoint\",\"arguments\":\"%2$\"}]"]
	}

do you have still an advice?
if that doesn´t work, i have to work with maps, but this is much better.

BR
Vaillan