dzVents 3.0 released

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

Moderator: leecollings

Post Reply
dannybloe
Posts: 1355
Joined: Friday 29 August 2014 11:26
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: Ermelo
Contact:

dzVents 3.0 released

Post by dannybloe »

Hi folks,

I am proud to announce dzVents 3.0 has been merged in Domoticz v4.11687. Thanks to the hard work of MrHobbes74, waaren and yours truly we have some very interesting new features:

System events

Many times asked for and now possible is a way to run scripts as soon as Domoticz was started. You can now use the new system on-trigger:

Code: Select all

return {
	on = {
		system = { 'start' }
	},
	execute = function(domoticz, item)
		domoticz.log('Domoticz has started')
		domoticz.log(item.trigger) -- will print 'start'
	end
}
Of course there are other system events and for now we implemented the following (but it is not unlikely there will be more in the future):
  • start - fired when Domoticz has started.
  • stop - fired when Domoticz is shutting down. As you probably can imagine you have only a limited amount of time to have Domoticz do stuff when your script has been completed. Some commands will probably not be executed. Just give it a try.
  • dailyBackupFinished - the trigger item (2nd parameter of the execute function) holds the location of the newly created backup. You can use this perhaps to copy the file to some other location.
  • hourlyBackupFinished
  • monthlyBackupFinished
  • manualBackupFinished - fired when you start a backup using the Domoticz GUI.

Code: Select all

return {
	on = {
		system = { 'dailyBackupFinished' }
	},
	execute = function(domoticz, item)
		domoticz.log('Backup finished. It took ' .. item.duration .. ' and the location is ' .. item.location)
	end
}

Custom events

It is now possible to emit your own custom events using the new domoticz.emitEvent() function. dzVents scripts can then subscribe to these custom events using the new on-trigger customEvent.

A script emiting an event:

Code: Select all

return {
	on = {
		devices = {
			'myTempSensor'
		}
	},
	execute = function(domoticz, mySensor)
		if (mySensor.temperature > 50) then
			domoticz.emitEvent('fire', { 'temp' = mySensor.temperature })
		end
	end
}
Then other scripts can subscribe to this event:

Code: Select all

return {
	on = {
		customEvent = { 'fire' }
	},
	execute = function(domoticz, trigger)
		domoticz.log('Fire! Run!!')
		domoticz.log(triggeredItem.data.temp)  
	end
}
Note that it is not necessary to provide data with you custom event. It is optional:

Code: Select all

domoticz.emitEvent('myEvent') -- no data
domoticz.emitEvent('another event', 'some data')
domoticz.emitEvent('hugeEvent', { a = 10, b = 20, some = 'text', sub = { x = 10, y = 20 } })
And there's a bonus: you can emit a custom event using the json api:

Code: Select all

https://path.to.domoticz/json.htm?type=command&param=customevent&event=MyEvent&data=myData
or MQTT:

Code: Select all

{"command" : "customevent", "event" : "MyEvent" , "data" : "myData" } 
This is very useful if for instance you have some external systems like sensors that want to send data to Domoticz and have a script process this data without having to create virtual devices or user variables. Previously that was the only way to trigger scripts in Domoticz. Now you can simply trigger an event with some data and a script can process the data and perhaps update the necessary devices based on this (processed) data. Much easier!

Oh, one more thing: both system and custom events can be combined with time rules:

Code: Select all

return {
	on = {
		system = { 
			['start'] = { 'between sunset and sunrise on mon,tue,fr', 'on sun' }
		},
		customEvent = { 
			['fire'] = { 'at daytime' } -- don't disturb me when I'm sleeping please!
		} 
	},
	execute = function(dz)
		...
	end
}
So, exciting new features. Happy scripting peeps and let's make our houses even smarter!

Cheers,
Danny
Creator dzVents - RPi3, loads of zwave devices, esp8266, evohome.
User avatar
felix63
Posts: 244
Joined: Monday 07 December 2015 9:30
Target OS: Raspberry Pi / ODroid
Domoticz version: 2020.1
Location: Gouda
Contact:

Re: dzVents 3.0 released

Post by felix63 »

Looks great! Can't wait to try them out. Thanks for the hard work.
brommetje
Posts: 67
Joined: Sunday 16 February 2014 17:40
Target OS: Raspberry Pi / ODroid
Domoticz version: Beta
Location: NL
Contact:

Re: dzVents 3.0 released

Post by brommetje »

Just a beginner but great new features, thanks for (all) hard work.
molnaratti
Posts: 34
Joined: Friday 02 February 2018 16:21
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: dzVents 3.0 released

Post by molnaratti »

I just updated, unfortunately I get this error message:
Error: EventSystem: in /home/pi/domoticz/dzVents/runtime/dzVents.lua: /home/pi/domoticz/dzVents/runtime/EventHelpers.lua:462: bad argument #1 to 'for iterator' (table expected, got string)
dannybloe
Posts: 1355
Joined: Friday 29 August 2014 11:26
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: Ermelo
Contact:

Re: dzVents 3.0 released

Post by dannybloe »

molnaratti wrote: Sunday 16 February 2020 19:03 I just updated, unfortunately I get this error message:
Error: EventSystem: in /home/pi/domoticz/dzVents/runtime/dzVents.lua: /home/pi/domoticz/dzVents/runtime/EventHelpers.lua:462: bad argument #1 to 'for iterator' (table expected, got string)
Please report this in the bugs section together with all that's necessary to reproduce the error.
Creator dzVents - RPi3, loads of zwave devices, esp8266, evohome.
User avatar
felix63
Posts: 244
Joined: Monday 07 December 2015 9:30
Target OS: Raspberry Pi / ODroid
Domoticz version: 2020.1
Location: Gouda
Contact:

Re: dzVents 3.0 released

Post by felix63 »

One of the use cases for the new custom events is improving some combinations with IFTTT.

For instance, I have a Withings Scale. I'm planning to use Webhooks to trigger a custom event. But how do I pass multiple data attributes? In this case both weight and fat percentage?

The syntax described is: https://path.to.domoticz/json.htm?type= ... ata=myData. But how do I pass multiple values? I have multiple use cases in mind for this new functionality. But they all would benefit from being able to transmit multiple values from IFTTT to Domoticz.

A second question is, could I use POST instead of GET when passing variables to json.htm? And what would the Webhook look like?
MrHobbes74
Posts: 115
Joined: Sunday 19 January 2020 22:29
Target OS: Raspberry Pi / ODroid
Domoticz version: latest B
Location: Netherlands
Contact:

Re: dzVents 3.0 released

Post by MrHobbes74 »

Hi! You can transfer multiple values by providing a json formatted string as the myData parameter (e.g. data=[{“a”:“b”, “c”:“d”}]
dannybloe
Posts: 1355
Joined: Friday 29 August 2014 11:26
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: Ermelo
Contact:

Re: dzVents 3.0 released

Post by dannybloe »

MrHobbes74 wrote: Monday 17 February 2020 10:01 Hi! You can transfer multiple values by providing a json formatted string as the myData parameter (e.g. data=[{“a”:“b”, “c”:“d”}]
Make sure it is url encoded.
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: dzVents 3.0 released

Post by waaren »

felix63 wrote: Monday 17 February 2020 9:24 One of the use cases for the new custom events is improving some combinations with IFTTT.
A second question is, could I use POST instead of GET when passing variables to json.htm? And what would the Webhook look like?
Webhook for sending customEvent from IFTTT to domoticz:

Code: Select all

https://waaren.xxx.xxx:8443/json.htm?username=SUXZXVFRXX==&password=SUXZXVFRXX==&type=command&param=customevent&event=MyEvent&data=[{"obj":"Kitchen ceiling","act":"On"}]
What do you want to do with the post that can't be done with a get ?
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
User avatar
felix63
Posts: 244
Joined: Monday 07 December 2015 9:30
Target OS: Raspberry Pi / ODroid
Domoticz version: 2020.1
Location: Gouda
Contact:

Re: dzVents 3.0 released

Post by felix63 »

Just curious... In the real world, the shortest way between two points is a straight line. In an IT-world there are at least 3 ways (and a shortcut) to get something done. But for now I am very happy with the previous answers!
User avatar
felix63
Posts: 244
Joined: Monday 07 December 2015 9:30
Target OS: Raspberry Pi / ODroid
Domoticz version: 2020.1
Location: Gouda
Contact:

Re: dzVents 3.0 released

Post by felix63 »

waaren wrote: Monday 17 February 2020 10:27 Webhook for sending customEvent from IFTTT to domoticz:

Code: Select all

https://waaren.xxx.xxx:8443/json.htm?username=SUXZXVFRXX==&password=SUXZXVFRXX==&type=command&param=customevent&event=MyEvent&data=[{"obj":"Kitchen ceiling","act":"On"}]
If I use your example and add the following to the event handler

Code: Select all

        domoticz.notify('Events', "data:".. triggeredItem.data.obj, domoticz.PRIORITY_NORMAL)
I get the following in my log-file upon making the call:

Code: Select all

2020-02-17 17:19:52.718 Status: dzVents: Info: EVT.TST: ------ Start internal script: event testing: Custom event: "MyEvent"
2020-02-17 17:19:52.723 Status: dzVents: Info: EVT.TST: ------ Finished event testing
2020-02-17 17:19:52.719 Error: dzVents: Error: (3.0.0) EVT.TST: An error occurred when calling event handler event testing
2020-02-17 17:19:52.723 Error: dzVents: Error: (3.0.0) EVT.TST: ...ticz/scripts/dzVents/generated_scripts/event testing.lua:19: attempt to concatenate a nil value (field '?')
2
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: dzVents 3.0 released

Post by waaren »

felix63 wrote: Monday 17 February 2020 17:21 If I use your example and add the following to the event handler

Code: Select all

        domoticz.notify('Events', "data:".. triggeredItem.data.obj, domoticz.PRIORITY_NORMAL)
The obj is in the first record of the data so you should use
triggeredItem.data[1].obj
here.
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
User avatar
beachhead
Posts: 6
Joined: Wednesday 04 February 2015 8:12
Target OS: Linux
Domoticz version: 4.10717
Location: Netherlands
Contact:

Re: dzVents 3.0 released

Post by beachhead »

When will dzvents 3.0 be implemented in a stable Domoticz release ?
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: dzVents 3.0 released

Post by waaren »

beachhead wrote: Saturday 29 February 2020 9:08 When will dzvents 3.0 be implemented in a stable Domoticz release ?
Next stable domoticz version. @gizmocuz to decide when beta branche is stable enough to be promoted to 'stable'
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
ronaldbro
Posts: 327
Joined: Thursday 15 November 2018 21:38
Target OS: Raspberry Pi / ODroid
Domoticz version: 2020.2
Location: Netherlands
Contact:

Re: dzVents 3.0 released

Post by ronaldbro »

I like the BackupFinished events and would like to use it to sftp the backup to a different location and start a backup of some domoticz folders. For me this would be more easy to maintain then a cron job and it will happen at exactly the right time.
But this would take some execution time...
I guess this will be executed by the dzVents thread right? In that case I shouldn't do this. Or are there any ways to execute a script asynchronously in a different thread and get a callback when it's finished?
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: dzVents 3.0 released

Post by waaren »

ronaldbro wrote: Sunday 24 May 2020 13:38 I like the BackupFinished events and would like to use it to sftp the backup to a different location and start a backup of some domoticz folders. For me this would be more easy to maintain then a cron job and it will happen at exactly the right time.
But this would take some execution time...
I guess this will be executed by the dzVents thread right? In that case I shouldn't do this. Or are there any ways to execute a script asynchronously in a different thread and get a callback when it's finished?
If you start the script with the sftp command(s) in the background by adding an & at the end of the command it will be executed without blocking the event system.
example: os.execute('sudo backupScript.sh &' )

you can include a json call to customEvent in the script to notify dzVents when the backup script finished and if it finished successfully.
example: curl -s "http://<domoticz_ip:domoticz_port>/json.htm?type=command&param=customevent&event=backupscript_finished&data=$backupResult"
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
ronaldbro
Posts: 327
Joined: Thursday 15 November 2018 21:38
Target OS: Raspberry Pi / ODroid
Domoticz version: 2020.2
Location: Netherlands
Contact:

Re: dzVents 3.0 released

Post by ronaldbro »

Thanks, I would have never thought of this :)
ronaldbro
Posts: 327
Joined: Thursday 15 November 2018 21:38
Target OS: Raspberry Pi / ODroid
Domoticz version: 2020.2
Location: Netherlands
Contact:

Re: dzVents 3.0 released

Post by ronaldbro »

Thanks waaren, works perfect

Created the bash script
Spoiler: show
#!/bin/bash

## LOCAL/FTP/SCP/MAIL PARAMETERS
DBFILE=$1
BACKUPTYPE=$2
SERVER="192.168.1.10" # IP of Network disk, used for: ftp mail scp
USERNAME="pi" # FTP username of Network disk used for: ftp mail scp
PASSWORD="########" # FTP password of Network disk used for: ftp mail scp
## END OF USER CONFIGURABLE PARAMETERS

SUFFIX="_system"
BACKUPFILEDIR="${DBFILE%".db"}$SUFFIX.tar.gz" # Change the xxx to yours

### Create backup and ZIP it
tar --exclude='home/pi/domoticz/backups'\
--exclude='home/pi/domoticz/domoticz.db*'\
--exclude='.git'\
--exclude='__pycache__'\
--exclude='home/pi/domoticz/scripts/dzVents/data'\
--exclude='home/pi/domoticz/scripts/dzVents/generated_scripts'\
-zcvf $BACKUPFILEDIR /home/pi/domoticz/ # Change the xxx to yours # Or try /home/pi/domoticz/
## -zcvf /tmp/$BACKUPFILEDIR /home/pi/domoticz/ # Change the xxx to yours # Or try /home/pi/domoticz/

### Send to Network disk through FTP
curl -k --disable-epsv -v -T"$BACKUPFILEDIR" -u"$USERNAME:$PASSWORD" "sftp://$SERVER/media/data/DomoticzBackup/$BACKUPTYPE/" # Change the ftp to yours !!!
curl -k --disable-epsv -v -T"$DBFILE" -u"$USERNAME:$PASSWORD" "sftp://$SERVER/media/data/DomoticzBackup/$BACKUPTYPE/" # Change the ftp to yours !!!

### Done!
And dzVents script
Spoiler: show
return {
on = {
system = {
'manualBackupFinished',
'dailyBackupFinished',
'hourlyBackupFinished',
'monthlyBackupFinished'
},

-- customEvents = {
-- 'myCustomEvent'
-- }
},

-- custom logging level for this script
logging = {
-- level = domoticz.LOG_DEBUG,
-- level = domoticz.LOG_INFO,
level = domoticz.LOG_ERROR,
marker = "Backups"
},

execute = function(dz, item, info)
if item.type == 'manualBackupFinished' then
os.execute('sudo /home/pi/domoticz/scripts/backup_dzVents.sh ' .. item.location .. ' manual &' )
elseif item.type == 'dailyBackupFinished' then
os.execute('sudo /home/pi/domoticz/scripts/backup_dzVents.sh ' .. item.location .. ' daily &' )
elseif item.type == 'hourlyBackupFinished' then
os.execute('sudo /home/pi/domoticz/scripts/backup_dzVents.sh ' .. item.location .. ' hourly &' )
elseif item.type == 'monthlyBackupFinished' then
os.execute('sudo /home/pi/domoticz/scripts/backup_dzVents.sh ' .. item.location .. ' monthly &' )
end
end
}
And now I got a backup of the domoticz folder together the database backup and it's send with sftp to another pi.
Post Reply

Who is online

Users browsing this forum: Bing [Bot] and 1 guest