Push sensor data to remote Domoticz

Moderator: leecollings

Post Reply
ronaldbro
Posts: 327
Joined: Thursday 15 November 2018 21:38
Target OS: Raspberry Pi / ODroid
Domoticz version: 2020.2
Location: Netherlands
Contact:

Push sensor data to remote Domoticz

Post by ronaldbro »

I have some Bluetooth plant sensors which are out of range of my Domoticz Raspberry Pi so decided to buy a new Pi as a slave. But after adding the plant sensors to my secondary Domoticz I found out that the standard Remote Server doesn't work for dummy sensors. I found some solutions on the forum using MQTT and node red, but I wanted a simple solution. So I created a generic script to push updates to the primary Domoticz using json.

It works very simple.
1) Create the sensor on the slave using the dummy device or a plugin.
2) Create the same sensor on the master using the dummy device. Make sure you create the same type.
3) Copy this script to the slave
4) Change the URL to the master
5) Edit the mapping table in the script. [slave idx] = {remoteIdx = 'master idx'}
6) Now as soon as the sensor on the slave get's an update the value is pushed to the master.

In the script below only the sensor types used by the Xiaomi Mi Flower Mate are added, but it can be easily extended for other sensor types. Just add an elseif for the correct type/subtype and make sure to use the correct json found here https://www.domoticz.com/wiki/Domoticz ... .2Fsensors

Code: Select all

 local server = 'http://192.168.2.10:8080'

local MAPPING = {
    [56] = {remoteIdx =  999}, -- Plant 1: Vocht
    [22] = {remoteIdx =  703}, -- Plant 1: Temperatuur
    [23] = {remoteIdx =  704}, -- Plant 1: Lucht
    [24] = {remoteIdx =  705}, -- Plant 1: Voeding

    [57] = {remoteIdx = 1000}, -- Plant 2: Vocht
    [26] = {remoteIdx =  707}, -- Plant 2: Temperatuur
    [27] = {remoteIdx =  708}, -- Plant 2: Lucht
    [28] = {remoteIdx =  709}, -- Plant 2: Voeding

    [58] = {remoteIdx = 1001}, -- Plant 3: Vocht
    [30] = {remoteIdx =  711}, -- Plant 3: Temperatuur
    [31] = {remoteIdx =  712}, -- Plant 3: Lucht
    [32] = {remoteIdx =  713}, -- Plant 3: Voeding

    [59] = {remoteIdx = 1002}, -- Plant 4: Vocht
    [34] = {remoteIdx =  715}, -- Plant 4: Temperatuur
    [35] = {remoteIdx =  716}, -- Plant 4: Lucht
    [36] = {remoteIdx =  717}, -- Plant 4: Voeding

    [60] = {remoteIdx = 1003}, -- Plant 5: Vocht
    [53] = {remoteIdx =  719}, -- Plant 5: Temperatuur
    [54] = {remoteIdx =  720}, -- Plant 5: Lucht
    [55] = {remoteIdx =  721}, -- Plant 5: Voeding

    [61] = {remoteIdx = 1004}, -- Plant 6: Vocht
    [48] = {remoteIdx =  723}, -- Plant 6: Temperatuur
    [49] = {remoteIdx =  724}, -- Plant 6: Lucht
    [50] = {remoteIdx =  725}, -- Plant 6: Voeding
}

---------- Script starts here ----------


local callbackString = 'RemoteDataPush_callback'

local getTriggerDevices = function()
	local tDevs = {}
	for idx, _ in pairs(MAPPING) do
		tDevs[idx] = idx
	end
	return(tDevs)
end

return {

	on = {
		devices = getTriggerDevices(),
        httpResponses = { callbackString }
	},

	logging = {
--        level = domoticz.LOG_DEBUG,
        level = domoticz.LOG_INFO,
--        level = domoticz.LOG_ERROR,
        marker = "Remote Data Push"
    },

	execute = function(dz, item, info)

        if item.isDevice then
            dz.log('Send data to ' .. server .. ' - local idx = ' .. item.idx .. ' - remote idx = ' .. MAPPING[item.idx].remoteIdx ..' - deviceType = ' .. item.deviceType .. ' - deviceSubType = ' .. item.deviceSubType .. ' - nvalue = ' .. item.nValue .. ' - svalue = ' .. item.sValue)
            if item.deviceType == 'General' and item.deviceSubType == 'Custom Sensor' then
                dz.openURL({url = server .. '/json.htm?type=command&param=udevice&idx=' .. MAPPING[item.idx].remoteIdx ..'&nvalue=0&svalue=' .. item.sValue, callback = callbackString})
            elseif item.deviceType == 'General' and item.deviceSubType == 'Soil Moisture' then
                dz.openURL({url = server .. '/json.htm?type=command&param=udevice&idx=' .. MAPPING[item.idx].remoteIdx ..'&nvalue=' .. item.nValue, callback = callbackString})
            elseif item.deviceType == 'General' and item.deviceSubType == 'Percentage' then
                dz.openURL({url = server .. '/json.htm?type=command&param=udevice&idx=' .. MAPPING[item.idx].remoteIdx ..'&nvalue=0&svalue=' .. item.nValue, callback = callbackString})
            elseif item.deviceType == 'Lux' and item.deviceSubType == 'Lux' then
                dz.openURL({url = server .. '/json.htm?type=command&param=udevice&idx=' .. MAPPING[item.idx].remoteIdx ..'&svalue=' .. item.sValue, callback = callbackString})
            elseif item.deviceType == 'Temp' and item.deviceSubType == 'LaCrosse TX3' then
                dz.openURL({url = server .. '/json.htm?type=command&param=udevice&idx=' .. MAPPING[item.idx].remoteIdx ..'&nvalue=0&svalue=' .. item.sValue, callback = callbackString})
            elseif item.deviceType == 'Humidity' and item.deviceSubType == 'LaCrosse TX3' then
                dz.openURL({url = server .. '/json.htm?type=command&param=udevice&idx=' .. MAPPING[item.idx].remoteIdx ..'&nvalue=' .. item.nValue .. '&svalue=' .. item.sValue, callback = callbackString})
            else
                dz.log('Not implemented devicetype found. Devicetype = ' .. item.deviceType .. ' subtype = ' .. item.deviceSubType, dz.LOG_ERROR)
            end
        elseif item.isHTTPResponse then
            if item.json.status == 'OK' then
                dz.log('Updated the remote device succesfully.')
                dz.log(item.json)
            else
                dz.log('Something went wrong updating the remote device', dz.LOG_ERROR)
                dz.log(item.json, dz.LOG_ERROR)
            end
        else
            dz.log('Unknown script trigger.', dz.LOG_ERROR)
        end        
	end
}
Last edited by ronaldbro on Sunday 07 June 2020 14:59, edited 6 times in total.
manjh
Posts: 749
Joined: Saturday 27 February 2016 12:49
Target OS: Raspberry Pi / ODroid
Domoticz version: 2020.2
Location: NL
Contact:

Re: Push sensor data to remote Domoticz

Post by manjh »

Very creative solution. There is one downside to this that I can see: if the master and slave are not in the same subnet, you will have to do a port-forward on the master network for 8080. Not something I look forward to.
Once this port is open, the master Domoticz is open for anything.
With the MQTT solution there is a filter on both sides: on the slave side you explicitely have to decide which devices are sent out on the queue. On the master side, there is a similar filter where you explicitely decide which updates are accepted from the queue.
Effectively this means that a really smart hacker could break in and send me fake updates to the devices in my replication list. Unless the device is critical (e.g. an alarm trigger) this seems like an acceptable risk.
Hans
ray308
Posts: 6
Joined: Thursday 02 January 2020 20:08
Target OS: NAS (Synology & others)
Domoticz version:
Contact:

Re: Push sensor data to remote Domoticz

Post by ray308 »

@ronaldbro thanks for this idea, I was strugling with the dummy's for a while to get them on my remote server.
Can you tell me where to put the script?

I did put in as an event (DzVents) but it returns an error. I assume that the script has to be somewhere else?

I only use the custom sensor to try this out.
ronaldbro
Posts: 327
Joined: Thursday 15 November 2018 21:38
Target OS: Raspberry Pi / ODroid
Domoticz version: 2020.2
Location: Netherlands
Contact:

Re: Push sensor data to remote Domoticz

Post by ronaldbro »

You can copy paste it to a new dzVents script in Domoticz. Just create a new script, delete everything and paste this one.
Can you post the error?
ray308
Posts: 6
Joined: Thursday 02 January 2020 20:08
Target OS: NAS (Synology & others)
Domoticz version:
Contact:

Re: Push sensor data to remote Domoticz

Post by ray308 »

Just created a new dzVents script on the slave, deleted everything, and pasted your script in it.
Offcource with my own server ip/port en the correct idx numbers.

No errors in the log now, but no update either on the master, any ideas?
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Push sensor data to remote Domoticz

Post by waaren »

ray308 wrote: Saturday 06 June 2020 11:38 Just created a new dzVents script on the slave, deleted everything, and pasted your script in it.
Offcource with my own server ip/port en the correct idx numbers.

No errors in the log now, but no update either on the master, any ideas?
What are your settings in
settings.png
settings.png (13.51 KiB) Viewed 2985 times
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
ray308
Posts: 6
Joined: Thursday 02 January 2020 20:08
Target OS: NAS (Synology & others)
Domoticz version:
Contact:

Re: Push sensor data to remote Domoticz

Post by ray308 »

10.0.1. is my local lan, both devices are in there.
Attachments
Schermafbeelding 2020-06-06 om 12.08.33.png
Schermafbeelding 2020-06-06 om 12.08.33.png (19.08 KiB) Viewed 2985 times
ronaldbro
Posts: 327
Joined: Thursday 15 November 2018 21:38
Target OS: Raspberry Pi / ODroid
Domoticz version: 2020.2
Location: Netherlands
Contact:

Re: Push sensor data to remote Domoticz

Post by ronaldbro »

Have you set an admin password? I thought even if you don’t need it it must be set to use json calls.
ronaldbro
Posts: 327
Joined: Thursday 15 November 2018 21:38
Target OS: Raspberry Pi / ODroid
Domoticz version: 2020.2
Location: Netherlands
Contact:

Re: Push sensor data to remote Domoticz

Post by ronaldbro »

Another possibility... Do you have dzVents enabled On your slave in settings -> other?
Do you see any action on the slave in the log when a value of the configured devices changes?
ray308
Posts: 6
Joined: Thursday 02 January 2020 20:08
Target OS: NAS (Synology & others)
Domoticz version:
Contact:

Re: Push sensor data to remote Domoticz

Post by ray308 »

Yep, I so have an admin password, and dzVents is enabled in settings, Im running more scriptst, fot weather and my watermeter. Just checked if it was really enabled.

I.ll check al the settings again..
ronaldbro
Posts: 327
Joined: Thursday 15 November 2018 21:38
Target OS: Raspberry Pi / ODroid
Domoticz version: 2020.2
Location: Netherlands
Contact:

Re: Push sensor data to remote Domoticz

Post by ronaldbro »

It would be interesting to know if the problem is on the master side or on the slave side. If you set the lof level in the script to info then you should see something in the log when the devices gets updated.
ronaldbro
Posts: 327
Joined: Thursday 15 November 2018 21:38
Target OS: Raspberry Pi / ODroid
Domoticz version: 2020.2
Location: Netherlands
Contact:

Re: Push sensor data to remote Domoticz

Post by ronaldbro »

I just updated the script to give an error when a DeviceType is not implemented yet, this might be the cause...
To update you can just copy/paste everything below the device mapping.
ray308
Posts: 6
Joined: Thursday 02 January 2020 20:08
Target OS: NAS (Synology & others)
Domoticz version:
Contact:

Re: Push sensor data to remote Domoticz

Post by ray308 »

Thx, just implemented the new script, no change in the log, just that the script started.


My dummy devices are getting there info from another script (weather script) is this maybe the problem?
ronaldbro
Posts: 327
Joined: Thursday 15 November 2018 21:38
Target OS: Raspberry Pi / ODroid
Domoticz version: 2020.2
Location: Netherlands
Contact:

Re: Push sensor data to remote Domoticz

Post by ronaldbro »

If the script is starting then it should work.
I added some error logging to the script and also checking the result. Can you update the script and post the log?
ray308
Posts: 6
Joined: Thursday 02 January 2020 20:08
Target OS: NAS (Synology & others)
Domoticz version:
Contact:

Re: Push sensor data to remote Domoticz

Post by ray308 »

I think it only saves the scripts, that is what I see in the log. I think the script is not running at al. Is there a way to check this?
ronaldbro
Posts: 327
Joined: Thursday 15 November 2018 21:38
Target OS: Raspberry Pi / ODroid
Domoticz version: 2020.2
Location: Netherlands
Contact:

Re: Push sensor data to remote Domoticz

Post by ronaldbro »

You should see log messages if the script is called. Can you post your device mapping you have in your script?
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest