Page 1 of 1
LUA-script for updating sensors by MQTT
Posted: Friday 10 January 2020 20:52
by Kouwe
I use both Home Assistant and Domoticz. Not all Xiaomi plant sensors can be reached from Domoticz. So I want to use the HA, which is in a different place to make the last 2 sensors also available in Domoticz. For this I use Node Red and MQTT. I get the data in the Pi on which Domoticz is running. I only know too little about the technology to update the devices in Domoticz with the value of the sensors. Does anyone have an idea? Can you do that with a "simple" Lua-script. I have a separate message 'Dracena' and 'Palm' for both sensors.
Re: LUA-script for updating sensors by MQTT
Posted: Saturday 11 January 2020 8:27
by waaren
Kouwe wrote: ↑Friday 10 January 2020 20:52
.... For this I use Node Red and MQTT. I get the data in the Pi on which Domoticz is running....
Domoticz can be set to listen to MQTT topic "domoticz/in" already so it could be that you don't need another script.
What type / subtype are the domoticz devices ? Which information do you already send to the PI and how ?
Re: LUA-script for updating sensors by MQTT
Posted: Saturday 11 January 2020 11:10
by Kouwe
At this time, only the value of the sensor indicating the percentage of humidity. For this purpose I created a device (from hardware dummy) of type general, percentage (humidity).
Once this works, I want to use more, because I've discovered that with the addition 'Nora' to Node-red, I can also use Google Assistant without having to switch to Controlz.
For everyone who is also interested, here's the link:
https://flows.nodered.org/node/node-red-contrib-nora .
Please share interesting experiences here on the forum.
Re: LUA-script for updating sensors by MQTT
Posted: Saturday 11 January 2020 13:25
by waaren
Kouwe wrote: ↑Saturday 11 January 2020 11:10
At this time, only the value of the sensor indicating the percentage of humidity. For this purpose I created a device (from hardware dummy) of type general, percentage (humidity).
Please find
the wiki on how to add MQTT to domoticz
For virtual device humidity the command to send to domoticz is:
Code: Select all
mosquitto_pub -m '{"idx":45,"nvalue":53,"svalue":"3","Battery":66}' -t domoticz/in
For virtual device percentage the command to send to domoticz is:
Code: Select all
mosquitto_pub -m '{"idx":46,"nvalue":0,"svalue":"53"}' -t domoticz/in
Re: LUA-script for updating sensors by MQTT
Posted: Saturday 11 January 2020 13:34
by Kouwe
Maybe this helps someone to give me clues. I've downloaded the source from Node-Red. Please notice that there is still an ERROR in this code. I want the actual message (namely the correct humility) to be in the message sent to Domoticz to adjust the value. I already discovered that Node-Red is very powerful once you know how to deal with it.
Code: Select all
[{"id":"67fa066b.31251","type":"mqtt out","z":"75ef81a3.3f27a","name":"","topic":"domoticz/in","qos":"2","retain":"true","broker":"51489f0e.3dc0e","x":590,"y":100,"wires":[]},{"id":"51489f0e.3dc0e","type":"mqtt-broker","z":"","name":"","broker":"192.168.0.130","port":"1883","clientid":"","usetls":false,"compatmode":false,"keepalive":"60","cleansession":true,"birthTopic":"","birthQos":"0","birthPayload":"","closeTopic":"","closeQos":"0","closePayload":"","willTopic":"","willQos":"0","willPayload":""}]
To use the above. Copy it and use the import-function of Node-Red. It will deliver my flow in a useful and readable form.
Re: LUA-script for updating sensors by MQTT
Posted: Saturday 11 January 2020 14:08
by FireWizard
Hi,
I do not want to interfere, with the good support waaren is trying to give you, but we need more info.
The code you posted is not a flow, but actually only a MQTT output node, configured for domoticz/in, including its (hidden) configuration node.
In order to make it work, you have to create a input to that MQTT output node, containing exactly the data for humidity and percentage with the syntax, as shown in waarens post. Probably, you will need some kind of conversion.
Therefore can you tell us, how your devices provide the data and how that data looks like.
I suggest that you install the following node in node-red:
https://flows.nodered.org/node/node-red ... xiaomi-ble.
This node can hopefully import the desired data into node-red.
Connect a debug node to that xiaomi node and check the result in the debug pane.
[Edit] Look also here:
https://flows.nodered.org/flow/b1af1d00 ... f025d33feb
Is it published at /home/miflora?
Regards
Re: LUA-script for updating sensors by MQTT
Posted: Saturday 11 January 2020 15:03
by Kouwe
You're not getting the point. I almost got it working. When I use my own data domoticz gets updated. But until now I'm not able to get in the data from HA. Only because I don't know how to filter the value from the message from HA. I fill it know with idx, nvalue, and svalue. I can even update switches.
Re: LUA-script for updating sensors by MQTT
Posted: Saturday 11 January 2020 15:28
by Kouwe
I already solved it. I don't need LUA. It works perfectly within Node-Red. I only oversaw my mistake. This solves the problem in the function-node:
Code: Select all
var moist = msg.payload
msg.payload = {};
msg.payload.idx = 1036;
msg.payload.nvalue = 0;
msg.payload.svalue = moist;
return msg;
I had to save the data of msg.payload first in a local variable. After msg.payload the data is no longer available. I do can recommend Node-Red It opens a world to new opportunities