7 Replies Latest reply on Apr 7, 2017 12:21 PM by njourdan RSS
    njourdan Newbie

    Advanced MQTT messages

    Hello, I used the tutorial from this site to install the MQTT Extension from the Marketplace to my installation. Everything works fine.


    For my purpose I need to transmit additional Information in the MQTT messages such as timestamp, senderID etc. I use JSON Format for the messages. Doing so I cant simply use the MQTT Extension to subscribe to a topic and directly save the messages to the ValueStream or whatever else. I would need a service that is called everytime a new message is received from the subscribed topics and that manages decoding the JSON etc.


    Is there a service that is called everytime a message is received? Or how can I create one?


    Thanks a lot

      • Re: Advanced MQTT messages
        jlittle Explorer

        If you have bound your topic to a property, you can use a Subscription to the DataChange event for that property to run services.  (Note, there may be a bug with DataChange events on Infotables; the infotable only triggers the event if a row is added or deleted, so a string would be better)


        With the subscription in place, you could then process your request, parse the JSON, etc. each time the topic changes.

          • Re: Advanced MQTT messages
            njourdan Newbie

            Hey Jeremy, thanks for the answer. I tried using a Subscription on the datachange event. I created a property "TemperatureMQTT" which subscribes to the MQTT Topic. I then created a Subscription for the datachange event of this property which should write the processed value via me.UpdatePropertyValues into another property "Temperature" that contains only the number values.


            I get the error:

            Error processing local event: Execution error in service script [WeatherStationMQTT TemperatureSubscriber.DataChange] : Wrapped com.thingworx.common.exceptions.InvalidRequestException: Not authorized for ServiceInvoke on UpdatePropertyValues in WeatherStationMQTT Cause: Not authorized for ServiceInvoke on UpdatePropertyValues in WeatherStationMQTT


            and nothing is written to the other property. Is it somehow not allowed to write to an other property than that the subscription is from? How can this be done?

              • Re: Advanced MQTT messages
                Ankit Gupta Ninja

                Hi Nicolas Jourdan,


                Create a Separate (Non MQTT) Thing for the number property and try to update the same.



                Ankit Gupta

                • Re: Advanced MQTT messages
                  jlittle Explorer

                  The "Not Authorized for Service Invoke" error indicates that the user performing the action does not have the necessary permission to run the service in question.  Easiest way to resolve the problem is to add a run time permission for the user / group that will be running these actions granting Service Execute on the entity in question.  You can do this either as a specific service, or as a overall grant to Service Execute.


                  On the WeatherStationMQTT thing:

                    - Select Permissions > Run Time

                    - To grant full Service Execute Permissions, type a group or user name in the topmost box (All properties, events, services), then select the green circle under Service Execute

                    - To only grant Service Execute permissions on the service in question, type in the name of the service in the bottom box (Property, Service, Event Overrides), then type in a user or group in the new box that appears for the selected service.


                  Occasionally you may have to grant permissions to the SuperUser or System user to work around this, though that is usually only in cases where a user context cannot be determined.

              • Re: Advanced MQTT messages
                stefanbod Apprentice

                Take a look at this youtube video, describing exactly what you want to reach:

                Minute 7:00 ++


                ThingWorx: MQTT Part1 Getting started - YouTube

                  • Re: Advanced MQTT messages
                    njourdan Newbie

                    Thanks for the video. This works fine. I also want to use the timestamp from the JSON for my valuestream in case the data transmission is not synchronous. For everyone that wants to achieve the same you can use this JS snippet in the subscription:


                    var decodedValue = eventData.newValue.value;

                    var createITParams = { infoTableName : "InfoTable", dataShapeName : "NamedVTQ" }; 

                    var properties = Resources["InfoTableFunctions"].CreateInfoTableFromDataShape(createITParams); 



                    var row = new Object();

                    row.name = decodedValue.topic;

                    row.value = decodedValue.data;

                    row.time = decodedValue.timestamp;




                    me.UpdatePropertyValues({ values: properties });