check device, create if not existing

Easy to use, 100% Lua-based event scripting framework.
Post Reply
manjh
Posts: 355
Joined: Saturday 27 February 2016 13:49
Target OS: Raspberry Pi
Domoticz version: 3.8153
Location: NL
Contact:

check device, create if not existing

Post by manjh » Wednesday 21 February 2018 17:44

I'm trying to put the following into a dzVents script:
1. check if a (text) device exists
2. if not, create the device

Have searched through the forum and WIki, looked at the dzVents documentation and examples, but can't find it.
Can someone point me in the right direction?
Hans

dannybloe
Posts: 1486
Joined: Friday 29 August 2014 11:26
Target OS: Raspberry Pi
Domoticz version:
Location: Ermelo
Contact:

Re: check device, create if not existing

Post by dannybloe » Wednesday 21 February 2018 17:49

What you can do is create the device in the GUI and check the network tab to see what url is called. Then use that url from dzVents using openURL.
Creator dzVents - RPi3, loads of zwave devices, esp8266, evohome.

manjh
Posts: 355
Joined: Saturday 27 February 2016 13:49
Target OS: Raspberry Pi
Domoticz version: 3.8153
Location: NL
Contact:

Re: check device, create if not existing

Post by manjh » Wednesday 21 February 2018 17:58

dannybloe wrote:
Wednesday 21 February 2018 17:49
What you can do is create the device in the GUI and check the network tab to see what url is called. Then use that url from dzVents using openURL.
Thanks. But this script will be run frequently, so I want to prevent any unnecessary overhead.

The script is checking the WAN IP address on regular intervals, and stores the found address in a text device for the next check. When a new IP is found, it will send an e-mail so I can update my DNS entries.
Normally, the text device would be created manually at install of the script. The user is instructed to do so. But if he forgets, or makes a typo in the name... :(
That's why I thought a quick check for device status would be "light" enough.
But after explaining all this, I am beginning to think I should not even go this route but rely on manual creationg of the device.

Nevertheless, the programmer in me is curious how this might be done... ;)

I have used Blockly very briefly, but that did not last long. Switched over to LUA and have been happy with that. A few days ago I tripped over dzVents, and it is looking very interesting. Has a good mix of being user-friendly (like Blockly) and still being able to do exactly what I want, down to the last bit and byte (like LUA).
Looking forward to learning more of dzVents...
Hans

dannybloe
Posts: 1486
Joined: Friday 29 August 2014 11:26
Target OS: Raspberry Pi
Domoticz version:
Location: Ermelo
Contact:

Re: check device, create if not existing

Post by dannybloe » Wednesday 21 February 2018 20:08

The check is very simple.
if domoticz.devices('mytextdevice') ~= nil then...
Creator dzVents - RPi3, loads of zwave devices, esp8266, evohome.

User avatar
waaren
Posts: 1348
Joined: Tuesday 03 January 2017 15:18
Target OS: Raspberry Pi
Domoticz version: Beta
Location: Netherlands
Contact:

Re: check device, create if not existing

Post by waaren » Thursday 22 February 2018 2:37

manjh wrote:
Wednesday 21 February 2018 17:58
dannybloe wrote:
Wednesday 21 February 2018 17:49
What you can do is create the device in the GUI and check the network tab to see what url is called. Then use that url from dzVents using openURL.
Thanks. But this script will be run frequently, so I want to prevent any unnecessary overhead.

The script is checking the WAN IP address on regular intervals, and stores the found address in a text device for the next check. When a new IP is found, it will send an e-mail so I can update my DNS entries.
Normally, the text device would be created manually at install of the script. The user is instructed to do so. But if he forgets, or makes a typo in the name... :(
That's why I thought a quick check for device status would be "light" enough.
But after explaining all this, I am beginning to think I should not even go this route but rely on manual creationg of the device.

Nevertheless, the programmer in me is curious how this might be done... ;)

I have used Blockly very briefly, but that did not last long. Switched over to LUA and have been happy with that. A few days ago I tripped over dzVents, and it is looking very interesting. Has a good mix of being user-friendly (like Blockly) and still being able to do exactly what I want, down to the last bit and byte (like LUA).
Looking forward to learning more of dzVents...

Code: Select all

return {

    on = { timer = {'every minute '} }, 
    
    execute = function(domoticz)
    
        if domoticz.devices('My_External_IP') == nil then 
            domoticz.openURL('http://pi-1:8084/json.htm?type=createdevice&idx=16&sensorname=My_External_IP&sensormappedtype=0xF313')
        else    -- Because of the async nature of openURL the device is not yet created in the initial run so we need to wait at least one cycle
            domoticz.devices('My_External_IP').updateText(os.date("last update at %X on %A %d %B %Y"))
        end
    end
}


--  2018-02-22 01:29:00.757  dzVents: Info:  ------ Start external script: CheckExternalIP.lua:, trigger: every minute
--  2018-02-22 01:29:00.761  dzVents: Info:  ------ Finished CheckExternalIP.lua
--
--  Given the 4 mSecs this check and update takes on my PI-3,  I don't think you need to be overly worried about unnecessary overhead. 
--
-- 
Raspberry (debian stretch via berryboot on Synology DS916+) , Domoticz (almost) latest Beta, , dzVents 2.6, RFLink, RFXtrx433e, P1, Youless, Harmony, Hue, Yeelight, Xiaomi, HomeWizard, Zwave, Amazon echo

manjh
Posts: 355
Joined: Saturday 27 February 2016 13:49
Target OS: Raspberry Pi
Domoticz version: 3.8153
Location: NL
Contact:

Re: check device, create if not existing

Post by manjh » Saturday 24 February 2018 13:31

waaren wrote:
Thursday 22 February 2018 2:37

Code: Select all

return {

    on = { timer = {'every minute '} }, 
    
    execute = function(domoticz)
    
        if domoticz.devices('My_External_IP') == nil then 
            domoticz.openURL('http://pi-1:8084/json.htm?type=createdevice&idx=16&sensorname=My_External_IP&sensormappedtype=0xF313')
        else    -- Because of the async nature of openURL the device is not yet created in the initial run so we need to wait at least one cycle
            domoticz.devices('My_External_IP').updateText(os.date("last update at %X on %A %d %B %Y"))
        end
    end
}


--  2018-02-22 01:29:00.757  dzVents: Info:  ------ Start external script: CheckExternalIP.lua:, trigger: every minute
--  2018-02-22 01:29:00.761  dzVents: Info:  ------ Finished CheckExternalIP.lua
--
--  Given the 4 mSecs this check and update takes on my PI-3,  I don't think you need to be overly worried about unnecessary overhead. 
--
-- 
Thanks. I tried the code. Get a warning on the domoticz.devices statement: device does not exist. This is as expected.
Then it tries to create the device, but that does not work. I modified the IP and port, no success...
Hans

manjh
Posts: 355
Joined: Saturday 27 February 2016 13:49
Target OS: Raspberry Pi
Domoticz version: 3.8153
Location: NL
Contact:

Re: check device, create if not existing

Post by manjh » Saturday 24 February 2018 17:48

dannybloe wrote:
Wednesday 21 February 2018 17:49
What you can do is create the device in the GUI and check the network tab to see what url is called. Then use that url from dzVents using openURL.
Perhaps a silly question: where can I find the "network tab"? I loked in Domoticz, cannot find it!
Hans

User avatar
waaren
Posts: 1348
Joined: Tuesday 03 January 2017 15:18
Target OS: Raspberry Pi
Domoticz version: Beta
Location: Netherlands
Contact:

Re: check device, create if not existing

Post by waaren » Saturday 24 February 2018 18:51

manjh wrote:
Saturday 24 February 2018 13:31
waaren wrote:
Thursday 22 February 2018 2:37

Code: Select all

return {

    on = { timer = {'every minute '} }, 
    
    execute = function(domoticz)
    
        if domoticz.devices('My_External_IP') == nil then 
            domoticz.openURL('http://pi-1:8084/json.htm?type=createdevice&idx=16&sensorname=My_External_IP&sensormappedtype=0xF313')
        else    -- Because of the async nature of openURL the device is not yet created in the initial run so we need to wait at least one cycle
            domoticz.devices('My_External_IP').updateText(os.date("last update at %X on %A %d %B %Y"))
        end
    end
}


--  2018-02-22 01:29:00.757  dzVents: Info:  ------ Start external script: CheckExternalIP.lua:, trigger: every minute
--  2018-02-22 01:29:00.761  dzVents: Info:  ------ Finished CheckExternalIP.lua
--
--  Given the 4 mSecs this check and update takes on my PI-3,  I don't think you need to be overly worried about unnecessary overhead. 
--
-- 
Thanks. I tried the code. Get a warning on the domoticz.devices statement: device does not exist. This is as expected.
Then it tries to create the device, but that does not work. I modified the IP and port, no success...
idx 16 is the ID number of my virtual hardware device. Probably yours is another number. You can also use 1 as idx (domoticz internal hardware).
Raspberry (debian stretch via berryboot on Synology DS916+) , Domoticz (almost) latest Beta, , dzVents 2.6, RFLink, RFXtrx433e, P1, Youless, Harmony, Hue, Yeelight, Xiaomi, HomeWizard, Zwave, Amazon echo

User avatar
waaren
Posts: 1348
Joined: Tuesday 03 January 2017 15:18
Target OS: Raspberry Pi
Domoticz version: Beta
Location: Netherlands
Contact:

Re: check device, create if not existing

Post by waaren » Saturday 24 February 2018 18:53

manjh wrote:
Saturday 24 February 2018 17:48
dannybloe wrote:
Wednesday 21 February 2018 17:49
What you can do is create the device in the GUI and check the network tab to see what url is called. Then use that url from dzVents using openURL.
Perhaps a silly question: where can I find the "network tab"? I loked in Domoticz, cannot find it!
Network tab is a feature of your browser where you can inspect what is being sent.
Raspberry (debian stretch via berryboot on Synology DS916+) , Domoticz (almost) latest Beta, , dzVents 2.6, RFLink, RFXtrx433e, P1, Youless, Harmony, Hue, Yeelight, Xiaomi, HomeWizard, Zwave, Amazon echo

manjh
Posts: 355
Joined: Saturday 27 February 2016 13:49
Target OS: Raspberry Pi
Domoticz version: 3.8153
Location: NL
Contact:

Re: check device, create if not existing

Post by manjh » Saturday 24 February 2018 21:43

waaren wrote:
Saturday 24 February 2018 18:53
manjh wrote:
Saturday 24 February 2018 17:48
dannybloe wrote:
Wednesday 21 February 2018 17:49
What you can do is create the device in the GUI and check the network tab to see what url is called. Then use that url from dzVents using openURL.
Perhaps a silly question: where can I find the "network tab"? I loked in Domoticz, cannot find it!
Network tab is a feature of your browser where you can inspect what is being sent.
Ah, yes. As I said: silly question... :shock: :roll:
Hans

manjh
Posts: 355
Joined: Saturday 27 February 2016 13:49
Target OS: Raspberry Pi
Domoticz version: 3.8153
Location: NL
Contact:

Re: check device, create if not existing

Post by manjh » Saturday 24 February 2018 22:05

OK, after capturing the JSON command in the network tab I found that this statement works:

Code: Select all

domoticz.openURL('127.0.0.1:8080/json.htm?type=createvirtualsensor&idx=1&sensorname=WAN&sensortype=5')
Happy!!!
Hans

Post Reply

Who is online

Users browsing this forum: Doler and 2 guests