MQTT send command

Hello,

how is it possible to send this command via MQTT?

{"message":"{\"systemcode\":1,\"programcode\":4,\"%s"\:1}","protocol":"rsl366"}

depend on when you wanna send it …
see here

1 Like

In Rules you can do it this way:

// Connect your MQTT Broker
val mqttActions = getActions("mqtt", "mqtt:broker:77e28eff")

// Publish your message to the MQTT Broker
mqttActions.publishMQTT("path/to/topic", "{\"message\":\"{\"systemcode\":1,\"programcode\":4,\"%s"\:1}\",\"protocol\":\"rsl366\"}")
2 Likes

thank you but it didn’t work :frowning:

here are my files:
*.rule

Thing topic RF "RF"{
    Channels:
        Type switch : Subwooferr "Subwooferrrrr" [ commandTopic="home/OpenMQTTGateway/commands/MQTTtoPilight", on="on", off="off" ]
}

*.item

Switch Subwooferr "Subwoofer NEU" {channel="mqtt:topic:pibroker:RF:Subwooferr"}

*.rule

rule "MQTT Test"
	when
		Item Subwooferr received update
	then
	logInfo("FILE", "SUB ACTIVE")
	val mqttActions = getActions("mqtt","mqtt:topic:pibroker:RF:Subwooferr")
		if(Subwooferr.state == ON){
			logInfo("FILE", "SUB ONNNNN")
		mqttActions.publishMQTT("home/OpenMQTTGateway/commands/MQTTtoPilight", "{\"message\":\"{\"systemcode\":1,\"programcode\":4,\"%s\":1}\",\"protocol\":\"rsl366\"}")
		}else{
			logInfo("FILE", "SUB OFFFFF"
		}
end
22:24:09.321 [ERROR] [untime.internal.engine.RuleEngineImpl] - Rule 'MQTT Test': Instance is not an MQTTActions class.

have to be on broker not on mqtt topic

thank you.

i change my rule to

rule "MQTT Test"
	when
		Item test received update
	then
	logInfo("FILE", "SUB ACTIVE")
	val mqttActions = getActions("mqtt","mqtt:topic:pibroker")
		if(test.state == ON){
			logInfo("FILE", "SUB ONNNNN")
			// Publish your message to the MQTT Broker
			mqttActions.publishMQTT("home/OpenMQTTGateway/commands/MQTTtoPilight", "{\"message\":\"{\"systemcode\":1,\"programcode\":4,\"%s\":1}\",\"protocol\":\"rsl366\"}")
		}else{
			logInfo("FILE", "SUB OFFFFF")
		}
end

but i got this warning again:

22:46:59.501 [ERROR] [untime.internal.engine.RuleEngineImpl] - Rule 'MQTT Test': Instance is not an MQTTActions class.

hardly believe that this is your broker… should be something like
mqtt:broker:name or mqtt:broker:id (if you made it by generic paperUI)

this is my last change:

rule "MQTT Test"
	when
		Item test received update
	then
	logInfo("FILE", "SUB ACTIVE")
	val mqttActions = getActions("mqtt","mqtt:broker:pibroker")
	if (test.state == ON)
	{
		mqttActions.publishMQTT("home/OpenMQTTGateway/commands/MQTTtoPilight", "{\"message\":\"{\"systemcode\":1,\"programcode\":4,\"on\":1}\",\"protocol\":\"rsl366\"}")
	}
	else
	{
		mqttActions.publishMQTT("home/OpenMQTTGateway/commands/MQTTtoPilight", "{\"message\":\"{\"systemcode\":1,\"programcode\":4,\"off\":1}\",\"protocol\":\"rsl366\"}")
	}
end

here is my last Problem.

how can i send

 {"message":"{\"systemcode\":1,\"programcode\":4,\"%s"\:1}","protocol":"rsl366"}

and not

{"message":"{"systemcode":1,"programcode":4,"off":1}","protocol":"rsl366"}

so i will send \" not "

{"message":"{\\"systemcode\\":1,\\"programcode\\":4,\\"%s"\\:1}","protocol":"rsl366"}

than i got this warning:

23:41:18.604 [WARN ] [del.core.internal.ModelRepositoryImpl] - Configuration model 'test.rules' has errors, therefore ignoring it: [30,93]: missing ')' at 'systemcode'
[30,112]: no viable alternative at input 'programcode'
[30,132]: no viable alternative at input 'on'
[30,168]: extraneous input ')' expecting '}'

I think you’ll need to use both \\ and \" to send \" , like so \\\"

But why on earth do you want to send messed up JSON?

Thank you.

I will try it.

Because the software of “openmqtt”, need this kind of command.

double escape? not sure, usually you need to escape once and then there goes your chars. But frankly I dunno, sending escaping chars to MQTT which is purely string based sounds to me bit weird.

that example is for cmndline mosquito_pub, have you tried to send it as whole text without escape chars?

not sure what is equivalent of Json object in OH, but when working with ESP’s this kind of code makes json publish into the mqtt

        StaticJsonBuffer<300> jsonBuffer;
        JsonObject& root = jsonBuffer.createObject();

        root["ver"] = swVersion;
        root["Uptime"] = String(uptime::getDays()) + "T" + String(uptime::getHours()) + ":" + String(uptime::getMinutes()) + ":" + String(uptime::getSeconds());
        root["Temperature"] = decimals(iaqSensor.temperature,1);
        root["Humidity"] = decimals(iaqSensor.rawHumidity,1);
        root["Pressure"] = decimals((iaqSensor.pressure / 1e2),1);
        root["Gas"] = decimals((iaqSensor.gasResistance / 1e3),2);
        root["IAQ"] = decimals(iaqSensor.iaqEstimate,1);
        root["sIAQ"] = decimals(iaqSensor.staticIaq,1);
        root["IAQ_Accuracy"] = iaqSensor.iaqAccuracy;
        root["CO2e"] = decimals(iaqSensor.co2Equivalent,3);
        root["bVOC"] = decimals(iaqSensor.breathVocEquivalent,3);
        root["rawTemperature"] = decimals(iaqSensor.rawTemperature,1);
        root["rawHumidity"] = decimals(iaqSensor.humidity,1);
        
        // use it as JSON
        char data[300];
        root.printTo(data, root.measureLength() + 1);

        // mqtt report
        client.publish(mqtt_topic, data);

which then produce

home/climate/livingroom {"ver":"0.6","Uptime":"3T11:52:2","Temperature":21.2,"Humidity":45.9,"Pressure":973.4,"Gas":404.47,"IAQ":248.4,"sIAQ":155.5,"IAQ_Accuracy":2,"CO2e":1555.14,"bVOC":3.596,"rawTemperature":21.3,"rawHumidity":46.2}

As you can see, there are no escape characters at all

The “double escape” is really escape-slash escape-quotemark, \\\", because he asked how to send \" in the MQTT payload.

I’m fully with you, it’s a stupid thing to want to do.

ah you probably right I thought it’s escape-slash-quote so after escape all toxic chars are escaped, but you are right that every single one needs to be escaped separately

Thank you for your support.

I will test it and give feedback

thank you four your tipp.

this solution works.

i got this warning:

18:43:19.628 [WARN ] [hab.binding.mqtt.generic.ChannelState] - Command '{"message":"{\"systemcode\":1,\"programcode\":4,\"on\":1}","protocol":"rsl366"}' not supported by type 'OnOffValue': No enum constant org.eclipse.smarthome.core.library.types.OnOffType.{"message":"{\"systemcode\":1,\"programcode\":4,\"on\":1}","protocol":"rsl366"}

do you also know how to change the sich status by receiving data from a RF remote?

most likely your command topic is same with state topic and OH can’t interpret this kind of message to any value ON or OFF

separate your command and status topics
for example like so:

Type switch : eazy          "EazyPod"           [ stateTopic="garden/service/filters/eazy/stat",    commandTopic="garden/service/filters/eazy/cmd",    on="ON", off="OFF"]

after i update to openhab 2.5.2 i got this error Message

Rule 'RF Steckerleiste': Instance is not an MQTTActions class.

here is my rule:

rule "RF Steckerleiste"
	when
		Item Steckerleiste received update
	then
		if (Steckerleiste.state == ON) {
			mqttActions.publishMQTT("home/OpenMQTTGateway/commands/MQTTtoPilight", "{\"message\":\"{\\\"systemcode\\\":1,\\\"programcode\\\":1,\\\"on\\\":1}\",\"protocol\":\"rsl366\"}")
		}
		else {
			mqttActions.publishMQTT("home/OpenMQTTGateway/commands/MQTTtoPilight", "{\"message\":\"{\\\"systemcode\\\":1,\\\"programcode\\\":1,\\\"off\\\":1}\",\"protocol\":\"rsl366\"}")
		}
end

Where did you define that object?