Skip to main content
Version: Next

Receiving and Reacting to Messages

Converters provide a UnityAction acting on other datatypes than string. When added through the Tools menu, a regular Subscriber component will be attached to the selected gameobject along with a universal converter component configured for the selected datatype. Add desired actions in the converter component, rather than the subscriber.

caution

The UniversalConverter component will replace all prior converters. Therefore these converter components are marked 'deprecated' already and will be removed in the next major version. See Update for help in replacing existing components.

dynamic vs static

If you want to use values from the MQTT message in UnityActions, make sure to select members from the dynamic section of the action (see image for an example). In the example, if you select intensity in the dynamic section it will take the value from the MQTT subscriber. If you select float intensity in the static section, you can define a fixed value to be used, no matter what the message payload is.

If you need to use strings from MQTT messages just use the regular Subscriber component as described in core components. However, sometimes it is desirable to interpret payloads as other datatypes or read more complex JSON payloads. For the former, SmoothMQTT brings a UniversalConverter component, the latter is explained in JSON Subscriber.

UniversalConverter

Type: float

Add actions that set floats in gameobjects. For example light.intensity, audioSource.volume, etc.

Type: int

Add actions that set integers in gameobjects. Potentially used to have objects switch layers and such.

Author's remark: To be perfectly honest, I never used it, it just felt mandatory to implement it. If you find a use-case, please contact me and I will add it to the manual.

Type: bool

Add actions that set boolean values. For example activating / deactivating Components and GameObjects, toggling gravity, or pretty much anything else that shows up as checkbox in the inspector.

Type: Color

Add actions that react to color payloads and set color tints, light color, or material color (Caution: Changing assets like materials in Editor is permanent)

Type: Quaternion

Add actions that set a Quaternion. This is mainly used to set rotations on transforms.

Type: Vector3

Add actions that set Vector3s. This is extremely versatile and can be used to change position, scale, rigidbody velocity, navmesh agent destination, and many more things.

Conditional Subscriber

These components will provide you with an easy way for action branching. All conditional subscribers will provide you with a comparison option (more on that in the component specific part) and two UnityActions.

  1. True Action (executed whenever the condition is met)
  2. False Action (executed when the condition is not met) It is totally fine to use only one of the two.

Compare String (ConditionalSubscriber)

This component compares a given string with incoming MQTT messages and Invokes all the UnityActions in TrueAction if it matches and those in FalseAction if it doesn't.

Add menu: Tools->SmoothMQTT->Receive->Compare String Subscriber

Compare float (ConditionalFloatSubscriber)

Compare incoming MQTT messages interpreted as float with a value or even a range. In the field comparison operator you can select either typical comparators like: "=, !=, <, <=, >, >=" to compare with the value in CompareValue. WithinRange and OutsideRange allows to compare with the range defined by CompareValue and AdditionalRangeCompareValue. (Attention: a period should be used as decimal point)

Add menu: Tools->SmoothMQTT->Receive->Compare Float Subscriber

JsonSubscriber

This component allows you to access single values from JSON payloads. Similar to a regular subscriber component, you can setup the topic to listen on. In the section Json specific parameters in the inspector, SmoothMQTT is expecting a path to value. This is a /-separated string of keys (value names) and integers (index for list). But let's take a step back and look at a proper example

{
"person": {
"name": "Simon",
"age": 47,
"isAgeTrue": "False",
"unityAssets": [
"SmoothMQTT"
]
}
}

In this JSON payload you can address the keys (value names) by their names. For example a PathToValue of person/age yields 47, while person/name yields Simon, etc. If you want to access list elements (identified by square brackets [ ] in JSON) you can just use the integer index (starting at 0 for the first element). So person/unityAssets/0 would yield SmoothMQTT

payload complexity

When working with lists, the complexity is limited to a single list index in the path. Complex nesting is not supported right away, because the interpretation works on regular expressions, to avoid third party dependencies. If you decide to install NewtonSoft Json, though, you can click the "Look for NewtonSoft Json" button in the inspector after installation. After a few moments it should detect the package and support more complex JSON payloads.

Like the regular Subscriber component, this component also Invokes the regular action with the complete JSON payload. If you want to continue working with the filtered value, add your listeners to "Do On Message" instead.

Finally the JsonSubscriber allows you to poll for values right away (as most off-the-shelf IoT devices have that kind of mechanism). Put in the topic and payload you have to send for polling and set the interval in seconds. If you don't want to use polling, just leave the topic empty. A more detailed description on how to use it to read temperatures from Tasmota devices will follow shortly in the Appendix of the online version of this manual: https://smoothmqtt.schliesky.com

Check out this screenshot for a quick example on how to extract the age of the above example JSON payload and use it to set the font size of a text field.

If you're interested in controlling sensors without writing code, checkout Appendix C - Tasmota

Updating old components

While great care is taken that the update process is smooth (as in SmoothMQTT), usually not all edge-cases can be considered at programming time. So please make a backup of your project before you proceed.

danger

Make a backup of your project, before you proceed!

As soon as your backup is done, click Tools->SmoothMQTT->Update Converter Components. This will replace all deprecated converter components in the currently active scene. If a gameobject happens to also be linked to a prefab, the prefab will be updated, as well. Please double check the action listeners after update.