Get sensor values with MQTT into Domoticz [Solved]

Everything about esp8266 and more.

Moderator: leecollings

Lipa
Posts: 6
Joined: Thursday 12 November 2020 12:50
Target OS: Raspberry Pi / ODroid
Domoticz version: 2024.4
Location: Slovenija
Contact:

Re: Get sensor values with MQTT into Domoticz [Solved]

Post by Lipa »

Hello.

This is a screenshot of the Debug node output, in the case of the "Topic" field: home/weather/solarweatherstation/#

2020-11-12_20-13-23.jpg
2020-11-12_20-13-23.jpg (114.67 KiB) Viewed 2338 times

"Lepo vreme" means "Nice weather" in Slovenian language.
At first I would be happy if I could get in Domoticz data of tempc, humi and relhpa in Domoticz sensor »temp+hum+baro«
Where should environment level and prediction come from, I don't know. Maybe let’s leave them for a begining.
All sensors a published at the same moment.

Regards
Roman
User avatar
FireWizard
Posts: 1745
Joined: Tuesday 25 December 2018 12:11
Target OS: Raspberry Pi / ODroid
Domoticz version: Beta
Location: Voorthuizen (NL)
Contact:

Re: Get sensor values with MQTT into Domoticz [Solved]

Post by FireWizard »

Hello Roman (@Lipa),

You wrote:
All sensors are published at the same moment.
Strictly spoken, that is not true. You can see it easily in your debug sequence.
All of them are arriving at 20:07:59, except the last, which arrives at 20:08:00.
Totally 12 messages. We should remember.

For the moment I have created a small flow, which handles
- temperature-
- humidity
- rel. pressure
- (environmental) level
This is a function, based on some calculation, which you can find : viewtopic.php?f=34&t=32414&p=245393&hil ... el#p245393
-prediction This is unknown for the moment, so I use "0".

It is based on:
Temperature+Humidity+Barometer

The temperature+Humidity+Barometer sensor will show just that and an environment level and a prediction. You can send digits for the temperature but only the first is displayed (it is rounded off). The humidity only displays whole numbers (chops off any digits).

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



where tm is the temperature, hu is the humidity and lv is the environment level, ba is the pressure and pr is the prediction.

lv can have the following values:

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

pr can have the following values:

0, no prediction
1, sunny
2, partly cloudy
3, cloudy
4, rain
5, unknown
(oddly enough 6 cloudy/rain does not work for this sensor)
Screenshot_weather_station1.png
Screenshot_weather_station1.png (8.81 KiB) Viewed 2330 times

The first node is the MQTT input node, which subscribe to your topic: home/weather/solarweatherstation/#
The second node is a "Join", which combines all incoming messages.
Currently 3 messages (temperature, humidity and pressure), but your weather station publish a sequence of 12 messages.
You should edit the "Join"node and replace "After a number of message parts" number 3 for 12.

In the 'Function" node you have to replace in line 17 the IDX for the IDX of your Domoticz device.

If everything has been done correctly, this is the result:

Screenshot_weather_station2.png
Screenshot_weather_station2.png (49.05 KiB) Viewed 2330 times

Regards
User avatar
FireWizard
Posts: 1745
Joined: Tuesday 25 December 2018 12:11
Target OS: Raspberry Pi / ODroid
Domoticz version: Beta
Location: Voorthuizen (NL)
Contact:

Re: Get sensor values with MQTT into Domoticz [Solved]

Post by FireWizard »

Hello Roman (@Lipa)

I forgot the most important part, the flow itself.

Code: Select all

[{"id":"a4e7fffc.b8f908","type":"mqtt in","z":"4b2085e6.d2fdf4","name":"Weather Station In","topic":"home/weather/solarweatherstation/#","qos":"0","datatype":"json","broker":"8591549f.77809","x":250,"y":320,"wires":[["8034b495.e06e18"]]},{"id":"8034b495.e06e18","type":"join","z":"4b2085e6.d2fdf4","name":"","mode":"custom","build":"object","property":"payload","propertyType":"msg","key":"topic","joiner":"\\n","joinerType":"str","accumulate":true,"timeout":"","count":"3","reduceRight":false,"reduceExp":"","reduceInit":"","reduceInitType":"","reduceFixup":"","x":450,"y":320,"wires":[["12537246.eac82e"]]},{"id":"12537246.eac82e","type":"function","z":"4b2085e6.d2fdf4","name":"","func":"var tm = msg.payload[\"home/weather/solarweatherstation/tempc\"];\nvar hu = msg.payload[\"home/weather/solarweatherstation/humi\"];\nvar ba = msg.payload[\"home/weather/solarweatherstation/relhpa\"];\n\nfunction lv (hum) {\nif (hum < 30) {\n    return 2;\n} else if ((hum >= 30) & (hum < 45)) {\n    return 0;\n} else if ((hum >= 45) & (hum < 70)) {\n    return 1;\n} else if (hum >= 70) {\n    return 3;    \n}\n}\n\nmsg.payload = {\"command\":\"udevice\", \"idx\":31, \"svalue\":tm.toString() + \";\" + hu.toString() + \";\" + lv(hu).toString() + \";\" + ba.toString() + \";\" + \"0\"}\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":620,"y":320,"wires":[["3e6c8450.78724c"]]},{"id":"3e6c8450.78724c","type":"mqtt out","z":"4b2085e6.d2fdf4","name":"Domoticz In","topic":"domoticz/in","qos":"2","retain":"false","broker":"f9f13036.e28b58","x":810,"y":320,"wires":[]},{"id":"8591549f.77809","type":"mqtt-broker","name":"Jonas_MQTT_Server","broker":"192.168.10.24","port":"1883","clientid":"","usetls":false,"compatmode":true,"keepalive":"60","cleansession":true,"birthTopic":"","birthQos":"0","birthPayload":"","closeTopic":"","closeQos":"0","closePayload":"","willTopic":"","willQos":"0","willPayload":""},{"id":"f9f13036.e28b58","type":"mqtt-broker","name":"localhost","broker":"127.0.0.1","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":""}]

Regards
Lipa
Posts: 6
Joined: Thursday 12 November 2020 12:50
Target OS: Raspberry Pi / ODroid
Domoticz version: 2024.4
Location: Slovenija
Contact:

Re: Get sensor values with MQTT into Domoticz [Solved]

Post by Lipa »

Hello FireWizard

Thanks, it works great ...

SolarWS.jpg
SolarWS.jpg (26.43 KiB) Viewed 2325 times

Code: Select all

2020-11-13 19:09:46.425 MQTT: Topic: domoticz/in, Message: {"command":"udevice","idx":121,"svalue":"9.6;91;3;1028;0"}
2020-11-13 19:09:46.536 MQTT: Topic: domoticz/in, Message: {"command":"udevice","idx":121,"svalue":"9.6;91;3;1028;0"}
2020-11-13 19:09:46.646 MQTT: Topic: domoticz/in, Message: {"command":"udevice","idx":121,"svalue":"9.6;91;3;1028;0"}
2020-11-13 19:09:46.757 MQTT: Topic: domoticz/in, Message: {"command":"udevice","idx":121,"svalue":"9.6;91;3;1028;0"}
2020-11-13 19:09:46.868 MQTT: Topic: domoticz/in, Message: {"command":"udevice","idx":121,"svalue":"9.6;91;3;1028;0"}
2020-11-13 19:09:46.979 MQTT: Topic: domoticz/in, Message: {"command":"udevice","idx":121,"svalue":"9.6;91;3;1028;0"}
2020-11-13 19:09:47.089 MQTT: Topic: domoticz/in, Message: {"command":"udevice","idx":121,"svalue":"9.6;91;3;1028;0"}
2020-11-13 19:09:47.200 MQTT: Topic: domoticz/in, Message: {"command":"udevice","idx":121,"svalue":"9.6;91;3;1028;0"}
2020-11-13 19:09:47.318 MQTT: Topic: domoticz/in, Message: {"command":"udevice","idx":121,"svalue":"9.6;91;3;1028;0"}
2020-11-13 19:09:47.430 MQTT: Topic: domoticz/in, Message: {"command":"udevice","idx":121,"svalue":"9.6;91;3;1028;0"}
Why does the Domoticz log message repeat 10 times?
Probably because:
Strictly spoken, that is not true. You can see it easily in your debug sequence.
All of them are arriving at 20:07:59, except the last, which arrives at 20:08:00.
Totally 12 messages. We should remember.
Regards
User avatar
FireWizard
Posts: 1745
Joined: Tuesday 25 December 2018 12:11
Target OS: Raspberry Pi / ODroid
Domoticz version: Beta
Location: Voorthuizen (NL)
Contact:

Re: Get sensor values with MQTT into Domoticz [Solved]

Post by FireWizard »

Hi,

@Lipa

You wrote:
Why does the Domoticz log message repeat 10 times?
Probably because:

Strictly spoken, that is not true. You can see it easily in your debug sequence.
All of them are arriving at 20:07:59, except the last, which arrives at 20:08:00.
Totally 12 messages. We should remember.
That is correct.

With the current content of the Function node the flow is triggered on all messages.
The first 3 incoming messages result in 1 outgoing message and then every incoming gives the same outgoing. So totally 10.

I think we can improve it by adding a node, so that it is only triggering, if temperature, humidity or pressure is received.
This node is not part of the default node set, so you have to load it from the Palette

To do so

1 .Go to the "hamburger" menu, just right of the Deploy button.
2. Click "Manage palette".
3. Goto to the tab "Install"
4. Search for node-red-contrib-filter
5. Install this node.

Please find below the new flow with "Filter" node included.

Code: Select all

[{"id":"a4e7fffc.b8f908","type":"mqtt in","z":"4b2085e6.d2fdf4","name":"Weather Station In","topic":"home/weather/solarweatherstation/#","qos":"0","datatype":"json","broker":"8591549f.77809","x":150,"y":320,"wires":[["2e7e4f04.135788"]]},{"id":"8034b495.e06e18","type":"join","z":"4b2085e6.d2fdf4","name":"","mode":"custom","build":"object","property":"payload","propertyType":"msg","key":"topic","joiner":"\\n","joinerType":"str","accumulate":true,"timeout":"","count":"3","reduceRight":false,"reduceExp":"","reduceInit":"","reduceInitType":"","reduceFixup":"","x":610,"y":320,"wires":[["12537246.eac82e"]]},{"id":"12537246.eac82e","type":"function","z":"4b2085e6.d2fdf4","name":"","func":"var tm = msg.payload[\"home/weather/solarweatherstation/tempc\"];\nvar hu = msg.payload[\"home/weather/solarweatherstation/humi\"];\nvar ba = msg.payload[\"home/weather/solarweatherstation/relhpa\"];\n\nfunction lv (hum) {\nif (hum < 30) {\n    return 2;\n} else if ((hum >= 30) & (hum < 45)) {\n    return 0;\n} else if ((hum >= 45) & (hum < 70)) {\n    return 1;\n} else if (hum >= 70) {\n    return 3;    \n}\n}\n\nmsg.payload = {\"command\":\"udevice\", \"idx\":31, \"svalue\":tm.toString() + \";\" + hu.toString() + \";\" + lv(hu).toString() + \";\" + ba.toString() + \";\" + \"0\"}\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":780,"y":320,"wires":[["3e6c8450.78724c"]]},{"id":"3e6c8450.78724c","type":"mqtt out","z":"4b2085e6.d2fdf4","name":"Domoticz In","topic":"domoticz/in","qos":"2","retain":"false","broker":"f9f13036.e28b58","x":970,"y":320,"wires":[]},{"id":"2e7e4f04.135788","type":"filter","z":"4b2085e6.d2fdf4","name":"Filter Temp/Hum/Bar","property":"topic","propertyType":"msg","asArray":false,"itemProperty":"","itemPropertyType":"item","rules":[{"t":"eq","v":"home/weather/solarweatherstation/tempc","vt":"str","output":1},{"t":"eq","v":"home/weather/solarweatherstation/humi","vt":"str","output":1},{"t":"eq","v":"home/weather/solarweatherstation/relhpa","vt":"str","output":1}],"checkall":"true","outputs":1,"x":400,"y":320,"wires":[["8034b495.e06e18"]]},{"id":"8591549f.77809","type":"mqtt-broker","name":"Jonas_MQTT_Server","broker":"192.168.10.24","port":"1883","clientid":"","usetls":false,"compatmode":true,"keepalive":"60","cleansession":true,"birthTopic":"","birthQos":"0","birthPayload":"","closeTopic":"","closeQos":"0","closePayload":"","willTopic":"","willQos":"0","willPayload":""},{"id":"f9f13036.e28b58","type":"mqtt-broker","name":"localhost","broker":"127.0.0.1","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":""}]
This "Filter" node let only the 3 messages, containing the currently used items, pass.
So if you want more items, you might have to change the content of the "Filter" node.

Also set the "Join" node to 3 again.

Regards
Last edited by FireWizard on Saturday 14 November 2020 9:31, edited 1 time in total.
Lipa
Posts: 6
Joined: Thursday 12 November 2020 12:50
Target OS: Raspberry Pi / ODroid
Domoticz version: 2024.4
Location: Slovenija
Contact:

Re: Get sensor values with MQTT into Domoticz [Solved]

Post by Lipa »

Hi,

@FireWizard

Thank you so much, now I just see how powerful a tool Node Red is if you know him.
There are now three records in the log.

Code: Select all

020-11-13 23:19:30.759 (Aeotec ZW090) General/kWh (AN 158 kWh Meter)
2020-11-13 23:19:52.742 MQTT: Topic: domoticz/in, Message: {"command":"udevice","idx":120,"svalue":"8.8;91;3;1029;0"}
2020-11-13 23:19:52.857 MQTT: Topic: domoticz/in, Message: {"command":"udevice","idx":120,"svalue":"8.8;91;3;1029;0"}
2020-11-13 23:19:52.968 MQTT: Topic: domoticz/in, Message: {"command":"udevice","idx":120,"svalue":"8.8;91;3;1029;0"}
2020-11-13 23:19:53.079 MQTT: Topic: domoticz/in, Message: {"command":"udevice","idx":121,"nvalue":0,"svalue":"4.02"}
2020-11-13 23:20:04.075 (Aeotec ZW090) Usage (AN 158 W Meter)
Thanks again for the help and I still ask you for further instructions if I need it.
lazaruss72
Posts: 17
Joined: Thursday 14 December 2017 23:13
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Get sensor values with MQTT into Domoticz [Solved]

Post by lazaruss72 »

Hi @FireWizard,
you are my last great hope :-)
I sit now for days on it and just do not come further.
I always get only: svalue: undefined out. Somewhere I have an error, but I just can't find it. Maybe I am doing fundamentally wrong.
I have the hope that you with your knowledge simply say "Yes, there is the error, take this value and it works".

Here is my situation:
I collect data from my electricity meter via a Raspberry (vzlogger aka volkszähler) and output that there via MQTT. I have installed a MQTT broker and Node Red and everything works. For example, I can write to a text sensor via Node Red.

in the node mqtt in I see the data:
mqtt_in.jpg
mqtt_in.jpg (85.4 KiB) Viewed 2232 times
in the json node also
json.jpg
json.jpg (64.55 KiB) Viewed 2232 times
But no matter how I try, I don't get anything usable from the function node.
I don't really have a clue about JavaScript. i tried to understand your examples and adapt them to me, but it just doesn't work.
function1.jpg
function1.jpg (72.6 KiB) Viewed 2232 times
with this code:

Code: Select all

var us = msg.payload["domoticz/in/chn1/raw"];
var cu = msg.payload["domoticz/in/chn3/raw"];
msg.payload = {"command":"udevice", "idx":473, "svalue":us}
return msg;

Can you tell me what i am doing wrong and how i can solve this?

The actual goal then is to get two values into my electric sensor.
The current consumption and the meter reading.

I really hope that you can help me, or someone else who has already implemented something like this.

many greetings

Marco
User avatar
FireWizard
Posts: 1745
Joined: Tuesday 25 December 2018 12:11
Target OS: Raspberry Pi / ODroid
Domoticz version: Beta
Location: Voorthuizen (NL)
Contact:

Re: Get sensor values with MQTT into Domoticz [Solved]

Post by FireWizard »

HI, @lazaruss72, Marco

It is not a huge problem.

1. If I look to the output of your MQTT out node, I see you are subscribed to domoticz/in/#.
That does not look correct. You use as input everything that is sent to Domoticz. In fact you are looking to a loop. And Domoticz can not handle this data.

You said:
vzlogger aka volkszähler
There exist a Node Red node for Volkszähler. Do you use that one?
See: https://flows.nodered.org/node/node-red ... lkszaehler

I see discussions about MQTT support for vzlogger. If you use that (I assume you do) do you have info about the published topics?

Your topic is domoticz/in/chn1/raw and also chn0, chn2 and chn3.

However, I found somewhere on Internet that vzlogger publishes the data under topic: vzlogger/data/chn0/raw.
Can you confirm this?

2. The Domoticz sensor, you want to control.
Is this of type Electric?

Screenshot_electric.png
Screenshot_electric.png (43.71 KiB) Viewed 2225 times
If you can give me that information, I have a flow ready.

Regards
lazaruss72
Posts: 17
Joined: Thursday 14 December 2017 23:13
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Get sensor values with MQTT into Domoticz [Solved]

Post by lazaruss72 »

Hello FireWizard,

first of all thank you for your prompt reply.

to your questions:
the orginal topic in volkszähler is vzlogger/data, that's right. I had changed it to domoticz/in since I don't have any other mqtt stuff and thought it has no effect.

the node red nodes for volkszähler i didn't know and i don't have them in use. i have them installed now but i don't understand how to use them. don't really find information about them.

Yes, I can confirm that the vzlogger publishes by default under the topic vzlogger/data/chn0/raw.

And yes, this is exactly the sensor I want to feed with data.
I think that is the right one.
sensor.png
sensor.png (8.58 KiB) Viewed 2218 times
I am very curious about your flow and hope that it is the solution... the topic has already given me some sleepless nights.

many greetings
lazarus
User avatar
FireWizard
Posts: 1745
Joined: Tuesday 25 December 2018 12:11
Target OS: Raspberry Pi / ODroid
Domoticz version: Beta
Location: Voorthuizen (NL)
Contact:

Re: Get sensor values with MQTT into Domoticz [Solved]

Post by FireWizard »

Hello @lazaruss72, Hello Marco.

If I look to your screenshots, I see that the value is a string. (the color is red and it is between " "). I see also that you have multiple channels (chn0, chn1, chn2 and chn3). Perhaps more, that I cannot see. I noticed you want to use the data from chn1 and chn 2.

I suggest the following flow:

Screenshot_vzlogger1.png
Screenshot_vzlogger1.png (26.04 KiB) Viewed 2207 times
As we have (at least) 4 channels, from which we want to use only 2, I propose not to use wildcards, like + or #, but to create 2 MQTT Input nodes with the correct topic. This avoids that we have to remove the other, unused topics, which may interfere with the correct stream.
The data is a simple payload with the value in a string. If it is a number we have to modify the "function"node slightly.

These two topics are joined in a Join node, where the topic (vzlogger/data/chn1/raw and vzlogger/data/chn3/raw) is used as the "key".
The result, you will see in the screenshot below.

These values are used in the function node to prepare the correct data to Domoticz.
The correct syntax is: {"command":"udevice", "idx":1234, "svalue":"us;cu"}.

The command "command":"udevice" is the default command and therefore may be omitted, but for clarity. I left it in.
You have to insert your IDX number for the sensor you use (electric)
The svalue has always to be a string value. In this case two string variable (us and cu), separated by a semicolon (;).

This is prepared in the function node.

Code: Select all

var us = msg.payload["vzlogger/data/chn1/raw."];
var cu = msg.payload["vzlogger/data/chn3/raw."];

msg.payload = {"command":"udevice", "idx":45, "svalue":us + ";" + cu};
return msg;
If the data consists of a number (blue color and no " ") you have to make it as a string with the function as follows:

Code: Select all

var us = msg.payload["vzlogger/data/chn1/raw."].toString();
var cu = msg.payload["vzlogger/data/chn3/raw."].toString();

msg.payload = {"command":"udevice", "idx":45, "svalue":us + ";" + cu};
return msg;
The result is as follows:

Screenshot_vzlogger2.png
Screenshot_vzlogger2.png (18.82 KiB) Viewed 2207 times
and will give you this values in the sensor:

Screenshot_vzlogger3.png
Screenshot_vzlogger3.png (44.89 KiB) Viewed 2207 times
I entered just some figures, so keep in mind that cu is cumulative and the values are added.
The input should be in Wh, although the presentation wil be in kWh.

Please find attached the complete flow:

Code: Select all

[{"id":"258d3ca4.36a2ec","type":"mqtt in","z":"b49db3af.cc9f8","name":"","topic":"vzlogger/data/chn1/raw.","qos":"2","datatype":"json","broker":"f9f13036.e28b58","x":250,"y":260,"wires":[["5afd9f35.aa70f"]]},{"id":"97ca8bf2.e3361","type":"function","z":"b49db3af.cc9f8","name":"","func":"var us = msg.payload[\"vzlogger/data/chn1/raw.\"];\nvar cu = msg.payload[\"vzlogger/data/chn3/raw.\"];\n\nmsg.payload = {\"command\":\"udevice\", \"idx\":45, \"svalue\":us + \";\" + cu};\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":640,"y":320,"wires":[["f352fb59.cd04f","d0e46a68.0d0e08"]]},{"id":"f352fb59.cd04f","type":"debug","z":"b49db3af.cc9f8","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":830,"y":422,"wires":[]},{"id":"d0e46a68.0d0e08","type":"mqtt out","z":"b49db3af.cc9f8","name":"","topic":"domoticz/in","qos":"0","retain":"false","broker":"f9f13036.e28b58","x":830,"y":320,"wires":[]},{"id":"5f929d20.09ce6c","type":"mqtt in","z":"b49db3af.cc9f8","name":"","topic":"vzlogger/data/chn3/raw.","qos":"2","datatype":"json","broker":"f9f13036.e28b58","x":250,"y":380,"wires":[["5afd9f35.aa70f"]]},{"id":"5afd9f35.aa70f","type":"join","z":"b49db3af.cc9f8","name":"","mode":"custom","build":"object","property":"payload","propertyType":"msg","key":"topic","joiner":"\\n","joinerType":"str","accumulate":true,"timeout":"","count":"2","reduceRight":false,"reduceExp":"","reduceInit":"","reduceInitType":"","reduceFixup":"","x":470,"y":320,"wires":[["4d33fb01.4811b4","97ca8bf2.e3361"]]},{"id":"4d33fb01.4811b4","type":"debug","z":"b49db3af.cc9f8","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":650,"y":262,"wires":[]},{"id":"f9f13036.e28b58","type":"mqtt-broker","name":"localhost","broker":"127.0.0.1","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":""}]
Do not forget to insert the correct IDX number

Awaiting your result.

Regards
lazaruss72
Posts: 17
Joined: Thursday 14 December 2017 23:13
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Get sensor values with MQTT into Domoticz [Solved]

Post by lazaruss72 »

Hello FireWizard,

thank you for your answer and the detailed explanations.
The two inputs and the join is completely ingenious.

I have tested your flow and unfortunately have to say that at the end comes out again undefined :-(

As you can see in the screenshot below after the join at debug node both values occur.
But at debug node after the function arrives undefined.
mqtt_flow.png
mqtt_flow.png (83.35 KiB) Viewed 2201 times
I don't understand why ... your function looks very logical. i also tried it with the second code. there is the exact same thing

best regards
lazarus
User avatar
FireWizard
Posts: 1745
Joined: Tuesday 25 December 2018 12:11
Target OS: Raspberry Pi / ODroid
Domoticz version: Beta
Location: Voorthuizen (NL)
Contact:

Re: Get sensor values with MQTT into Domoticz [Solved]

Post by FireWizard »

Hi, @lazaruss72

I see it. There is a dot behind the topic. No idea, how that has been introduced. Obviously introduced by me, during copy/paste.

I have now the following flow:

Screenshot_vzlogger4.png
Screenshot_vzlogger4.png (68.67 KiB) Viewed 2192 times

And the contents of the 'Function" node is now as follows, as it turned out to be a number and not a string:

Code: Select all

var us = msg.payload["vzlogger/data/chn1/raw"].toString();
var cu = msg.payload["vzlogger/data/chn3/raw"].toString();

msg.payload = {"command":"udevice", "idx":45, "svalue":us + ";" + cu};
return msg;
Do not forget to insert the correct IDX number

Awaiting your result.

Regards
lazaruss72
Posts: 17
Joined: Thursday 14 December 2017 23:13
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Get sensor values with MQTT into Domoticz [Solved]

Post by lazaruss72 »

Hello @FireWizard,

You are absolutely amazing!
IT WORKS
I had seen the point at mqtt in node and corrected it, but it didn't occur to me to do the same in the function node. that's not where I noticed it.
I am so happy that it is finally working now!!! Many many thanks for that.
verbrauch.png
verbrauch.png (29.74 KiB) Viewed 2184 times
verbrauch2.png
verbrauch2.png (134.36 KiB) Viewed 2184 times
Now I will put the second sensor into operation and hope that I can manage it.

Stay healthy and many greetings

lazarus
User avatar
FireWizard
Posts: 1745
Joined: Tuesday 25 December 2018 12:11
Target OS: Raspberry Pi / ODroid
Domoticz version: Beta
Location: Voorthuizen (NL)
Contact:

Re: Get sensor values with MQTT into Domoticz [Solved]

Post by FireWizard »

Hi, @lazaruss72

Nice that it is working as expected
Now I will put the second sensor into operation and hope that I can manage it.
If you need some support, do not hesitate to ask.

Best regards
rippe
Posts: 7
Joined: Monday 07 October 2019 18:33
Target OS: Windows
Domoticz version:
Contact:

Re: Get sensor values with MQTT into Domoticz [Solved]

Post by rippe »

Hello Dear expert,

So sorry to trouble you with another noob at this... :-(

I am trying to get measured current data to Domoticz (using EmonTX Shield for Arduino).
I have MQTT and Node-red up and running (I can see the Json data on the MQTT broker).
It looks like this: {"Phase1":xxx,"Phase2":yyy,"Phase3":zzz}

I did at a time manage to get updated values to the three 1-phase labels in Domoticz, but always with the (quite useless) value zero. In this picture they aren't updates at all.
Below is the flow with code from this helpful thread.

Hoping to get some help.
Thanjs in advance.
Br Richard
Attachments
Domoticz.JPG
Domoticz.JPG (30.12 KiB) Viewed 2035 times
MQTT_to_Domoticz.JPG
MQTT_to_Domoticz.JPG (50.7 KiB) Viewed 2035 times
User avatar
FireWizard
Posts: 1745
Joined: Tuesday 25 December 2018 12:11
Target OS: Raspberry Pi / ODroid
Domoticz version: Beta
Location: Voorthuizen (NL)
Contact:

Re: Get sensor values with MQTT into Domoticz [Solved]

Post by FireWizard »

Hello @rippe
I did at a time manage to get updated values to the three 1-phase labels in Domoticz, but always with the (quite useless) value zero. In this picture they aren't updates at all.
As far as I can see, the values for Current L1, CurrentL2 and Current L3 should update. The commands to Domoticz are correct.
Dis you ever try to comment out the line starting with msg4? By putting // in front of it. And also remove msg4 between [[ ]]?

It should work.

But the problem is msg4.

What you do is that you send with msg 4, 3 complete message objects. You can see that after IDX 327 in the picture you send in your post.
What you want is to send the 3 values (ampere) of both Phase 1, Phase 2 and Phase 3 (separated by a semicolon ;)
These values you will find back in msg1.payload.svalue, etc.

I modified your flow as follows:

Code: Select all

var msg1 ={};
var msg2 ={};
var msg3 ={};
var msg4 ={};
msg1.payload = {"command": "udevice","idx":18,"svalue": msg.payload.Phase1.toString()};
msg2.payload = {"command": "udevice","idx":19,"svalue": msg.payload.Phase2.toString()};
msg3.payload = {"command": "udevice","idx":20,"svalue": msg.payload.Phase3.toString()};
msg4.payload = {"command": "udevice","idx":21,"svalue": msg1.payload.svalue + ";" + msg2.payload.svalue + ";" + msg3.payload.svalue};
return [[msg1,msg2,msg3,msg4]]
I changed the IDX (18-21) numbers in order to test on my system.

This results in:

Screenshot_3fase-electra1.png
Screenshot_3fase-electra1.png (63.65 KiB) Viewed 2028 times

And in Domoticz:

Screenshot_3fase-electra2.png
Screenshot_3fase-electra2.png (118.4 KiB) Viewed 2028 times
Best regards
rippe
Posts: 7
Joined: Monday 07 October 2019 18:33
Target OS: Windows
Domoticz version:
Contact:

Re: Get sensor values with MQTT into Domoticz [Solved]

Post by rippe »

Hi @FireWizard,

Jeeez... You are quick :D
Message 4 was much better.
However, Domoticz still will not update the values. Have a look at the picture. I believe it's somthing with the incoming string or something (see the parsing error).

Best regards,
Richard
Attachments
ParseError.JPG
ParseError.JPG (49.77 KiB) Viewed 2026 times
MQTT_to_Domoticz_3.JPG
MQTT_to_Domoticz_3.JPG (35.14 KiB) Viewed 2026 times
MQTT_to_Domoticz_2.JPG
MQTT_to_Domoticz_2.JPG (67.01 KiB) Viewed 2026 times
User avatar
FireWizard
Posts: 1745
Joined: Tuesday 25 December 2018 12:11
Target OS: Raspberry Pi / ODroid
Domoticz version: Beta
Location: Voorthuizen (NL)
Contact:

Re: Get sensor values with MQTT into Domoticz [Solved]

Post by FireWizard »

Hi, @rippe

It looks much better, but I really do not understand why the sensors are not updated.

If I take my values (and my input is exactly the same as yours) and configure the MQTT input node, to receive a string, then it gives me a string of 55 characters. The error you get, "Failed to parse JSON string" indicates a string of 27 characters.

But the output of your MQTT input node is a parsed JSON Object.

I assume that this error message is coming from this flow and not from another flow at another tab.
If you hoover with your mouse over the error message, does it indicate the MQTT input node?

If so, can you disconnect the MQTT input node from the "Function" node and change the Output to auto-detect.
Can you then make a screenshot of that first "Debug" node.

What I do not understand is why the sensors are not updated, while the commands are correct.

Question.

1. Is the MQTT out node correctly configured? (Is it pointing to same MQTT server as where Domoticz has subscribed to?)
Are there more MQTT servers?

2. Are the IDX numbers correct and do they match with the configured virtual sensors?

Let me know,

Regards
rippe
Posts: 7
Joined: Monday 07 October 2019 18:33
Target OS: Windows
Domoticz version:
Contact:

Re: Get sensor values with MQTT into Domoticz [Solved]

Post by rippe »

Hi @FireWizard,

Yes, doesn't it :-).

It comes from the Arduino code and "StaticJsonDocument", so it should be correct (I suppose):
const int capacity = JSON_OBJECT_SIZE(3);
StaticJsonDocument<capacity> doc;
doc["Phase1"] = Irms1;
doc["Phase2"] = Irms2;
doc["Phase3"] = Irms3;
serializeJson(doc, Serial);

I have a lot of Flows, but showing messages only from the one currently opened. Hoovering over it indicates the incoming MQTT node.

1. Is the MQTT out node correctly configured? (Is it pointing to same MQTT server as where Domoticz has subscribed to?)
Are there more MQTT servers?
Answer: There are more MQTT brokers, but I actually double checked now when you asked - it's the correct one :-)

2. Are the IDX numbers correct and do they match with the configured virtual sensors?
Answer: Yes, they are matching.

Please have a look at the attached debug message.

Best regards Richard
Attachments
MQTT_to_Domoticz_4.JPG
MQTT_to_Domoticz_4.JPG (47.61 KiB) Viewed 2023 times
User avatar
FireWizard
Posts: 1745
Joined: Tuesday 25 December 2018 12:11
Target OS: Raspberry Pi / ODroid
Domoticz version: Beta
Location: Voorthuizen (NL)
Contact:

Re: Get sensor values with MQTT into Domoticz [Solved]

Post by FireWizard »

Hello @rippe
Please have a look at the attached debug message.
I did and I was able to simulate the problem.
The problem is caused by the second message with a string, containing only a "carriage return" and this cannot be parsed by the MQTT input node.
This caused the message "Failed to parse JSON string"
In addition, I see also a "carriage return" at the end of the first string, but this one doesn't seem to cause a problem.

To prevent this extra string to give a fault message, we have to block the faulty string or allow the correct strings to pass.
I choose for the second option, as I'am not sure, if other faulty strings may occur.

To do so:

1. Change the Output in the MQTT input node from "a parsed JSON object" to "auto-detect (string or buffer)".
2. Add a "Switch" node after the MQTT input node as follows:

Screenshot_3fase-electra3.png
Screenshot_3fase-electra3.png (14.53 KiB) Viewed 2011 times
Filter on "contains" and "Phase
That means that only strings, that contains the substring "Phase" will pass.

3. Add a JSON node after the "Switch" node. (As we removed the parsing in the MQTT node, we have to do it now.)

The flow will look as follows:

Screenshot_3fase-electra4.png
Screenshot_3fase-electra4.png (17.75 KiB) Viewed 2011 times

Can you test this and report back?

Regards
Post Reply

Who is online

Users browsing this forum: No registered users and 0 guests