how to call a dzvents script from within another?  [Solved]

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

Moderator: leecollings

Post Reply
lastofthejediknights
Posts: 24
Joined: Tuesday 27 March 2018 14:17
Target OS: Raspberry Pi / ODroid
Domoticz version: 2021.1
Location: uk
Contact:

how to call a dzvents script from within another?

Post by lastofthejediknights »

Hi,
The issue I wish to solve is this.
A switch that represents my shower pump state.
A dimmer that is the power level of a heated towel rail.
I wish to turn the towel rail on, but only after the pump has been running for say 3 mins.
I could do this within a script that runs frequently, storing the time of status change from low to high, with a persistent variable, then turning the towel rail on when the condition is met.
This seems a bit wasteful of resources to me, and would not trigger as soon as the state changes.
So could I have a script trigger on the device, and have it call another script on a Delay of 3 mins say, and check within this second script that the pump is still active, and turn on the towel rail.
Suggestions or other ideas gratefully entertained.
User avatar
waltervl
Posts: 5893
Joined: Monday 28 January 2019 18:48
Target OS: Linux
Domoticz version: 2024.7
Location: NL
Contact:

Re: how to call a dzvents script from within another?

Post by waltervl »

You can use a scene for this: switch on shower pump and after 3 minutes switch on towel rail.

Or you can make a dzvents script that switches on the shower pump and use the .afterMin(3) extension on the towel rail device to switch it 3 minutes later. No extra script needed
Domoticz running on Udoo X86 (on Ubuntu)
Devices/plugins: ZigbeeforDomoticz (with Xiaomi, Ikea, Tuya devices), Nefit Easy, Midea Airco, Omnik Solar, Goodwe Solar
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: how to call a dzvents script from within another?

Post by waaren »

lastofthejediknights wrote: Tuesday 02 February 2021 22:52 I wish to turn the towel rail on, but only after the pump has been running for say 3 mins.
Do you only want to check if the pump was started 3 minutes ago? Then a simple afterMin(3) would do it.

or do you want to ensure the pump has been running for at least 3 minutes since it started (taking paused periods into account)? In that case you would need extra triggers ( on = { devices = { 'pump'}, customEvents = {'pumpExecution '} } and some timekeeping for which you could use dzVents persistent historical API
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
lastofthejediknights
Posts: 24
Joined: Tuesday 27 March 2018 14:17
Target OS: Raspberry Pi / ODroid
Domoticz version: 2021.1
Location: uk
Contact:

Re: how to call a dzvents script from within another?

Post by lastofthejediknights »

Hi, yes I only want to turn on the towel rail AFTER the pump has been running for 3 mins continuously,.
Not just a 3 min delay, as could be solved by the scenes method.

Wouldn't the historical or persistent date still require including it into a frequently timed event?

I suppose I could use the device trigger and look at the time elapsed between the on and off, but this would be the time taken to say fill the bath in its entirety, rather than say the first 3 mins. Practically not much difference.
I could use a dummy switch, activated by the pump script on a
switchOn().afterMin(​3) basis, and check the state of the pump at that point, switching the rail as appropriate.
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: how to call a dzvents script from within another?

Post by waaren »

lastofthejediknights wrote: Wednesday 03 February 2021 1:00 Wouldn't the historical or persistent date still require including it into a frequently timed event?
Not if you calculate the minimal remaining time to get to the three minutes and use domoticz.emitEvent() to trigger the script based on the named customEvent .

so initially right after starting the pump you would do something like emitEvent('lookAtPump' ).afterSec(180) and when this script checks the pump active time and determine is was active for only 110 seconds during these 3 minutes, it would send another emitEvent('lookAtPump' ).afterSec(180 - 110) and repeat these steps until the pumps total active time becomes >= 180
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
lastofthejediknights
Posts: 24
Joined: Tuesday 27 March 2018 14:17
Target OS: Raspberry Pi / ODroid
Domoticz version: 2021.1
Location: uk
Contact:

Re: how to call a dzvents script from within another?

Post by lastofthejediknights »

Hi,

Ok, Many thanks for the help. As a first pass solution, simply looking at the status after some elapsed time, rather than tracking the actual time the device is actually switched on, I have the following two scripts.
1___ Called by the ShowerPump input

Code: Select all

return {
	on = {
		devices = {
			'Shower Pump'
		}
	},
	execute = function(domoticz, device)
	    emitEvent('CheckPumpRun').afterSec(120)
		domoticz.log('Device ' .. device.name .. ' called PumpRun Script', domoticz.LOG_INFO)
	end
}
and 2____ the Custom Event script - CheckPumpRun'

Code: Select all

return {
	on = {
		customEvents = {
			'CheckPumpRun' -- event triggered by emitEvent
		}
	},
	data = {},
	logging = {},
	
	execute = function(domoticz, triggeredItem)
		if (triggeredItem.isCustomEvent) then
		    if((domoticz.devices('Shower Pump').state == 'On') ) then -- if pump running after time
		        domoticz.devices('TowelRail Boost').switchOn()-- turn on the towelrail for a bit
		        domoticz.log(' Pump running, turned on Towelrail ' , domoticz.LOG_INFO)
		    else
		      domoticz.log(' Pump NOT running, do nothing ' , domoticz.LOG_INFO)
		    end
			  --domoticz.utils._.print(triggeredItem.data)
		else
			-- second parameter can be anything, number, string, boolean or table
			--domoticz.emitEvent('MyEvent', 'Some data')
		end
	end
}
This is tested and working fine.

Again, any suggestions or improvements gratefully received.
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: how to call a dzvents script from within another?

Post by waaren »

lastofthejediknights wrote: Wednesday 03 February 2021 11:41 This is tested and working fine.
Again, any suggestions or improvements gratefully received.
Great !

you could consider to merge the two scripts together.

Code: Select all

local scriptVar = 'CheckPumpRun'
return
{
    on =
    {
        devices =
        {
            'Shower Pump',
        },
        customEvents =
        {
            scriptVar,
        },
    },

    logging =
    {
        level = domoticz.LOG_DEBUG,
        marker = scriptVar,
    },

    execute = function(dz, item)
        if item.isDevice and item.active then
            dz.emitEvent(scriptVar).afterSec(120)
            dz.log('Device ' .. item.name .. ' called customEvent ' .. scriptVar, dz.LOG_INFO)
        elseif item.isCustomEvent then
            if dz.devices('Shower Pump').active then
                dz.devices('TowelRail Boost').switchOn()-- turn on the towelrail for a bit
                dz.log(' Pump running, turned on Towelrail ' , dz.LOG_DEBUG)
            else
              dz.log(' Pump NOT running, do nothing ' , dz.LOG_DEBUG)
            end
        else
             dz.log(' Pump NOT active, do nothing ' , dz.LOG_DEBUG)
        end
    end
}

Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
lastofthejediknights
Posts: 24
Joined: Tuesday 27 March 2018 14:17
Target OS: Raspberry Pi / ODroid
Domoticz version: 2021.1
Location: uk
Contact:

Re: how to call a dzvents script from within another?  [Solved]

Post by lastofthejediknights »

Hi,

Many thanks for the education.
I had to change the line

Code: Select all

emitEvent(scriptVar).afterSec(120)
to

Code: Select all

dz.emitEvent(scriptVar).afterSec(120)
to get it to run, Odd that the omission of "domoticz" in the otherwise identical line in my two seperate script solution ran ok.

In any event, thank you for the help.

P.S. I don't see how to mark this as "Solved" but it is!

regards
Nigel
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest