How to send messages to the simulated IoT device on my PC when the received temperature value is > 23 while using a public MQTT broker

Hi i want to send a message to the simulated IoT device on my PC when the received temperature is > 23 using a public MQTT broker, the temperature and the humidity value is being sent from my simulated IoT device on my local version of Node-red

and this is the code inside the function node

// Array of simulated temperatures
var temp1 = [15, 17, 18.5, 20, 21.5, 23, 24, 22.2, 19, 1];

// Array of simulated relative humidities
var humidity1 = [50, 55, 61, 68, 65, 60, 80, 70, 62, 53];

var counter1 = context.get('counter1') || 0;
counter1 = counter1 + 1;
if (counter1 > 9)
    counter1 = 0;
context.set('counter1', counter1);

msg = {
    payload:
    {
        d: {
            "temperature": temp1[counter1],
            "humidity": humidity1[counter1]
        }
    }
};
return msg;

and this is my IoT cloud application which receives the temperature and humidity values using IBM IoT node

The message should be in the following format, where <ACTUAL_VALUE> is the newly received temperature value:
“Warning, high temperature. Temperature = <ACTUAL_VALUE> degrees C’.

I don't see why you are using context('counter1')?

Using your example of an array of data, this will send a message for every temperature, humidity pair where temperature > 23:

[{"id":"db9b6889.776dc8","type":"function","z":"c4fb1264.a93c18","name":"","func":"// Array of simulated temperatures\nvar temp1 = [15, 17, 18.5, 20, 21.5, 23, 24, 22.2, 19, 1];\n\n// Array of simulated relative humidities\nvar humidity1 = [50, 55, 61, 68, 65, 60, 80, 70, 62, 53];\n\nfor (let i in temp1) {\n    if (temp1[i] > 23){\n        msg.payload ={\n            d: {\n                \"temperature\": temp1[i],\n                \"humidity\": humidity1[i]\n            }\n        }\n        node.send(msg);\n    }\n}    \n\n","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":360,"y":180,"wires":[["b07cd94d.5e06"]]}]

If you only want to send a single message if there is any temperature > 23, change node.send(msg) to return(msg).
If you want to send the highest values you will need var maxtemp, and compare it with each array value.

Does the flow really receive data in batches as an array? Can't you send individual temperature, humidity readings?

Depending on what you are trying to achieve ?? assuming when the temperature crosses a threshold (say 23) then put a switch node with > 23 as the condition to test.

If on the other hand you want to try and handle hysterisis use the smooth node

If you do not want to see multiple temperatures then put an RBE ater the switch node.

Craig

red1

These are my flows above. I'm unable to figure out how i could set up these other questions below and get them working. Please i will appreciate an explicit explanation on how to get this sorted out, thanks.

Questions:

  1. If the received temperature value is >23 (high-temperature) then a message should be sent to the IoT device, using a public MQTT broker; you should use a unique topic for this, because the broker is public.
    The message should be in the following format, where <ACTUAL_VALUE> is the newly received temperature value:
    “Warning, high temperature. Temperature =<ACTUAL_VALUE>degrees C’.

  2. If the received humidity value is >70 (high-humidity) then a message should be sent to a ‘Slack’ messaging channel. The Slack workspace should have been created by you.
    The message should be in the following format, where <ACTUAL_VALUE> is the newly received humidity value and <DEVICE_NAME>is the Watson IoT device name taken directlyfrom the receivedIoT message:
    “Warning, device <DEVICE_NAME>humidity = <ACTUAL_VALUE>’.

Usually when we ask you to post a flow we mean to export the flow now just a picture of it

Well you need to show us the debug window to see what format the incoming temperature and humidity are.

However generally you would use a switch node to test the temperature

In the switch node you can test if Temperature > 23 and send it out to one output - this output would be a change node that could setup the topic and correctly formatted message for your broker

This would then forward the message on to an MQTT node.

I have not done anything with slack - it will be the same concept but you will have to find a slack out node to replace the MQTT node.

Craig

This flow is for the IoT Device

[{"id":"a644dc6c.6e28e8","type":"tab","label":"Deployed IoT Device-CW2","disabled":false,"info":""},{"id":"a0661c70.92bfb","type":"inject","z":"a644dc6c.6e28e8","name":"Send Data","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"2","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":130,"y":180,"wires":[["47bbeeb6.eedb2"]]},{"id":"47bbeeb6.eedb2","type":"function","z":"a644dc6c.6e28e8","name":"Device Payload","func":"// Array of simulated temperatures\nvar temp1 = [15, 17, 18.5, 20, 21.5, 23, 24, 22.2, 19, 1];\n\n// Array of simulated relative humidities\nvar humidity1 = [50, 55, 61, 68, 65, 60, 80, 70, 62, 53];\n\nvar counter1 = context.get('counter1') || 0;\ncounter1 = counter1 + 1;\nif (counter1 > 9)\n    counter1 = 0;\ncontext.set('counter1', counter1);\n\nmsg = {\n    payload:\n    {\n        d: {\n            \"temperature\": temp1[counter1],\n            \"humidity\": humidity1[counter1]\n        }\n    }\n};\nreturn msg;\n","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":340,"y":200,"wires":[["2a91433b.5baad4","faefdbdb.6f702","bda63538.44b718"]]},{"id":"faefdbdb.6f702","type":"wiotp out","z":"a644dc6c.6e28e8","authType":"d","qs":"false","qsDeviceId":"","deviceKey":"f7785387.86a37","deviceType":"","deviceId":"","event":"event","format":"json","qos":"","name":"","x":570,"y":240,"wires":[]},{"id":"2a91433b.5baad4","type":"debug","z":"a644dc6c.6e28e8","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":550,"y":160,"wires":[]},{"id":"bda63538.44b718","type":"mqtt out","z":"a644dc6c.6e28e8","name":"","topic":"temp/hum","qos":"","retain":"","respTopic":"","contentType":"","userProps":"","correl":"","expiry":"","broker":"b885666a.157e","x":340,"y":300,"wires":[]},{"id":"f7785387.86a37","type":"wiotp-credentials","name":"","org":"8e25qe","serverName":"","devType":"CW2","devId":"CW2Dev1","keepalive":"60","cleansession":true,"tls":"","usetls":false},{"id":"b885666a.157e","type":"mqtt-broker","name":"","broker":"broker.hivemq.com","port":"1883","clientid":"","usetls":false,"protocolVersion":"4","keepalive":"60","cleansession":true,"birthTopic":"Topic/temperature","birthQos":"1","birthPayload":"msg.payload","birthMsg":{},"closeTopic":"","closeQos":"0","closePayload":"","closeMsg":{},"willTopic":"","willQos":"0","willPayload":"","willMsg":{},"sessionExpiry":""}]

This flow is for the IBM Cloud Application

[{"id":"31eedb0e.d43ad4","type":"tab","label":"IBM Cloud Application-CW2","disabled":false,"info":""},{"id":"59122774.65345","type":"ibmiot in","z":"31eedb0e.d43ad4","authentication":"apiKey","apiKey":"29c89d9a.a85ada","inputType":"evt","logicalInterface":"","ruleId":"","deviceId":"","applicationId":"","deviceType":"+","eventType":"+","commandType":"","format":"json","name":"IBM IoT","service":"registered","allDevices":"","allApplications":"","allDeviceTypes":true,"allLogicalInterfaces":"","allEvents":true,"allCommands":"","allFormats":"","qos":0,"x":110,"y":100,"wires":[["f58d5388.a416e8","b18be1d4.7cf7b8","c23ac597.0c4f6"]]},{"id":"f58d5388.a416e8","type":"change","z":"31eedb0e.d43ad4","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"payload.d.humidity","tot":"msg"},{"t":"set","p":"topic","pt":"msg","to":"humidity","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":380,"y":260,"wires":[["e83cb213.5bd988"]]},{"id":"b18be1d4.7cf7b8","type":"change","z":"31eedb0e.d43ad4","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"payload.d.temperature","tot":"msg"},{"t":"set","p":"topic","pt":"msg","to":"temperature","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":380,"y":160,"wires":[["e83cb213.5bd988"]]},{"id":"e83cb213.5bd988","type":"ui_chart","z":"31eedb0e.d43ad4","name":"","group":"a6977aad.77827","order":0,"width":0,"height":0,"label":"chart","chartType":"line","legend":"false","xformat":"HH:mm:ss","interpolate":"linear","nodata":"","dot":false,"ymin":"","ymax":"","removeOlder":1,"removeOlderPoints":"","removeOlderUnit":"60","cutout":0,"useOneColor":false,"useUTC":false,"colors":["#1f77b4","#ff0000","#ff7f0e","#2ca02c","#98df8a","#ff0000","#ff9896","#9467bd","#c5b0d5"],"outputs":1,"useDifferentColor":false,"x":630,"y":200,"wires":[[]]},{"id":"c23ac597.0c4f6","type":"debug","z":"31eedb0e.d43ad4","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":350,"y":60,"wires":[]},{"id":"29c89d9a.a85ada","type":"ibmiot","name":"API-1","keepalive":"60","serverName":"","cleansession":true,"appId":"","shared":false},{"id":"a6977aad.77827","type":"ui_group","name":"Temp-Humidity Readings","tab":"c9544103.6cb6c8","order":5,"disp":true,"width":"6","collapse":false},{"id":"c9544103.6cb6c8","type":"ui_tab","name":"Home","icon":"dashboard","disabled":false,"hidden":false}]

Which of the flows should i add the switch node and the change node to answer the questions so as to enable me configure the message when the temperature is above 23 degrees and where do i add the message, is it on the change node or switch node. If you can possibly add the node and the message and then post the code so i can copy back and implement it. Thanks

Here is an example for the temperature

[{"id":"a644dc6c.6e28e8","type":"tab","label":"Deployed IoT Device-CW2","disabled":false,"info":""},{"id":"a0661c70.92bfb","type":"inject","z":"a644dc6c.6e28e8","name":"Send Data","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"2","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":130,"y":180,"wires":[["47bbeeb6.eedb2"]]},{"id":"47bbeeb6.eedb2","type":"function","z":"a644dc6c.6e28e8","name":"Device Payload","func":"// Array of simulated temperatures\nvar temp1 = [15, 17, 18.5, 20, 21.5, 23, 24, 22.2, 19, 1];\n\n// Array of simulated relative humidities\nvar humidity1 = [50, 55, 61, 68, 65, 60, 80, 70, 62, 53];\n\nvar counter1 = context.get('counter1') || 0;\ncounter1 = counter1 + 1;\nif (counter1 > 9)\n    counter1 = 0;\ncontext.set('counter1', counter1);\n\nmsg = {\n    payload:\n    {\n        d: {\n            \"temperature\": temp1[counter1],\n            \"humidity\": humidity1[counter1]\n        }\n    }\n};\nreturn msg;\n","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":340,"y":200,"wires":[["2a91433b.5baad4","e56a467c.7dbab8"]]},{"id":"2a91433b.5baad4","type":"debug","z":"a644dc6c.6e28e8","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":550,"y":160,"wires":[]},{"id":"bda63538.44b718","type":"mqtt out","z":"a644dc6c.6e28e8","name":"","topic":"temp/hum","qos":"","retain":"","respTopic":"","contentType":"","userProps":"","correl":"","expiry":"","broker":"b885666a.157e","x":960,"y":220,"wires":[]},{"id":"e56a467c.7dbab8","type":"switch","z":"a644dc6c.6e28e8","name":"","property":"payload.d.temperature","propertyType":"msg","rules":[{"t":"gt","v":"23","vt":"num"},{"t":"else"}],"checkall":"true","repair":false,"outputs":2,"x":580,"y":220,"wires":[["5a2dec94.9d9a74"],[]]},{"id":"5a2dec94.9d9a74","type":"change","z":"a644dc6c.6e28e8","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"payload.d.temperature","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":780,"y":220,"wires":[["bda63538.44b718","ae596a7f.1ed298"]]},{"id":"ae596a7f.1ed298","type":"debug","z":"a644dc6c.6e28e8","name":"temp only","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":980,"y":300,"wires":[]},{"id":"b885666a.157e","type":"mqtt-broker","name":"","broker":"broker.hivemq.com","port":"1883","clientid":"","usetls":false,"protocolVersion":"4","keepalive":"60","cleansession":true,"birthTopic":"Topic/temperature","birthQos":"1","birthPayload":"msg.payload","birthMsg":{},"closeTopic":"","closeQos":"0","closePayload":"","closeMsg":{},"willTopic":"","willQos":"0","willPayload":"","willMsg":{},"sessionExpiry":""}]

Craig

You would then look at using json expressions in the change node to add the additional text - you can have a try and post up where you get to.

This feels like a school/uni project that we are doing for you BTW

Craig

Hi, thanks for the support. The IoT device can now recieve the temperature from the cloud application that is above 23 degrees using the MQTT node. However, what is left now is adding or using the statement on JSON expression. When i add the JSON expression is gives me error.

I added the json expression on the change node where i have that of the temperature showing 2 rules. with the json expression it became 3 rules but was giving me error. I'm sure i have not gotten the right json expression and how i could input the rules on the change node.

However, its really not a school work, i'm preparing for a test that is similar to this work. Your support is really appreciated, thanks and hope to hear from you.

This is the flow below with the new nodes.

[{"id":"31eedb0e.d43ad4","type":"tab","label":"IBM Cloud Application-CW2","disabled":false,"info":""},{"id":"59122774.65345","type":"ibmiot in","z":"31eedb0e.d43ad4","authentication":"apiKey","apiKey":"29c89d9a.a85ada","inputType":"evt","logicalInterface":"","ruleId":"","deviceId":"","applicationId":"","deviceType":"+","eventType":"+","commandType":"","format":"json","name":"IBM IoT","service":"registered","allDevices":"","allApplications":"","allDeviceTypes":true,"allLogicalInterfaces":"","allEvents":true,"allCommands":"","allFormats":"","qos":0,"x":70,"y":160,"wires":[["f58d5388.a416e8","fbb762e5.64c5c8"]]},{"id":"f58d5388.a416e8","type":"change","z":"31eedb0e.d43ad4","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"payload.d.humidity","tot":"msg"},{"t":"set","p":"topic","pt":"msg","to":"humidity","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":280,"y":340,"wires":[["e83cb213.5bd988"]]},{"id":"b18be1d4.7cf7b8","type":"change","z":"31eedb0e.d43ad4","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"payload.d.temperature","tot":"msg"},{"t":"set","p":"topic","pt":"msg","to":"temperature","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":300,"y":260,"wires":[["e83cb213.5bd988","564171e3.1b5858","f8ac0e04.71bb3"]]},{"id":"e83cb213.5bd988","type":"ui_chart","z":"31eedb0e.d43ad4","name":"","group":"a6977aad.77827","order":0,"width":0,"height":0,"label":"chart","chartType":"line","legend":"false","xformat":"HH:mm:ss","interpolate":"linear","nodata":"","dot":false,"ymin":"","ymax":"","removeOlder":1,"removeOlderPoints":"","removeOlderUnit":"60","cutout":0,"useOneColor":false,"useUTC":false,"colors":["#1f77b4","#ff0000","#ff7f0e","#2ca02c","#98df8a","#ff0000","#ff9896","#9467bd","#c5b0d5"],"outputs":1,"useDifferentColor":false,"x":510,"y":300,"wires":[[]]},{"id":"c23ac597.0c4f6","type":"debug","z":"31eedb0e.d43ad4","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":530,"y":100,"wires":[]},{"id":"fbb762e5.64c5c8","type":"switch","z":"31eedb0e.d43ad4","name":"","property":"payload.d.temperature","propertyType":"msg","rules":[{"t":"gt","v":"23","vt":"num"},{"t":"else"}],"checkall":"true","repair":false,"outputs":2,"x":270,"y":160,"wires":[["b18be1d4.7cf7b8"],["c23ac597.0c4f6"]]},{"id":"564171e3.1b5858","type":"debug","z":"31eedb0e.d43ad4","name":"Temp. Above 23 Degrees","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":590,"y":180,"wires":[]},{"id":"f8ac0e04.71bb3","type":"mqtt out","z":"31eedb0e.d43ad4","name":"","topic":"send/temp","qos":"","retain":"","respTopic":"","contentType":"","userProps":"","correl":"","expiry":"","broker":"e758f019.1038e8","x":560,"y":240,"wires":[]},{"id":"29c89d9a.a85ada","type":"ibmiot","name":"API-1","keepalive":"60","serverName":"","cleansession":true,"appId":"","shared":false},{"id":"a6977aad.77827","type":"ui_group","name":"Temp-Humidity Readings","tab":"c9544103.6cb6c8","order":5,"disp":true,"width":"6","collapse":false},{"id":"e758f019.1038e8","type":"mqtt-broker","name":"","broker":"mqtt://broker.hivemq.com","port":"1883","clientid":"","usetls":false,"protocolVersion":"4","keepalive":"60","cleansession":true,"birthTopic":"","birthQos":"0","birthPayload":"","birthMsg":{},"closeTopic":"","closeQos":"0","closePayload":"","closeMsg":{},"willTopic":"","willQos":"0","willPayload":"","willMsg":{},"sessionExpiry":""},{"id":"c9544103.6cb6c8","type":"ui_tab","name":"Home","icon":"dashboard","disabled":false,"hidden":false}]