MQTT Function not writing to Device - Node-red to Domoticz [solved]

Everything about esp8266 and more.

Moderator: leecollings

Post Reply
Quasimodo
Posts: 3
Joined: Monday 18 October 2021 23:08
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

MQTT Function not writing to Device - Node-red to Domoticz [solved]

Post by Quasimodo »

I have been busy for two long days and I just can't seem to figure out why I can't get this to work :-(
I did (I think) exactly what was described in topic "Re: Get sensor values with MQTT into Domoticz"

Is there anyone who has an idea maybe?

Goal:
To get MQTT values from my second Raspberry Pi to my first RBPi Dummy Device via/with domoticz ánd mosquito ánd node-red.

Situation:
I have installed and running:
domoticz ánd mosquito ánd node-red on my Raspberry Pi 3B.
With "mosquitto_sub -v -t '#' " I do see all messages coming by via putty (see bottom)

Problem:
All goes fine exépt for the data to be updated in the domoticz-Device (idx).
Image

Situation/what I have:
MQTT Hardware set in domotics with these settings:
Data Timeout: Disabled
Remote Address: localhost
Port 1883
Publish Topic: Index

A Dummy device created with name: GAS, type: gas, Idx: 289

Node-Red:
MQTT object, with the following settings:
Topic: p1monitor/smartmeter/consumption_gas_m3
Output: a parsed JSON object
Server: localhost : 1883
Client ID: p1monitor

JSON node:
Action: Convert between JSON string & object

Function:
switch (msg.topic) {
case "p1monitor/smartmeter/consumption_gas_m3":

msg.payload = {"command":"udevice","idx":289,"nvalue":0,"svalue": msg.payload};
}
return msg;

* Note: I also changed this to:
msg.payload = {"command":"udevice","idx":289,"nvalue":0,"svalue": 40 };
and
msg.payload = {"command":"udevice","idx":289,"nvalue":0,"svalue": "40"};

but that makes no differtence: the device won't update with the given value...

Debug output:

Code: Select all

[color=#404000]10/18/2021, 11:32:06 PMnode: 2a8a6f12fe8314fc
p1monitor/smartmeter/consumption_gas_m3 : msg.payload : Object
object
command: "udevice"
idx: 289
nvalue: 0
svalue: "40"[/color]
... repeatingly...
mosquitto_sub -v -t '#'
pi@raspberrypi:~ $ mosquitto_sub -v -t '#'
p1monitor/smartmeter/timestamp_local 2021-10-18 23:34:13
p1monitor/smartmeter/timestamp_utc 1634592853
p1monitor/smartmeter/consumption_gas_m3 1389.693
p1monitor/smartmeter/consumption_kwh_high 1656.412
p1monitor/smartmeter/consumption_kwh_low 1597.443
p1monitor/smartmeter/consumption_kw 0.555
p1monitor/smartmeter/production_kwh_high 1583.278
p1monitor/smartmeter/production_kwh_low 657.426
p1monitor/smartmeter/production_kw 0.0
p1monitor/smartmeter/tarifcode D
p1monitor/smartmeter/record_is_processed 0
p1monitor/phase/timestamp_local 2021-10-18 23:34:13
p1monitor/phase/timestamp_utc 1634592853
p1monitor/phase/consumption_l1_w 0.547
p1monitor/phase/consumption_l2_w 0.0
p1monitor/phase/consumption_l3_w 0.0
p1monitor/phase/production_l1_w 0.0
p1monitor/phase/production_l2_w 0.0
p1monitor/phase/production_l3_w 0.0
p1monitor/phase/l1_v 235.2
p1monitor/phase/l2_v 0.0
p1monitor/phase/l3_v 0.0
p1monitor/phase/l1_a 2.0
p1monitor/phase/l2_a 0.0
p1monitor/phase/l3_a 0.0


It is almost as if node-red misses some kind of package responsible for communicating with domoticz itself...

Any suggestion someone maybe?
Attachments
Image04.png
Image04.png (13.36 KiB) Viewed 3092 times
Image03.png
Image03.png (5.76 KiB) Viewed 3092 times
Image02.png
Image02.png (9.46 KiB) Viewed 3092 times
Last edited by Quasimodo on Wednesday 20 October 2021 23:39, edited 2 times in total.
User avatar
FireWizard
Posts: 1770
Joined: Tuesday 25 December 2018 12:11
Target OS: Raspberry Pi / ODroid
Domoticz version: Beta
Location: Voorthuizen (NL)
Contact:

Re: MQTT Function not writing to Device - Node-red to Domoticz

Post by FireWizard »

Hello @Quasimodo,

If I look to your screenshot with the Domoticz flow I see at the very left a MQTT input node called Domoticz In.
Does this mean that the topic is also domoticz/in? If so, it's not correct.

I do not see any MQTT output node.

Remember:

Domoticz publishes by default to domoticz/out and subscribes by default to domoticz/in.

That means that the MQTT input node should use as topic domoticz/out and the MQTT output node should have as topic domoticz/in.

I also see that the message is correct, but the topic is still wrong (p1monitor/smartmeter/consumption_gas_m3).

As you do not send anything to Domoticz, you have the red bar at the top, because the sensor timed out.

Can you publish your flow?

Regards
Last edited by FireWizard on Tuesday 19 October 2021 23:48, edited 1 time in total.
Quasimodo
Posts: 3
Joined: Monday 18 October 2021 23:08
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: MQTT Function not writing to Device - Node-red to Domoticz

Post by Quasimodo »

Firewizard,

I think you might be right!
I cannot test it anymore, since I decided to swap my Raspberry(1) for my Raspberry(2) (meaning now my
new bought raspberry(2) is directly attached to my smart meter P1).

In the above situation I got messages from RBPi-1, which was directly connected to the smart meter.
So in my RBPi-2 the above is what I saw, and now that you mention, I see that the object was pushed to:
"p1monitor/smartmeter/consumption_gas_m3"
And I assume that has to be "domoticz/in" indeed....

By the way, I got things working now directly in domoticz on my RBPi(2) :-) So all happy :-)
With using dzVents scripting :-) Quite new to that but I start understanding it more and more.
And once you get it.... a whole world of opportunities open up :D Really enthusiastic :-)

Thanks for the help in this case. Still very usefull in case I go back to the old situation.... 👍👍👍
Quasimodo
Posts: 3
Joined: Monday 18 October 2021 23:08
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: MQTT Function not writing to Device - Node-red to Domoticz

Post by Quasimodo »

Firewizard,

I figured it out :-D
For personal reasons I had to go back to the old situation...

The problem was within what you described, so I needed a MQTT out so to say.
And the second problem was:

This was my function:

Code: Select all

switch (msg.topic) {
case "p1monitor/smartmeter/consumption_gas_m3":

msg.payload = {"command":"udevice","idx":289,"nvalue":0,"svalue": msg.payload};
}
return msg;
And this is how it should have been (and now is...):

Code: Select all

switch (msg.topic) {
case "p1monitor/smartmeter/consumption_gas_m3":

  msg.payload = JSON.stringify(msg.payload);
  msg.payload = {"idx": 10, "svalue": msg.payload };
}
return msg;
That simple...
Now it works like charm :-)

For those beginners like me who would like to use this as an example, just import:

Code: Select all

[{"id":"b85a8c34171f16dc","type":"tab","label":"Flow 1","disabled":false,"info":""},{"id":"fad68e83d9be2461","type":"mqtt in","z":"b85a8c34171f16dc","name":"MQTT-in","topic":"p1monitor/smartmeter/consumption_gas_m3","qos":"2","datatype":"json","broker":"3c204f72949242d0","nl":false,"rap":true,"rh":0,"x":160,"y":240,"wires":[["fe34caedf1732919","6e3e4c3a6bb3be80"]]},{"id":"fe34caedf1732919","type":"function","z":"b85a8c34171f16dc","name":"Get Payload","func":"switch (msg.topic) {\ncase \"p1monitor/smartmeter/consumption_gas_m3\":\n\n  msg.payload = JSON.stringify(msg.payload);\n  msg.payload = {\"idx\": 10, \"svalue\": msg.payload };\n}\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":450,"y":240,"wires":[["5360b193b9a49482","1d72d2a3bd837bca"]]},{"id":"5360b193b9a49482","type":"mqtt out","z":"b85a8c34171f16dc","name":"MQTT-out","topic":"domoticz/in","qos":"2","retain":"","respTopic":"","contentType":"","userProps":"","correl":"","expiry":"","broker":"3c204f72949242d0","x":710,"y":240,"wires":[]},{"id":"6e3e4c3a6bb3be80","type":"debug","z":"b85a8c34171f16dc","name":"","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":360,"y":120,"wires":[]},{"id":"1d72d2a3bd837bca","type":"debug","z":"b85a8c34171f16dc","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":610,"y":120,"wires":[]},{"id":"3c204f72949242d0","type":"mqtt-broker","name":"","broker":"localhost","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":""}]
Enjoy :-)
User avatar
FireWizard
Posts: 1770
Joined: Tuesday 25 December 2018 12:11
Target OS: Raspberry Pi / ODroid
Domoticz version: Beta
Location: Voorthuizen (NL)
Contact:

Re: MQTT Function not writing to Device - Node-red to Domoticz [solved]

Post by FireWizard »

Hello @Quasimodo,

Please allow me to add a few things to your solution.

The question has often been raised, what is better, to use multiple if-then-else statements or the switch-case statement.

I do not present an opinion here, but you might want to read: https://www.geeksforgeeks.org/switch-vs-else/ and also https://www.javatpoint.com/if-else-vs-switch

In general, it is the opinion that in case of 5 or more nested if-then-else statements, a switch-case is faster.

I prefer also a switch-case statement, because of its readability

So you can also use in your "Function" node, the following code:

Code: Select all

if (msg.topic == "p1monitor/smartmeter/consumption_gas_m3") {
    msg.payload = {"idx": 10, "svalue": msg.payload.toString()};
    return msg;
}
I thought this remark would offer something to choose from.

Regards
Post Reply

Who is online

Users browsing this forum: No registered users and 0 guests