MQTT - Node Red - Smartthings

I have a MQTT server with control of my pool equipment. I would like to control the equipment using smartthings virtual switches via node-red and MQTT. I have node-red-contrib-smartthings installed. MQTT is working well and i can control a smartthings virtual switch in node-red using a function node. I cannot figure out how to connect MQTT to the function node to in turn control the switch.

Here is my current flow:

Here is the function code to turn off the virtual switch using an inject node
image

How can i connect MQTT and the virtual switch together? I am very new to this so thank you in advance.

Although i do not have Smartthings - my understading is that you use the Smartthings integration you have installed and send the commands to it - it will be attached to your Smartthings hub and MQTT does not need to get involved. i.e. the communication between NR and the Smartthings hub is not handled over MQTT but rather through the contrib node

Just did some searching on the Smartthings forum and it appears that yes the contrib node provides a webhook between NR and ST.

Craig

Try it like that ...

zzz

It's easy when you start using Node-red to write too much code in function nodes.

Your function nodes to set topic and payload are unnecessary.

You can achieve the same thing in your inject nodes:
Untitled 1

And if the message is initiated from somewhere else such as MQTT in, you can do the same with a change node.

@craigcurtin Thanks. You're correct, i dont need MQTT to control smartthings but it is needed to control the pool equipment. my goal is to use a smartthings virtual switch to control a switch on my pool equipment and update status of that switch. thus the need to connect the mqtt node to the smartthings on/off node. I'm using aqualinkd (GitHub - sfeakes/AqualinkD: Daemon to control Jandy Aqualink RS pool equipment from any home automation hub (Alexa, Homekit & Siri, Home Assistant, smartthings, domoticz etc) or web browser.) for the pool equipment on the same rpi as my mqtt and node-red server

Giamma I have tried this combination or variations of it, but the mqtt output is not recognized by the on/off device so no response. Somehow need to convert the mqtt output to the required input for the smartthings node outlined in the function node I shared above

@jbudd Thanks for this info. I tested this and works great for controlling the on/off node with an inject node without the need for a function node. unfortunately I still need to connect to mqtt.

Here is the flow I am looking for but unsure of how to make it work

OK do you understand MQTT ??

You need a MQTT broker setup - your NR would talk to this broker and send commands to it.

Your physical switches on the pool equipment also need to attach to this MQTT broker and listen for messages sent by the MQTT out node on NR.

Have you got all of this setup ?

If so can you show us the MQTT logs (presumably you would be running Mosquitto for this) so we can see what is happening at that level

Craig

Have you seen this thread on

Also i am not quite understanding what you want to do - lets use an easy example

  1. Want to turn the pool pump on
  2. Go to smartthings and hit a button etc
  3. This send a message to NR through the ST Webhook integration
  4. You then want to take this message and send it through MQTT to the Aqualink Daemon that tells it to turn on the Pump
  5. You then want to query the status through NR to check if the Pool pump is on and send that info back to ST to update the Virtual switch on the ST dashboard ?

Is that correct ?

Craig

If i am correct in what i have stated above then you need the following

  1. A debug of the Virtual Switch on ST sending to the Webhook in NR - so we can see what it sends when it sends a turn on and a turn off

  2. A list of commands that the Aqualink MQTT integration supports for your devices - at the very least a command for on and off and to query the status

  3. Acceptable commands to a ST Virtual switch to have it change its state to reference the real world i.e. what do we send it to have ti change on the dashboard from on to off so it tracks the real world.

Craig

With that flow you get a message aqualinkd/Aux_4/set and you send it to both "On" and "Off" function nodes. Then each of those nodes sends it's output to the "Pool Light" node, then on to both "On" and "Off" nodes and finally each of those passes it on to MQTT.

You started off with one msg and you are probably going to end up with 4.

It would be possible to code each function node so it does not return a payload - eg in an "On" only if payload is "ON".

But as I said before, your function nodes are not doing anything that couldn't be done better with switch and/or change nodes.

I suspect that you have not yet grasped the idea of flow based programming
Have you watched these introductory videos?

@craigcurtin You described exactly what i am wanting to accomplish. i've had some success using the debug to capture the mqtt output and change nodes to convert the mqtt output to the desired input for the smartthings on/off node. i've not been able to convert the output from the on/off node back to mqtt to update status without creating an endless loop. I will grab some screenshots of the items you requested and post. i appreciate your help.

@jbudd your assessment of me grasping this is fair. i have watched many videos and will review these as well. thanks for sharing!

Yep this is the classic problem - you need to be able to differentiate between the ST switches being display only or display and control and thats where it gets tricky depending on what level of control the ST integration gives you

Essentially when you send a command to the ST virtual switch to update its status - you do not want it to feed back to you a result as if it is sending you a command - if this is not an option in the integration then you will need to create a routine that throws away commands that it receives for a set period of time.

One way to do this is to use the Trigger node - essentially you take any incoming commands from ST and feed them through the Trigger node - you have this trigger command accept changes of state from the ST switch node, but not repeated iterations.

You then also feed your NR routine into that trigger node - so if NR sends out a command to change the physical switch state state it also sends that command to the trigger node.

But yes first things first - send us the output of the debug nodes as i described in my previous post and we can build it up step by step from there

Craig

  1. here is the debug of the virtual switch on ST. Turn on has value of 1 and off is 0
    image

  2. as an example, the aqualinkd mqtt command for the pool light is:
    aqualinkd/Aux_4/set, value of 1=on 0=off

this inject node connected to a mqtt out node will turn on the pool light
image

here is the debug connected to the mqtt in node
image

  1. here is the documentation from contrib-smartthings to active the ST virtual switch node

Here is how it looks in the node itself

image

OK so thats pretty easy to start with

  1. Capture the input from the ST Virtual Switch. Using the ST Webhook integration - push the output into a switch node

Try this below (configure the MQTT broker as appropriate)

[{"id":"af6902ab.0659","type":"inject","z":"e9a79db6.6c277","name":"Simulate Turn on from ST","props":[{"p":"payload.value","v":"1","vt":"num"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payloadType":"str","x":260,"y":340,"wires":[["17bc015e.d3238f"]]},{"id":"1620309d.3cba1f","type":"inject","z":"e9a79db6.6c277","name":"Simulate Turn off from ST","props":[{"p":"payload.value","v":"0","vt":"num"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","x":260,"y":420,"wires":[["17bc015e.d3238f"]]},{"id":"17bc015e.d3238f","type":"switch","z":"e9a79db6.6c277","name":"Check for ST Turn on/off ","property":"payload.value","propertyType":"msg","rules":[{"t":"eq","v":"0","vt":"num"},{"t":"eq","v":"1","vt":"str"}],"checkall":"true","repair":false,"outputs":2,"x":630,"y":380,"wires":[["9838a3f3.19757"],["dfe441c8.bfdfa"]]},{"id":"9838a3f3.19757","type":"change","z":"e9a79db6.6c277","name":"Output Off to Aqualink","rules":[{"t":"set","p":"payload","pt":"msg","to":"0","tot":"str"},{"t":"set","p":"topic","pt":"msg","to":"aqualinkd/Aux_4/set","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":950,"y":340,"wires":[["3e17efff.6b27a"]]},{"id":"dfe441c8.bfdfa","type":"change","z":"e9a79db6.6c277","name":"Send on to Aqualink","rules":[{"t":"set","p":"payload","pt":"msg","to":"1","tot":"str"},{"t":"set","p":"topic","pt":"msg","to":"aqualinkd/Aux_4/set","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":940,"y":440,"wires":[["3e17efff.6b27a"]]},{"id":"3e17efff.6b27a","type":"mqtt out","z":"e9a79db6.6c277","name":"","topic":"","qos":"","retain":"","respTopic":"","contentType":"","userProps":"","correl":"","expiry":"","x":1200,"y":400,"wires":[]}]

So just confirming - at this stage there is no link between ST and the Aqualink system - you are going to use NR as the translator and also possibly build a NR dashboard to inject commands/run automations etc

Craig

that flow worked well for controlling the aqualinkd device. as you noted, the goal is the link ST to aqualink. my plan is to simply control the aqualinkd device from ST (i.e turn on/off and reflect correct status) from the ST app. how do i link them together using the flow you provided?

So have you installed the ST Webhook integration - you have the node sending in its commands to NR as by the debug you posted up above.

Instead of my two inject nodes - take your ST Virtual Switch node and link the output from that directly in the switch node i put up

thats the first stage and should then have switches from ST change the states in Aqualink

Craig

I have just looked some more at some of the pictures that you have posted above

  1. Why do you have the Aqualink feeding back and hoping to send updates to ST ? Presumably you are controlling everything from ST - or do you have automations running on the Aqualink also - so it can independently change the state of (say) the pool pump ?

Is yes to this - then you need to find what mqtt command you send to Aqualink to query the state of each of the possible switches

What you do is send that command out from a NR Inject node every (say) 30 seconds and then have the Aqualink respond to that with the states of all the possible devices.

When you have that working then post up the code/flow and i will show you how to set context/flow variables to store the state - we can then query those with another routine and send that to ST to tell it what the state is at any time

Can you confirm that aqualink does have its own automations ?

Craig

@craigcurtin 1. i would like to have it fed back to ST because aqualink has its own app. sometimes the wife will use that to control the pool equipment so i want the state of the device to be updated whether it is being controlled directly through aqualink or ST. aqualink has modes (i.e pool mode or spa mode) which does include some automations.

this link shows all the available commands and status updates for aqualinkd: Home · sfeakes/AqualinkD Wiki · GitHub

here is the debug output from mqtt when activated in node-red

Here is the debug output from mqtt when activated from the aqualink app

i created this flow using the output message and change nodes. it does update device status in ST when the device is turned on in aqualink but obviously not back the other direction
image

here is the content of the change node

i clearly dont have a good idea yet of how to do this the right way, so appreciate your expertise as i learn.

here is the help info for the smartthings on/off node