When issuing a command it is usually necessary to get a acknowledgement of the command execution.
For example when throwing a switch to turn a light on you would want to know that the action has been performed.
If the action hasn’t been performed we can the try again either manually or automatically.
In this tutorial we are going to cover an example using a device which can be controlled using MQTT and HTTP.
For the examples we will be using a simple Shelly switch and a Dashboard switch node to turn the device on/off.
The actual switch type isn’t important. All that is required is that the switch responds to a command by publishing it’s current state.
MQTT Example
For our Shelly switch we send the command on the topic
The dashboard switch configuration is shown below:
The on and off payload are set in the switch and the topic field is used to pass in the switch name to the publish function.
Also note that the indicator is set to display the state of the input.
How it works
The switch sends the device name using the topic into the publish function node. The function node creates the MQTT topic that is used to publish. The code is shown below:
msg.topic="shellies/"+msg.topic+"/relay/0/command"; return msg;
The msg is sent to the MQTT node and published.
A MQTT receive node subscribed to the exact topic of
shellies/shelly1-8CAAB5614FA3/relay/0
receives the response.
Note: subscribing to simply shellies/# would require topic filtering by a receive function node.
By subscribing to the exact topic we can simple send the response message into the dashboard switch node to set the switch state.
If no response is received then the dashboard switch reverts back to the previous state.
HTTP Example
You can use either a POST or GET request in the flow we will use a get request.
The command is part of the url and looks like this
http://IPaddress/relay/0?turn=[on,off,toggle]
The flow is shown below:
The switch node settings are shown below and they are basically the same as the MQTT switch except we send the IP address of the Shelly device and not the name.
How it Works
The switch send payload of on or off with a topic set to the IP address of the Shelly device.
The publish function node constructs the URL for the http node using the IP address.
The function node code is shown below:
msg.url="http://"+msg.topic+"/relay/0?turn="+msg.payload; return msg;
When the command is sent by the http request node the response is also received by this node.
The response is a JSON object and is shown below:
The response is processed by the receive function node. The code is shown below:
let payload=JSON.parse(msg.payload); let status=payload.ison; if(status) msg.payload="on"; else msg.payload="off"; return msg;
The payload is converted by the receive function node and the message passed to the dashboard switch node to display the switch state.
If no response is received then the dashboard switch reverts back to the previous state.
Demo Flow
You can download a demo flow using the link below. The flow using dashboard 1 switches but will also work with dashboard 2 switches.
You will notice that operating either switch will change the state of the other switch which is to be expected.
However if you don’t have Shelly switch the flow comes with a simulator but in the simulator the switches operate independently.
If you don’t need the simulator then disable the flow otherwise it will interfere with the real switch.
Also when using the simulator you will need to edit the http publish function node. The instructions are in the node.
Video
Related Tutorials and Resources:
- Using HTTP APIs For IOT -Beginners Guide
- Understanding HTTP Basics
- Using MQTT APIs For IOT -Beginners Guide
- Understanding MQTT Topics