Page 1 of 1

Import data from website into domoticz sensor script

Posted: Monday 19 December 2022 14:24
by TomDoRpi
Hi,
I have read some other discussions but I do not really have a clue how to create a working script to import data from a different automation system into domoticz.
The other system is working autonomous fine and has several sensors installed in my house already and I like to get this data in domoticz.
When I open the webpage the respond looks like in the attachment file.
I want to import the temperature value of 11,9403 degrees Celsius into domoticz every 5 minutes.
Really appreciate any help here, when I know how its working for 1 sensor I am sure I can adapt for the others.

Re: Import data from website into domoticz sensor script

Posted: Monday 19 December 2022 15:16
by willemd
Have a look at the documentation how to use dzVents to do asynchronous shell command execution.

Assuming you are on a Linux system, you could use "cat filename | grep href | awk -F">" '{print $3}' | awk -F"<" '{ print $1}' "
to get the value out of the example file above (replace filename by your filename of course)
1) grep selects the line with "href" in it from the file
2) the first awk selects the 3rd field of the that line, using field separator ">"
3) the second awk selects the 1st field of the result of step2, with field separator "<" which is the value you want

The value will be in item.data once the command has been executed. Might needed conversion to number. You can then assign that value to a device or do anything else with it that you want.

I don't want to spell it out for you .... much more rewarding to discover yourself how this works ;-)

Re: Import data from website into domoticz sensor script

Posted: Monday 02 January 2023 13:03
by TomDoRpi
Hi, sorry about missing out the main info:
I am running domoticz on a raspberry pi3B

Thanks for your idea with the linux script.
Can you imagine to work with a script instead (lua/dzvents)?
It will be easier to maintain within the domoticz scripts backup etc.

Re: Import data from website into domoticz sensor script

Posted: Monday 02 January 2023 13:29
by waltervl
Here is a DzVents example to scrape an html page: https://domoticz.com/forum/viewtopic.ph ... 54#p223354

Re: Import data from website into domoticz sensor script

Posted: Monday 02 January 2023 13:49
by willemd
TomDoRpi wrote: Monday 02 January 2023 13:03 Hi, sorry about missing out the main info:
I am running domoticz on a raspberry pi3B

Thanks for your idea with the linux script.
Can you imagine to work with a script instead (lua/dzvents)?
It will be easier to maintain within the domoticz scripts backup etc.
Actually, the example I gave is a unix shell command, but what I suggested was to launch this shell command from within a Dzvents script as a asynchronous command. So you develop a dzVents script to get the value and then do whatever you want with that value, such as store it in a device, perform checks on it, make decision based on it, etc etc. This script is fully stored within the domoticz dbase and also included in the backup. The only unix shell part of the script is the single line of my example which is within the dzVents script as a asynchronous command.

Here is a small example based on the same principle. It is a dzvent script that launches a shell script command to check disk usage and then reports it back in the domoticz log.

Code: Select all

local diskused='diskused'

return {
	on = {
		timer = {
			'every hour'
		},
		shellCommandResponses =
        {
            diskused,
        },
	},
	logging = {
		level = domoticz.LOG_INFO,
		marker = 'template',
	},
	execute = function(domoticz, item)
	    if item.isTimer then
    		domoticz.executeShellCommand({
                command = 'df -h /media/usbdisk | grep media | awk \'{print $5}\' | awk -F% \'{print $1}\'',
                callback = diskused,
                timeout = 5,
            })
        else 
            domoticz.log('Disk used % ' .. tonumber(item.data), domoticz.LOG_INFO)
            if tonumber(item.data)<50 then
                domoticz.log('Enough space left. Disk used ' .. item.data, domoticz.LOG_INFO)
            else  
                domoticz.log('Watch your disk space. Disk used ' .. item.data, domoticz.LOG_INFO)
            end    
        end
	end
}