Page 1 of 1

mqtt temperature and humidity

Posted: Tuesday 28 April 2020 14:51
by redkooga
hi
i have been trying to send temperature and humidity from node-red but get stuck with sending with semicolon between them, how is it done?

msg={
payload:
{
"dtype" : "Temp ; Humidity",
"idx": 8,
"nvalue": 0,
"svalue": hum ; temp

}}
return msg;

Thanks

Re: mqtt temperature and humidity

Posted: Tuesday 28 April 2020 15:14
by FireWizard
HI,

See: https://piandmore.wordpress.com/2019/02 ... -domoticz/

Temperature+Humidity

The temperature+humidity sensor will show humidity and an environment level. For the temperature you can send digits after the decimal. For humidity anything after the decimal will be chopped off.

{"command":"udevice", "idx":1234, "svalue":"tm;hu;lv"}

where tm is the temperature, hu is the humidity and lv is the environment level which can have the following values:

0, normal
1, comfortable
2, dry
3, wet

So in your case you have to send from Node Red.

Code: Select all

msg.payload = {"command":"udevice","idx":8,"nvalue":0,"svalue":"temp;hum;level"};
return msg;
Regards

Re: mqtt temperature and humidity

Posted: Tuesday 28 April 2020 16:13
by redkooga
hi thanks, i tried that it it doesn't send the values the log only shows the string in quotes.

Re: mqtt temperature and humidity

Posted: Tuesday 28 April 2020 16:19
by FireWizard
Hi,

@redkooga

Can you publish your flow?

Regards

Re: mqtt temperature and humidity

Posted: Tuesday 28 April 2020 17:11
by redkooga
i have been trying to get this to work for hours, i can send each value separately.

var hum=msg.payload.humidity;
var temp=msg.payload.air_temp;
var hum=JSON.stringify(hum);
var temp=JSON.stringify(temp);

msg.payload = {
"idx":8,
"nvalue":0,
"svalue":hum, temp
};
return msg;

the log entry from domoticz

2020-04-28 16:05:40.805 MQTT: Topic: domoticz/in, Message: {"idx":8,"nvalue":0,"svalue":"0","temp":"0"}
2020-04-28 16:05:40.833 Status: Warning: Expecting svalue with at least 2 elements separated by semicolon, 1 elements received ("0"), notification not sent (Hardware: 3 - rfm12, ID: 82008, Unit: 1, Type: 52 - Temp + Humidity, SubType: 1 - THGN122/123/132, THGR122/228/238/268)

Re: mqtt temperature and humidity

Posted: Tuesday 28 April 2020 19:41
by FireWizard
Hi,

@redkooga

I will have a look to it and will try to create an example.

However it would be much easier for both of us, if you publish the flow,

Do as follows:

1. Go to Node Red and go to your tab with that flow
2. Go to the "Hamburger" menu at the top right.
3. Select "Export" and then "current flow"
4. Select "Copy to clipboard"
5. Return to this forum and in your post, select the button, marked </> and paste the contents of the clipboard (Ctrl-V)

This is much easier.

Regards

Re: mqtt temperature and humidity

Posted: Tuesday 28 April 2020 19:56
by EddyG
This my function.

Code: Select all

var HUM=msg.payload.humidity;
var HUM_STAT=0;
if (HUM < 30) {
    HUM_STAT=2;
} else if ((HUM >= 30) & (HUM < 45)) {
    HUM_STAT=0;
} else if ((HUM >= 45) & (HUM < 70)) {
    HUM_STAT=1;
} else if (HUM >= 70) {
    HUM_STAT=3;
}
msg.payload = { "command": "udevice", "idx": 552, "nvalue" : 0, "svalue": msg.payload.temperature+";"+msg.payload.humidity+";"+HUM_STAT};

return msg;

Re: mqtt temperature and humidity

Posted: Tuesday 28 April 2020 23:14
by FireWizard
Hi,

@redkooga

As an example find below a small test flow.

Screenshot_TEMP-HUM_flow.png
Screenshot_TEMP-HUM_flow.png (12.33 KiB) Viewed 4243 times
Brief description.

Left you find an "inject" node, just to start the flow. You should replace it with your flow.
Next you will find the function node in order to prepare it for Domoticz.
As indicated in the links above the TEMP/HUM sensor requires 3 parameters, as follows:

1. Temperature (degrees centigrade, as a string value)
2. Humidity (percentage 0-100, as a string value)
3. Humidity level as follows:
0=Normal
1=Comfortable
2=Dry
3=Wet

The contents of the function node is as follows:

Code: Select all

//var TEMP = msg.payload.air_temp;
var TEMP = "20";
//var HUM= msg.payload.humidity;
var HUM = "45";
var HUM_STAT=0;
if (HUM < 30) {
    HUM_STAT=2;
} else if ((HUM >= 30) & (HUM < 45)) {
    HUM_STAT=0;
} else if ((HUM >= 45) & (HUM < 70)) {
    HUM_STAT=1;
} else if (HUM >= 70) {
    HUM_STAT=3;
}
msg.payload = {"command":"udevice","idx":397,"nvalue":0,"svalue":TEMP + ";" + HUM + ";" + HUM_STAT};
return msg;
My thanks goes also to @EddyG to sort out the values for HUM_STAT.
(There might exist some discussions how the percentages are divided and it will differ a little bit between winter and summer)

If you run this flow, you will see in the "debug" node:

Screenshot_TEMP-HUM_flow2.png
Screenshot_TEMP-HUM_flow2.png (9.95 KiB) Viewed 4243 times
The "svalue" is a string consisting of three parameters, separated by a semicolon (;).
So take care that the calculated/collected values for temperature and humidity are string values.
The whole string, you should concatenate it with + sign.

I use in this example a temperature of 20 degrees and a humidity of 45%.
You should finally remove these lines and use the lines that I have commented out (//)

Replace also the IDX (397) for your IDX number.

If everything is correct you will find the next result:

Screenshot_TEMP-HUM_flow3.png
Screenshot_TEMP-HUM_flow3.png (46.84 KiB) Viewed 4243 times

Below you will find the complete flow:

Code: Select all

[{"id":"71372723.d13458","type":"tab","label":"Flow 1","disabled":false,"info":""},{"id":"b1659882.c25798","type":"inject","z":"71372723.d13458","name":"","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":240,"y":220,"wires":[["1f47577c.90fc49"]]},{"id":"1f47577c.90fc49","type":"function","z":"71372723.d13458","name":"","func":"//var tm = msg.payload.air_temp;\nvar TEMP = \"20\";\n//var hu = msg.payload.humidity;\nvar HUM = \"45\";\nvar HUM_STAT=0;\nif (HUM < 30) {\n    HUM_STAT=2;\n} else if ((HUM >= 30) & (HUM < 45)) {\n    HUM_STAT=0;\n} else if ((HUM >= 45) & (HUM < 70)) {\n    HUM_STAT=1;\n} else if (HUM >= 70) {\n    HUM_STAT=3;\n}\nmsg.payload = {\"command\":\"udevice\",\"idx\":397,\"nvalue\":0,\"svalue\":TEMP + \";\" + HUM + \";\" + HUM_STAT};\nreturn msg;","outputs":1,"noerr":0,"x":410,"y":220,"wires":[["bfc7a14b.5a7998","c028db47.8757a"]]},{"id":"bfc7a14b.5a7998","type":"debug","z":"71372723.d13458","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":590,"y":260,"wires":[]},{"id":"c028db47.8757a","type":"mqtt out","z":"71372723.d13458","name":"Domoticz In","topic":"domoticz/in","qos":"2","retain":"false","broker":"f9f13036.e28b58","x":590,"y":180,"wires":[]},{"id":"f9f13036.e28b58","type":"mqtt-broker","z":"","name":"RPI1_ MQTT_Broker","broker":"192.168.10.51","port":"1883","clientid":"","usetls":false,"compatmode":true,"keepalive":"60","cleansession":true,"birthTopic":"","birthQos":"0","birthRetain":"false","birthPayload":"","closeTopic":"","closeQos":"0","closePayload":"","willTopic":"","willQos":"0","willPayload":""}]
Best regards

Re: mqtt temperature and humidity

Posted: Wednesday 29 April 2020 9:29
by redkooga
thank you that has solved my problem :D

Re: mqtt temperature and humidity

Posted: Sunday 22 November 2020 0:18
by bjornsundberg
I have a similar issue.

I get this (IN*) from a sensor into Node-red and want to change and send it like this (2*) to domoticz:

(*1) - I recieve this (string):
{ "Battery" : 100, "RSSI" : 6, "description" : "", "dtype" : "Temp + Humidity", "id" : "36609", "idx" : 12, "name" : "Temp02", "nvalue" : 0, "stype" : "Rubicson/IW008T/TX95", "svalue1" : "5.7", "svalue2" : "84", "svalue3" : "3", "unit" : 1}


(*2) - send like this (note the change in IDX):
{"command":"udevice","idx":4,"nvalue":0,"svalue":"5,7;84;3"}

Anyone have some thoughts on how I do this, i guess function node?

Re: mqtt temperature and humidity

Posted: Sunday 22 November 2020 9:59
by FireWizard
Hi, @bjornsundberg,

You wrote:
i guess function node
You are right.

Create a "Function" node with as input the MQTT input node and as output the MQTT output node.

The contents of the "Function" node should be:

Code: Select all

msg.payload = {"command":"udevice","idx":4,"nvalue":0,"svalue":msg.payload.svalue1 + ";" + msg.payload.svalue2 + ";" + msg.payload.svalue3};
return msg;
Regards

Re: mqtt temperature and humidity

Posted: Sunday 22 November 2020 13:45
by bjornsundberg
@FireWizard Perfect!!! Thank you very very much - worked like a charm!

Re: mqtt temperature and humidity

Posted: Sunday 11 July 2021 22:07
by BounceMeister
FireWizard wrote: Sunday 22 November 2020 9:59 Hi, @bjornsundberg,

You wrote:
i guess function node
You are right.

Create a "Function" node with as input the MQTT input node and as output the MQTT output node.

The contents of the "Function" node should be:

Code: Select all

msg.payload = {"command":"udevice","idx":4,"nvalue":0,"svalue":msg.payload.svalue1 + ";" + msg.payload.svalue2 + ";" + msg.payload.svalue3};
return msg;
Regards
How would it work the other way around? When i have a message with a svalue like:

Code: Select all

"svalue": "32.56;34.68;0;1014.76;0"
and i want to extract the three values in node red, how would i get the first value e.g. 32.56 out of that string in node red?

Re: mqtt temperature and humidity

Posted: Monday 12 July 2021 17:44
by FireWizard
Hi, @BounceMeister,

I'm currently on a short holiday. I will answer your question and will come up with a solution, as soon as I have returned, as I do not have easy access to Node Red at the moment..

However can you tell me, what the five (not three) values represent?
How do you want the values presented? In an array? Or something else?

Regards

Re: mqtt temperature and humidity

Posted: Thursday 15 July 2021 19:51
by FireWizard
Hi, @BounceMeister,

As my previous questions has not been answered (yet), I will inject your string with the "Inject" node.

The string is as follows:
"svalue": "32.56;34.68;0;1014.76;0"
It looks, that it is a message received from Domoticz.

We have several possibilities to solve that and the approach is dependent on what you want with the data.
A flexible way is to put it into an array.

In this case I will use the "Node Red way".

After the "Inject" node, I put a "Change" node and this node does 2 things:

-1 Remove unnecessary characters.
-2 Split the data into the various elements and write it into an array.

The flow and the result looks as follows:

Screenshot_split_string.png
Screenshot_split_string.png (19.76 KiB) Viewed 3115 times
From there you can continue with msg.payload[0], msg.payload[1] and msg.payload[3], etc.

The complete flow is as follows:

Code: Select all

[{"id":"aad7d02a.515ff","type":"inject","z":"26947ec2.64e18a","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"svalue\": \"32.56;34.68;0;1014.76;0","payloadType":"str","x":810,"y":160,"wires":[["6a207cd0.e697c4"]]},{"id":"6a207cd0.e697c4","type":"change","z":"26947ec2.64e18a","name":"","rules":[{"t":"change","p":"payload","pt":"msg","from":"svalue\": \"","fromt":"str","to":"","tot":"str"},{"t":"set","p":"payload","pt":"msg","to":"$split(payload,';')","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":1000,"y":160,"wires":[["3b60e92b.b92fee"]]},{"id":"3b60e92b.b92fee","type":"debug","z":"26947ec2.64e18a","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":1210,"y":160,"wires":[]}]
If you have more questions, do not hesitate to ask.

Regards