[SOLVED] MQTT 2 Binding: Publish action with JSR223 Javascript

Tags: #<Tag:0x00007f386f6d1f38> #<Tag:0x00007f386f6d1df8> #<Tag:0x00007f386f6d1cb8>

Hi,

is there an example for using the publish action with the new MQTT 2 binding?
I am using javascript and the jsr223.

On the page there is the following example for DSL:
val actions = getActions(“mqtt”,“mqtt:systemBroker:embedded-mqtt-broker”)
actions.publishMQTT(“test/system/started”,“true”)

How does it look for JS?

Thanks in advance!
HFM

Here are some action examples for JS, and here are some examples for Jython. I haven’t specifically tried using an MQTT action, but let me know if you have trouble and I’ll make a working example.

Hi @5iver,

I used your links and used this function:

JSRule({
	name: me+" getActions",
	description: "TEST L:"+__LINE__,
	triggers: [ 
		//TimerTrigger("0/15 * * * * ?")
	],
	execute: function( module, input){
		logInfo("################ "+me+" Line: "+__LINE__+"  #################");	
		var a = getActions();
		var aList = getActionList();
		print("getActionList: 	" + JSON.stringify( aList ));
		
		for(var i=0; i<aList.length; i++){
			logInfo("################ "+me+" Line: "+__LINE__+"  #################|"+aList[i]);
			logInfo("################ "+me+" Line: "+__LINE__+"  #################|"+a[aList[i]]);
			if(aList[i] == "XMPP")logInfo("### "+me+" Line: "+__LINE__+"  ###|"+a[aList[i]].getActionClass().static.sendXMPP("helmutl@lewi-cleantech.net","jkkkk")+"|###");
		}
		//
		//var XMP = ScriptServiceUtil.actionServices[6].getActionClass();//.getConstructor().newInstance();
		//logInfo("################ "+me+" Line: "+__LINE__+"  #################|"+XMP.static.sendXMPP("helmutl@lewi-cleantech.net","vvvvvvvvvvv"));
		//logInfo("################ "+me+" Line: "+__LINE__+"  #################|"+getAction("XMPP").static.sendXMPP("helmutl@lewi-cleantech.net","jkkkk")+"|##########");
		
	}
});

This is the result:

2019-03-31 19:44:30.566 [INFO ] [lesupport.internal.shared.SimpleRule] - |################  Line: 1306  #################| 
2019-03-31 19:44:30.571 [INFO ] [lesupport.internal.shared.SimpleRule] - |actions = [object Object]| 
2019-03-31 19:44:30.575 [INFO ] [lesupport.internal.shared.SimpleRule] - |actionList = NotificationAction,Transformation,Audio,PersistenceExtensions,Voice,Things| 
2019-03-31 19:44:30.588 [INFO ] [lesupport.internal.shared.SimpleRule] - |################  Line: 1312  #################|NotificationAction| 
2019-03-31 19:44:30.604 [INFO ] [lesupport.internal.shared.SimpleRule] - |################  Line: 1313  #################|org.openhab.io.openhabcloud.internal.CloudService@1db8373| 
2019-03-31 19:44:30.609 [INFO ] [lesupport.internal.shared.SimpleRule] - |################  Line: 1312  #################|Transformation| 
2019-03-31 19:44:30.613 [INFO ] [lesupport.internal.shared.SimpleRule] - |################  Line: 1313  #################|org.eclipse.smarthome.model.script.internal.engine.action.TransformationActionService@1c5e0a8| 
2019-03-31 19:44:30.617 [INFO ] [lesupport.internal.shared.SimpleRule] - |################  Line: 1312  #################|Audio| 
2019-03-31 19:44:30.621 [INFO ] [lesupport.internal.shared.SimpleRule] - |################  Line: 1313  #################|org.eclipse.smarthome.model.script.internal.engine.action.AudioActionService@68b0a0| 
2019-03-31 19:44:30.626 [INFO ] [lesupport.internal.shared.SimpleRule] - |################  Line: 1312  #################|PersistenceExtensions| 
2019-03-31 19:44:30.631 [INFO ] [lesupport.internal.shared.SimpleRule] - |################  Line: 1313  #################|org.eclipse.smarthome.model.script.internal.engine.action.PersistenceActionService@1b63e7c| 
2019-03-31 19:44:30.636 [INFO ] [lesupport.internal.shared.SimpleRule] - |################  Line: 1312  #################|Voice| 
2019-03-31 19:44:30.640 [INFO ] [lesupport.internal.shared.SimpleRule] - |################  Line: 1313  #################|org.eclipse.smarthome.model.script.internal.engine.action.VoiceActionService@11381af| 
2019-03-31 19:44:30.645 [INFO ] [lesupport.internal.shared.SimpleRule] - |################  Line: 1312  #################|Things| 
2019-03-31 19:44:30.649 [INFO ] [lesupport.internal.shared.SimpleRule] - |################  Line: 1313  #################|org.eclipse.smarthome.model.script.internal.engine.action.ThingActionService@1c9e545|

So, I do not see any MQTT action. I think this is because there is no action defined with MQTT2 binding. But how can I use the publish action?

Any help very appreciated!
And with thanks in advance!
HFM

Read about code fences… the easiest is 3 ticks before and after the code or log.

I’ll take a look later this afternoon.


I thought I had figured this out, but ended up having to look into the code to figure it out. This worked for me, and is the same format in Jython, Groovy, etc., minus the semicolon.

actions.get("mqtt", "mqtt:systemBroker:embedded-mqtt-broker").publishMQTT("test/system/started", "true");
1 Like

Thank you, @5iver!

Will test it in the evening.

This should be part of the documentation. Who should be contacted?

Have a good start into the week!
HFM

I need to update the docs about something else, so I’ll put this in too.

1 Like

Hi @5iver,

thanks for the hint with the ticks, changed the formatting.
And thank you for the code example!!!
Everything works now! :wink:

This solved my issue.

Have a great week!
HFM

1 Like