check device, create if not existing

Easy to use, 100% Lua-based event scripting framework.

Moderator: leecollings

Post Reply
manjh
Posts: 859
Joined: Saturday 27 February 2016 12:49
Target OS: Raspberry Pi / ODroid
Domoticz version: 2020.2
Location: NL
Contact:

check device, create if not existing

Post by manjh »

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: 1355
Joined: Friday 29 August 2014 11:26
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: Ermelo
Contact:

Re: check device, create if not existing

Post by dannybloe »

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: 859
Joined: Saturday 27 February 2016 12:49
Target OS: Raspberry Pi / ODroid
Domoticz version: 2020.2
Location: NL
Contact:

Re: check device, create if not existing

Post by manjh »

dannybloe wrote: Wednesday 21 February 2018 16: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: 1355
Joined: Friday 29 August 2014 11:26
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: Ermelo
Contact:

Re: check device, create if not existing

Post by dannybloe »

The check is very simple.
if domoticz.devices('mytextdevice') ~= nil then...
Creator dzVents - RPi3, loads of zwave devices, esp8266, evohome.
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: check device, create if not existing

Post by waaren »

manjh wrote: Wednesday 21 February 2018 16:58
dannybloe wrote: Wednesday 21 February 2018 16: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. 
--
-- 
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
manjh
Posts: 859
Joined: Saturday 27 February 2016 12:49
Target OS: Raspberry Pi / ODroid
Domoticz version: 2020.2
Location: NL
Contact:

Re: check device, create if not existing

Post by manjh »

waaren wrote: Thursday 22 February 2018 1: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: 859
Joined: Saturday 27 February 2016 12:49
Target OS: Raspberry Pi / ODroid
Domoticz version: 2020.2
Location: NL
Contact:

Re: check device, create if not existing

Post by manjh »

dannybloe wrote: Wednesday 21 February 2018 16: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: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: check device, create if not existing

Post by waaren »

manjh wrote: Saturday 24 February 2018 12:31
waaren wrote: Thursday 22 February 2018 1: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).
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: check device, create if not existing

Post by waaren »

manjh wrote: Saturday 24 February 2018 16:48
dannybloe wrote: Wednesday 21 February 2018 16: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.
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
manjh
Posts: 859
Joined: Saturday 27 February 2016 12:49
Target OS: Raspberry Pi / ODroid
Domoticz version: 2020.2
Location: NL
Contact:

Re: check device, create if not existing

Post by manjh »

waaren wrote: Saturday 24 February 2018 17:53
manjh wrote: Saturday 24 February 2018 16:48
dannybloe wrote: Wednesday 21 February 2018 16: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: 859
Joined: Saturday 27 February 2016 12:49
Target OS: Raspberry Pi / ODroid
Domoticz version: 2020.2
Location: NL
Contact:

Re: check device, create if not existing

Post by manjh »

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: No registered users and 1 guest