Copy of device

Python and python framework

Moderator: leecollings

Post Reply
Toulon7559
Posts: 859
Joined: Sunday 23 February 2014 17:56
Target OS: Raspberry Pi / ODroid
Domoticz version: <2025
Location: Hengelo(Ov)/NL
Contact:

Copy of device

Post by Toulon7559 »

Have several Raspberries each with specific devices in the configuration.
Sometimes data from such specific device needed elsewhere.

Have a Python script-solution which reads a selected device by JSON-call & dissects & recompiles as required, e.g. to feed a new Virtual Device for further application, etc..
Rather laboured, especially for Virtual Devices with multiple-field svalues .....

;) ;-) Getting lazy, looking for a quicker & more direct way.
Is it possible to simply online 'copy' or 'link' a device in one Domoticz instance to an equivalent device in another Domoticz-instance?
;-) Any solution is OK, which is shorter & simpler than described in above section 2 of this message.

Or (another way of dealing with this aspect end-to-end):
how remotely reading specific data from a Device at another Domoticz-instance?
Making JSON-call is easy enough, but what is further shortest route to 'dissect' the JSON-file and put specific, selected data into a user variable?
Set1 = RPI-Zero+RFXCom433+S0PCM+Shield for BMP180/DS18B20/RS485+DDS238-1ZNs
Set2 = RPI-3A++RFLinkGTW+ESP8266s+PWS_WS7000
Common = KAKUs+3*PVLogger+PWS_TFA_Nexus
plus series of 'satellites' for dedicated interfacing, monitoring & control.
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Copy of device

Post by waaren »

Toulon7559 wrote: Wednesday 22 May 2019 6:10 ...Rather laboured, especially for Virtual Devices with multiple-field svalues .....
As a possible first step, this dzVents script generates a json file with a selection of attributes of a set of selected devices. This file can be red by a separate script on the remote location and processed.

Code: Select all

-- getDevices.lua (dzVents >= 2.4.21)

scriptVar = getDevices

return {
    on      =   {   timer =   { "at *:03" }, -- Triggers the getJsonPart
                    devices = { "XB*" }},  -- I use this a test / debug trigger
                
    logging =   {   level     =   domoticz.LOG_DEBUG,
                    marker    =   scriptVar 
                },
        
    execute = function(dz, item)

        local idx = {35,133,486, "YoulessLux", 1987 } -- "names" or idx of your devices
        local outFile = "/tmp/dzOut.json"
        local devices = {}

        local function write2File(data, file)
            local out = assert(io.open(file,"w"))
            out:write(data)
            out:close()
        end

        table.insert(devices, {created= (dz.time.rawDate .. " " .. dz.time.rawTime) })
        dz.devices().filter(idx).forEach(function(dv)
            dz.log("Found: " .. dv.name .. "; nValue=" .. dv.nValue .. "; sValue=" .. dv.sValue,dz.LOG_DEBUG)
            local t = { idx=dv.idx, name=dv.name, nValue=dv.nValue, sValue=dv.sValue} -- use dz.setValues to copy the sValue to another device
            table.insert(devices, t)
        end)

        dz.utils.dumpTable(devices)
        write2File(dz.utils.toJSON(devices),outFile)
    end
}
Spoiler: show
cat /tmp/dzOut.json
[{"created":"2019-05-22 16:29:14"},{"idx":35,"nValue":0,"name":"Power","sValue":"7544687;5477757;3243875;7380936;0;2444"},{"idx":133,"nValue":0,"name":"Youless","sValue":"16142878;3152"},{"idx":486,"nValue":0,"name":"Vloer verw","sValue":"19.5;16.5;AutoWithEco"},{"idx":964,"nValue":0,"name":"YoulessLux","sValue":"3729.0833333333"},{"idx":1987,"nValue":0,"name":"Airplanes","sValue":"4227"}]
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
Toulon7559
Posts: 859
Joined: Sunday 23 February 2014 17:56
Target OS: Raspberry Pi / ODroid
Domoticz version: <2025
Location: Hengelo(Ov)/NL
Contact:

Re: Copy of device

Post by Toulon7559 »

@waaren

;-) 'Laboured' in my understanding is already something like the Python script below.
This script reads from one Domoticz, and recompiles & uploads to another Domoticz.
In that perspective I am looking for a very short script preferably without the recompilation effort .............

Code: Select all

#!/usr/bin/python
# -*- coding = utf-8 to enable reading by simple editors -*-
# (c)2019 script compiled by Toulon7559 from various material from forums, version 00_190522
# --------------------------------------------------
# Line005 = PREPARATION & SETTING
# --------------------------------------------------
# Imports for script-operation
import json
import urllib

# --------------------------------------------------
# Line012 = EXTRACT CONTENT BY JSON-CALL to Domoticz1
# --------------------------------------------------
# RID=9 is RFXCOM-sensor for Wind from TFA_Nexus
page = urllib.urlopen('http://192.168.1.5:8080/json.htm?type=devices&rid=9')
content_test = page.read()
obj_test = json.loads(content_test)
# --------------------------------------------------
# Line019 = Extraction of components
# --------------------------------------------------
# JSON-call for update: /json.htm?type=command&param=udevice&idx=IDX&nvalue=0&svalue=WB;WD;WS;WG;Temp;Temp_WC
print(obj_test)
WB = obj_test['result'][0]['Direction'] 
WD = obj_test['result'][0]['DirectionStr']
WS = obj_test['result'][0]['Speed'] 
WG = obj_test['result'][0]['Gust']
Temp = obj_test['result'][0]['Temp'] 
Temp_WC = obj_test['result'][0]['Chill']
# -------------------------------------------------
# Line030 = Upload to  Virtual Device at Domoticz2
# -------------------------------------------------
#domoticz2 settings
domoticz2_host      	= '192.168.1.6'
domoticz2_port      	= '8080'
domoticz2_url       	= 'json.htm'
domoticz2_idx           = '3305' #idx of new device

#uploading values to domoticz2
url = ("http://" + domoticz2_host + ":" + domoticz2_port + "/" + domoticz2_url+ "?type=command&param=udevice&idx=" + domoticz2_idx + "&nvalue=0&svalue=" + str(WB) + ";" + str(WD) + ";" + str(WS) + ";" + str(WG) + ";" + str(Temp) + ";" + str(Temp_WC))
urllib.urlopen(url)
Set1 = RPI-Zero+RFXCom433+S0PCM+Shield for BMP180/DS18B20/RS485+DDS238-1ZNs
Set2 = RPI-3A++RFLinkGTW+ESP8266s+PWS_WS7000
Common = KAKUs+3*PVLogger+PWS_TFA_Nexus
plus series of 'satellites' for dedicated interfacing, monitoring & control.
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Copy of device

Post by waaren »

Toulon7559 wrote: Wednesday 22 May 2019 20:44 @waaren
;-) 'Laboured' in my understanding is already something like the Python script .
Sorry but I don't understand your point. Please elaborate.

The idea behind the script I posted is that you have the nValue and sValue. With these values you don't have to split the results any further. You can simply use the dzVents method setValue to fill the nValue and sValue of the copy device. For the majority of the devices this is enough and for the remaining devices the JSON can easily be extended to include the missing parts.
The advantage of this approach is that you can include as many devices as you want.
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
Toulon7559
Posts: 859
Joined: Sunday 23 February 2014 17:56
Target OS: Raspberry Pi / ODroid
Domoticz version: <2025
Location: Hengelo(Ov)/NL
Contact:

Re: Copy of device

Post by Toulon7559 »

@waaren

Right!
Your dzVents-script is a suitable solution for my question.
;-) But have a personal preference for Python-scripting ...........
Set1 = RPI-Zero+RFXCom433+S0PCM+Shield for BMP180/DS18B20/RS485+DDS238-1ZNs
Set2 = RPI-3A++RFLinkGTW+ESP8266s+PWS_WS7000
Common = KAKUs+3*PVLogger+PWS_TFA_Nexus
plus series of 'satellites' for dedicated interfacing, monitoring & control.
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Copy of device

Post by waaren »

Toulon7559 wrote: Thursday 23 May 2019 11:29 @waaren

Right!
Your dzVents-script is a suitable solution for my question.
;-) But have a personal preference for Python-scripting ...........
@Toulon7559, understand. That's one of great things in domoticz. You can reach your goals in many ways.
Moved the topic to the Python subforum.
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
Post Reply