MQTT Topic help needed

Hi all,
I am on OH4.3.5 and have a challenge defining a mqtt channel.
I am using MQTT to manage an external device (a pellet oven) and want to look up the state of a switch in a MQTT topic where the on-state will be described with the value 1 in the datafield “start” and off will be defined by the value 1 in the “stop” datafield. I didnt build the MQTT provider, so I have to live with it as is.

I understand I need to describe a “transformationPattern” for the stateTopic, but simply cannot figure out how to deal with the varying states depending on two different JSON values in the string.

Can anyone suggest how my transformationPattern for the stateTopic should look like?
I have a similar case for the commandTopic - but think I have that one solved in the example…

Things:

			Type switch : toggle		[
											stateTopic = "test/settings/misc",
											transformationPattern = ??????,
											commandTopic="test/set",
											on="misc.start", off="misc.stop",
											formatBeforePublish = "{\"path\": \"%s\", \"value\": \"1\" }"
										]

MQTT Topic of "test/settings/misc:
{"address_registered": 1.0, "clock_changed": 0.0, "clock_date": 1.0, "clock_hour": 0.0, "clock_minute": 0.0, "clock_month": 1.0, "clock_second": 0.0, "clock_year": 15.0, "commit_picture": 0.0, "dl_version": 0.0, "dummy": 1.0, "expansion_module": 0.0, "fahrenheit": 0.0, "model_number": 0.0, "push_picture": 0.0, "push_prog_size": 0.0, "push_version": 0.0, "removesensor": 1.0, "reset_alarm": 0.0, "rsa_key": 0.0, "shaft_sensor": 0.0, "smoke_sensor": 1.0, "start": 0.0, "stop": 0.0, "test": 0.0, "url": 1.0, "wifi_network_status": 2.0, "xtea_key": 0.0}

You’ll need to use a script transform, parse the json and pull and compare the two fields to determine the on/off status.

I’n JS it would look something like:

(function(input){
  var parsed. = JSON.parse(input);
  if(parsed.start == 1) return "ON";
  if(parsed.stop == 1) return "OFF";
  return "UNDEF";
})(input)
1 Like

Thanks Rich - I got it working using your example as inspiration. When I am done with the integration, I will publish a demo/example page with all items, things, transformation etc. described.