Page 1 of 1
check device, create if not existing
Posted: Wednesday 21 February 2018 16:44
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?
Re: check device, create if not existing
Posted: Wednesday 21 February 2018 16:49
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.
Re: check device, create if not existing
Posted: Wednesday 21 February 2018 16:58
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...
Re: check device, create if not existing
Posted: Wednesday 21 February 2018 19:08
by dannybloe
The check is very simple.
if domoticz.devices('mytextdevice') ~= nil then...
Re: check device, create if not existing
Posted: Thursday 22 February 2018 1:37
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.
--
--
Re: check device, create if not existing
Posted: Saturday 24 February 2018 12:31
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...
Re: check device, create if not existing
Posted: Saturday 24 February 2018 16:48
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!
Re: check device, create if not existing
Posted: Saturday 24 February 2018 17:51
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).
Re: check device, create if not existing
Posted: Saturday 24 February 2018 17:53
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.
Re: check device, create if not existing
Posted: Saturday 24 February 2018 20:43
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...

Re: check device, create if not existing
Posted: Saturday 24 February 2018 21:05
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!!!