mqtt temperature and humidity

For heating/cooling related questions in Domoticz

Moderator: leecollings

Post Reply
redkooga
Posts: 4
Joined: Tuesday 28 April 2020 14:37
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

mqtt temperature and humidity

Post 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
User avatar
FireWizard
Posts: 1747
Joined: Tuesday 25 December 2018 12:11
Target OS: Raspberry Pi / ODroid
Domoticz version: Beta
Location: Voorthuizen (NL)
Contact:

Re: mqtt temperature and humidity

Post 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
redkooga
Posts: 4
Joined: Tuesday 28 April 2020 14:37
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: mqtt temperature and humidity

Post by redkooga »

hi thanks, i tried that it it doesn't send the values the log only shows the string in quotes.
User avatar
FireWizard
Posts: 1747
Joined: Tuesday 25 December 2018 12:11
Target OS: Raspberry Pi / ODroid
Domoticz version: Beta
Location: Voorthuizen (NL)
Contact:

Re: mqtt temperature and humidity

Post by FireWizard »

Hi,

@redkooga

Can you publish your flow?

Regards
redkooga
Posts: 4
Joined: Tuesday 28 April 2020 14:37
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: mqtt temperature and humidity

Post 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)
User avatar
FireWizard
Posts: 1747
Joined: Tuesday 25 December 2018 12:11
Target OS: Raspberry Pi / ODroid
Domoticz version: Beta
Location: Voorthuizen (NL)
Contact:

Re: mqtt temperature and humidity

Post 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
EddyG
Posts: 1042
Joined: Monday 02 November 2015 5:54
Target OS: -
Domoticz version:

Re: mqtt temperature and humidity

Post 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;
User avatar
FireWizard
Posts: 1747
Joined: Tuesday 25 December 2018 12:11
Target OS: Raspberry Pi / ODroid
Domoticz version: Beta
Location: Voorthuizen (NL)
Contact:

Re: mqtt temperature and humidity

Post 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 4235 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 4235 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 4235 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
Last edited by FireWizard on Wednesday 29 April 2020 9:43, edited 1 time in total.
redkooga
Posts: 4
Joined: Tuesday 28 April 2020 14:37
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: mqtt temperature and humidity

Post by redkooga »

thank you that has solved my problem :D
Attachments
grnhouse.jpg
grnhouse.jpg (135.07 KiB) Viewed 4217 times
bjornsundberg
Posts: 13
Joined: Wednesday 11 December 2019 15:49
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: mqtt temperature and humidity

Post 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?
User avatar
FireWizard
Posts: 1747
Joined: Tuesday 25 December 2018 12:11
Target OS: Raspberry Pi / ODroid
Domoticz version: Beta
Location: Voorthuizen (NL)
Contact:

Re: mqtt temperature and humidity

Post 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
bjornsundberg
Posts: 13
Joined: Wednesday 11 December 2019 15:49
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: mqtt temperature and humidity

Post by bjornsundberg »

@FireWizard Perfect!!! Thank you very very much - worked like a charm!
BounceMeister
Posts: 6
Joined: Sunday 28 June 2015 21:14
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: The moon

Re: mqtt temperature and humidity

Post 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?
User avatar
FireWizard
Posts: 1747
Joined: Tuesday 25 December 2018 12:11
Target OS: Raspberry Pi / ODroid
Domoticz version: Beta
Location: Voorthuizen (NL)
Contact:

Re: mqtt temperature and humidity

Post 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
User avatar
FireWizard
Posts: 1747
Joined: Tuesday 25 December 2018 12:11
Target OS: Raspberry Pi / ODroid
Domoticz version: Beta
Location: Voorthuizen (NL)
Contact:

Re: mqtt temperature and humidity

Post 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 3107 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
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest