MQTT to Domoticz

Topics (not sure which fora)
when not sure where to post, post here and mods will move it to right forum.

Moderators: leecollings, remb0

Post Reply
Busmeikel
Posts: 23
Joined: Monday 19 December 2016 8:48
Target OS: Raspberry Pi / ODroid
Domoticz version: 2023.2
Location: Germany - Münsterland
Contact:

MQTT to Domoticz

Post by Busmeikel »

Hi Folks,
with less programming skills I'm totally lost with the explanations I found yet.

Can someone explain maybe step by step how I can get a MQTT-topic into Domoticz?

I have a watermeter publishing the total counterreading via MQTT, all is working fine. I can see the amount via Node red in the msg.payload/debug window inside Node red.
Bild_2021-04-21_185242.png
Bild_2021-04-21_185242.png (6.88 KiB) Viewed 937 times
Now the last step, getting it into Domoticz, is my mountain that I can't climb.

Regards
Meikel
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: MQTT to Domoticz

Post by FireWizard »

Hi, @Busmeikel

Can you publish your flow or a screenshot of what you see in your "Debug" window?

Regards
Busmeikel
Posts: 23
Joined: Monday 19 December 2016 8:48
Target OS: Raspberry Pi / ODroid
Domoticz version: 2023.2
Location: Germany - Münsterland
Contact:

Re: MQTT to Domoticz

Post by Busmeikel »

Hi FireWizard,
my "Debug" looks like that
Bild_2021-04-22_062103.png
Bild_2021-04-22_062103.png (34 KiB) Viewed 910 times
It is the value in m³, published every 5 minutes. It would be fine to have that counter in Domoticz and also a function maybe "Liter per day" or something like that.

Thanks for your reply.

Meikel
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: MQTT to Domoticz

Post by FireWizard »

Hello @Busmeikel

I assume that you have already Domoticz configured, so that Domoticz and the MQTT broker communicates.

If not, see: https://www.domoticz.com/wiki/MQTT

1. Create a new device in Domoticz ==> Hardware.
Type: Dummy (Does nothing, use for virtuals switches only)

2. Press "Create Virtual Sensors".
Select Sensor Type: "Counter" and give it a name (In this example I use "Water Usage")

3. Go to the device under the "Utility" tab.
Edit the device as follows:

Screenshot_water_usage3.png
Screenshot_water_usage3.png (25.93 KiB) Viewed 881 times
Make a note of the Idx:. In this case it is 12.

For this type of device the following command is required:
(source: https://piandmore.wordpress.com/2019/02 ... -domoticz/)
Counter

The counter sensor will show usage. You can add digits after the decimal if needed. The main display will show it rounded of to 2 digits while beneath it, it will show the whole number you upload. For some reason the log shows the value for every hour and that value is the value at the start of the hour.

{"command":"udevice", "idx":1234, "svalue":"3.45"}

The tricky part is that the type of counter determines what number you should upload:

Energy, upload per Wh, displayed in kWh
Gas, upload per 0.01 m3, displayed in m3
Water, upload per 10 l, display in l (and in m3 below the main display)
Counter, upload per 1 something, also displayed in the same format
Energy generated, upload per Wh, displayed in kWh
For water, the value, we have to upload is in 10L and so we have to multiply the received value with 100.

The flow looks as follows:

Screenshot_water_usage1.png
Screenshot_water_usage1.png (35.06 KiB) Viewed 881 times
The Function node "translates" the received input to the required Domoticz input.

The contents is as follows:

Code: Select all

msg.payload = {"command":"udevice", "idx":12, "svalue":(parseFloat(msg.payload) * 100).toFixed(1)}
return msg;
Brief explanation:

As you have seen above the Water sensor receives its value in the svalue variable.
But we have to calculate with the value.

1. The first function (parseFloat) converts the string ("131,638") to a Number.
2. This number is multiplied with 100
3. The result is formatted to 1 digit after the comma (toFixed(1))
4. This value is sent to Domoticz

See the "debug" part in the picture.

If everything is correct, you will see:

Screenshot_water_usage2.png
Screenshot_water_usage2.png (88.56 KiB) Viewed 881 times
You asked:
It is the value in m³, published every 5 minutes. It would be fine to have that counter in Domoticz and also a function maybe "Liter per day" or something like that.
The Liter per day, you will find in the Log files of the Sensor.

If you have available a flow meting, you can also use the other sensor (Currently Red, as it receives no data)

Regards
Last edited by FireWizard on Thursday 22 April 2021 19:49, edited 1 time in total.
Busmeikel
Posts: 23
Joined: Monday 19 December 2016 8:48
Target OS: Raspberry Pi / ODroid
Domoticz version: 2023.2
Location: Germany - Münsterland
Contact:

Re: MQTT to Domoticz

Post by Busmeikel »

Okay,
everything done and Domoticz is now unreachable, ERR_CONNECTION_REFUSED. What happened?

Before I use my safety copy and everything is at point zero. What can I do to bring Domoticz back to life? Reboot and cold start with no effect.

:|
Busmeikel
Posts: 23
Joined: Monday 19 December 2016 8:48
Target OS: Raspberry Pi / ODroid
Domoticz version: 2023.2
Location: Germany - Münsterland
Contact:

Re: MQTT to Domoticz

Post by Busmeikel »

...RasPi is still alive, cleared Browsercache...

No idea...
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: MQTT to Domoticz

Post by FireWizard »

@Busmeikel

Honestly I have no idea what has happened.

You followed exactly the steps? Used the same device?
And after you created the device, Domoticz has been working?

I can only think that because of an unexpected value the database has got corrupted?

Can you try to restore a previous database?

:shock:
Busmeikel
Posts: 23
Joined: Monday 19 December 2016 8:48
Target OS: Raspberry Pi / ODroid
Domoticz version: 2023.2
Location: Germany - Münsterland
Contact:

Re: MQTT to Domoticz

Post by Busmeikel »

Hi,
after reactivating Domoticz with my safety copy and updating to 2021.1 system works now. But...

It is not clear for me how to configure the "MQTT To Domoticz"-Node, when I put in the same topic of "MQTT Out"
it lasts in a closed loop and I have to delete the node and restart Node red.

The multiplier from m³ to Liters is 1000 :D

@FireWizard: Can you help with "MQTT To Domoticz"

Regards
Meikel
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: MQTT to Domoticz

Post by FireWizard »

Hello @Busmeikel
after reactivating Domoticz with my safety copy and updating to 2021.1 system works now. But...
Fortunately it works again. I broke almost my head, why this happened. It happened to me never before, that Domoticz crashed,
because of MQTT input. So I was afraid of an erroneous input in the database.
@FireWizard: Can you help with "MQTT To Domoticz"
Sure I can.

First:
The multiplier from m³ to Liters is 1000
I fully agree, your correct, but that is not the multiplier we need in the "Function" node.

The requirements for water in the "Counter" are that we have to send the value in 10 L units.

See:
Water, upload per 10 l, display in l (and in m3 below the main display)
But studying this issue, the data at piandmore may be outdated.

So let´s look to:
https://www.domoticz.com/wiki/Domoticz_API/JSON_URL%27s

I think it is easier to set the counter/divider in Domoticz: Setup ===> Settings ===> Meters/Counters ===> RFXMeter/Counter Dividers: ===> Water =1.
In this case we simple send the value of msg.payload to Domoticz.

This is, I think what you want:
When there is a counter created, there is a possibility to change the units by clicking on "Change" at the utility tab.

Energy (kWh)
Gas (m3)
Water (m3)
Counter (no unit)

The counter will be treated with the divider which is defined in the parameters in the application settings. For example if the counter is set to "Water" and the value is passed as liters, the divider must set to 1000 (as the unit is m3). The device displays 2 values:

The status is the overall total volume (or counter).
The volume (or counter) of the day (in the top right corner).

The today's volume (or counter) is calculated from the total volume (or counter).
The contents of the "Function" node:

Code: Select all

msg.payload = {"command":"udevice", "idx":11, "svalue":msg.payload}
return msg;
Remember to change the idx number (currently 11) to the idx number of your "Water Usage" device!
@FireWizard: Can you help with "MQTT To Domoticz"
Yes, I can imagine that it is a loop, because you publish the same value to the same topic as you did subscribe to.

You said:
It is not clear for me how to configure the "MQTT To Domoticz"-Node, when I put in the same topic of "MQTT Out"
it lasts in a closed loop and I have to delete the node and restart Node red.
Your topic is "wasserzaehler/zaehlerstand ". If you publish the final value to that same topic, it will go into a loop.

The topic should be changed to the topic that Domoticz has subscribed to.

Screenshot_MQTT topic.png
Screenshot_MQTT topic.png (45.6 KiB) Viewed 820 times
So that means that you have to publish to the same topic as you configured in Domoticz.
If you left the fields empty, you have to publish to domoticz/in.

Screenshot_MQTT to Domoticz.png
Screenshot_MQTT to Domoticz.png (21.81 KiB) Viewed 820 times
So try it and let´s hope we have more success now.

Regards
Busmeikel
Posts: 23
Joined: Monday 19 December 2016 8:48
Target OS: Raspberry Pi / ODroid
Domoticz version: 2023.2
Location: Germany - Münsterland
Contact:

Re: MQTT to Domoticz

Post by Busmeikel »

Hi FireWizard,
live could be so easy when you know how something works.

Now it is working well and I can go further with the next steps.

Thank you very much
Meikel
BlueMotion
Posts: 10
Joined: Thursday 04 June 2020 15:10
Target OS: NAS (Synology & others)
Domoticz version: 2020.2
Location: Subtropical Scandinavia (Fryslan)
Contact:

Re: MQTT to Domoticz

Post by BlueMotion »

Grrr it's getting frustrated

From NodeRed I want to send the daycount and the total count.
Somehow I only get or the daycount OR the total count.

Can someome help me with a correct msg.payload.svalue ??

My current msg.payload is: (value1 = daycount, value2 =total count

Code: Select all

 
 value = parseInt(msg.payload.COUNTER.C1);
 value2 = parseInt(global.get("WaterTotalCounter")/1000);
 
 msg.payload={};
 msg.payload.idx=134
 msg.payload.svalue= (value).toString() + ";" + (value2).ToString();
 return [msg];
 
User avatar
waltervl
Posts: 5149
Joined: Monday 28 January 2019 18:48
Target OS: Linux
Domoticz version: 2024.7
Location: NL
Contact:

Re: MQTT to Domoticz

Post by waltervl »

If you use the standard counter device https://www.domoticz.com/wiki/Domoticz_ ... 7s#Counter then you can only sent a total counter value. Domoticz will calculate the daily value for you as you will send this multiple time per day.
Domoticz running on Udoo X86 (on Ubuntu)
Devices/plugins: ZigbeeforDomoticz (with Xiaomi, Ikea, Tuya devices), Nefit Easy, Midea Airco, Omnik Solar, Goodwe Solar
BlueMotion
Posts: 10
Joined: Thursday 04 June 2020 15:10
Target OS: NAS (Synology & others)
Domoticz version: 2020.2
Location: Subtropical Scandinavia (Fryslan)
Contact:

Re: MQTT to Domoticz

Post by BlueMotion »

Okay. Thanks for the quick response.

But then again. On my previous sensor I had the offset filled in with the current state of the water meter. But every new day, the day counter was reset and my total count was the offset data. So not an actual total.

The only workaround is then to add another virtual sensor to push my totalcount. Or can I somehow misuse a managed counter?
User avatar
waltervl
Posts: 5149
Joined: Monday 28 January 2019 18:48
Target OS: Linux
Domoticz version: 2024.7
Location: NL
Contact:

Re: MQTT to Domoticz

Post by waltervl »

It all depends how your previous sensor was defined and how it was filled with the script/plugin you used. We cannot see that. Perhaps the script did the resetting.

The counter sensor works like you read every time your water meter and send the counter value on the water meter to Domoticz. If you do that every 5 min or every hour the counter will modify and Domoticz will calculate the used delta of today. And will store that into the database. At 0.00 hrs it will reset the daily usage to 0.

If your hardware sensor only counts and sends the number of rotations every delta of 5 min or hour you will have to use the incremental counter. That one I think needs a script to reset every day and update the offset. But I am no expert on this. What manual are you using to implement the water meter with MQTT?

Edit: It seems that the incremental counter works the same as a normal counter but instead of the total counter you send the delta. Domoticz will calculate the rest. So no manual/script reset necessary. You only need to set the offset once if you start measuring.
Domoticz running on Udoo X86 (on Ubuntu)
Devices/plugins: ZigbeeforDomoticz (with Xiaomi, Ikea, Tuya devices), Nefit Easy, Midea Airco, Omnik Solar, Goodwe Solar
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: MQTT to Domoticz

Post by FireWizard »

Hello @BlueMotion,

Just two questions:

1. Do you want to use the same widget (sensor), that is shown in my post of 22 Apr 2021, 16:59?

2. Where do you get the values:
value = parseInt(msg.payload.COUNTER.C1);
value2 = parseInt(global.get("WaterTotalCounter")/1000);
from?

Your current "function" does not look very good or is it only a part of a bigger function?

Regards
BlueMotion
Posts: 10
Joined: Thursday 04 June 2020 15:10
Target OS: NAS (Synology & others)
Domoticz version: 2020.2
Location: Subtropical Scandinavia (Fryslan)
Contact:

Re: MQTT to Domoticz

Post by BlueMotion »

1. Yes that was my plan. For now I created two sensors: One for DayCount and the other for TotalCount

2. Yes, just toke a small snapshot of the whole node-red flow. Just to explain that I have 2 values. A DayCount and a TotalCount
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: MQTT to Domoticz

Post by FireWizard »

hello @BlueMotion

You wrote:
1. Yes that was my plan. For now I created two sensors: One for DayCount and the other for TotalCount
I assume, you have already created the sensor, so that does not need further explanation.
Yes, just toke a small snapshot of the whole node-red flow. Just to explain that I have 2 values. A DayCount and a TotalCount
For now, we will prepare a "Function" node to format the command for Domoticz.
The output of this "Function" node has to be connected to the input of a MQTT Output node, with the topic domoticz/in.

I assume, you have a variable, called msg.payload.COUNTER.C1
I have no idea, whether this is a number or a string.
Let us assume, it is a number.
The IDX of the sensor is 134.

The contents of the "Function" node would become:

Code: Select all

msg.payload = {"command":"udevice","idx":134,"nvalue":0,"svalue":msg.payload.COUNTER.C1.toString();
return msg;
If the variable msg.payload.COUNTER.C1 is a string, we can omit the toString() function.
The contents will become:

Code: Select all

msg.payload = {"command":"udevice","idx":134,"nvalue":0,"svalue":msg.payload.COUNTER.C1;
return msg;
The selected widget will only accept one svalue and that should be a string.
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest