SOLVED Run dzVents script every 10 seconds?  [Solved]

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

Moderator: leecollings

Post Reply
McJohn
Posts: 91
Joined: Wednesday 10 October 2018 17:01
Target OS: Raspberry Pi / ODroid
Domoticz version: 2020.2
Location: The Netherlands
Contact:

SOLVED Run dzVents script every 10 seconds?  [Solved]

Post by McJohn »

Is it possible to run the script below every 10 seconds?

The problem is that when the Aeotec Home Energy Meter (HEM) doesn't has power,
Domoticz still keeps holding the last values (for example 230V / 16.2 Watt / 0.2 A).
Ands that's unfortunately a fault in Domoticz I think because when there is no power all the Devices have to say/set to 0.

So, we are working with a script but this script fires only every minute. So, when the power is gone (or back again) it takes a full minute before Domoticz can do/say something. (And sometimes are 60 seconds a very loong time in a human life).
So, Is it possible to run the script below every 10 seconds?

The HEM Z-Wave Device data refreshes every 5 seconds.
So, the most beautiful solution is a Trigger when the HEM data doesn't refresh after >10 seconds. (Or is Life back again).

Thanks for all the support!

John

Code: Select all

return {
    on = {
            timer = { 'every minute' }},
        
    execute = function( dz )
        local voltage = dz.devices('Voltage')
        local power= dz.variables('Power')
        
        local function setvar(var, to)
            if var.value ~= to and dz.startTime.secondsAgo > 60 then var.set(to) end
        end
        
        if voltage.lastUpdate.secondsAgo > 60 then
            setvar(power,'Off')
        else
            setvar(power,'On')
        end
        
    end
}
Last edited by McJohn on Monday 31 May 2021 16:57, edited 1 time in total.
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Run dzVents script every 10 seconds?

Post by waaren »

McJohn wrote: Saturday 29 May 2021 19:31 Is it possible to run the script below every 10 seconds?
Could look like below.

Code: Select all


local scriptVar = 'every10Seconds'

return
{
    on =
    {
        timer =
        {
            'every minute',
        },
        customEvents =
        {
            scriptVar,
        },
    },

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

    execute = function( dz, item )
        if item.isTimer then
            for delay = 10, 50, 10 do
                dz.emitEvent(scriptVar).afterSec(delay)
            end
        end

        local voltage = dz.devices('Voltage')
        local power= dz.variables('Power')

        local function setvar(var, to)
            if var.value ~= to and dz.startTime.secondsAgo > 60 then var.set(to) end
        end

        if voltage.lastUpdate.secondsAgo > 60 then
            setvar(power,'Off')
        else
            setvar(power,'On')
        end
    end
}

Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
McJohn
Posts: 91
Joined: Wednesday 10 October 2018 17:01
Target OS: Raspberry Pi / ODroid
Domoticz version: 2020.2
Location: The Netherlands
Contact:

Re: Run dzVents script every 10 seconds?

Post by McJohn »

Thank you so much for the very quick reply!

The script is much faster now when the power is back; within 10 seconds the Variable is ON. Perfect.
But when the power is off, there is still a delay of ~60 seconds.
Isn't that strange?
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Run dzVents script every 10 seconds?

Post by waaren »

McJohn wrote: Saturday 29 May 2021 20:59 But when the power is off, there is still a delay of ~60 seconds.
Isn't that strange?
No not really. You check for 60 seconds so it should not come as a surprise...
If you want it quicker then change line

Code: Select all

if voltage.lastUpdate.secondsAgo > 60 then
to

Code: Select all

if voltage.lastUpdate.secondsAgo > 9 then
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
McJohn
Posts: 91
Joined: Wednesday 10 October 2018 17:01
Target OS: Raspberry Pi / ODroid
Domoticz version: 2020.2
Location: The Netherlands
Contact:

Re: Run dzVents script every 10 seconds?

Post by McJohn »

Thank you very much for your answer.
It’s working now, perfect! :D

Last questions:
When these kind of scripts are running every 10 seconds:
- Ist that an heavy load for Domoticz?
- When this script is running every 10 seconds, can this affect the operation/execution of other scripts at that moment?
- What happens when 5 scripts are running every 10 seconds?

Sorry for all the questions, I appreciate your help!
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Run dzVents script every 10 seconds?

Post by waaren »

McJohn wrote: Sunday 30 May 2021 10:55 - Ist that an heavy load for Domoticz?
it depends on the size of your system and any other apps executed on that system
- When this script is running every 10 seconds, can this affect the operation/execution of other scripts at that moment?
Yes. Only one event script can be executed simultaniously. So when this script executes. Others have to wait.
- What happens when 5 scripts are running every 10 seconds?
30 scripts will run every minute.
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
McJohn
Posts: 91
Joined: Wednesday 10 October 2018 17:01
Target OS: Raspberry Pi / ODroid
Domoticz version: 2020.2
Location: The Netherlands
Contact:

Re: Run dzVents script every 10 seconds?

Post by McJohn »

Thanks for all the interesting information and your patience.
In our case it's a Raspberry Py3 B+ with only Domoticz running.

Is there a way (via a Dummy switch) to switch On and Off these kind of scripts?
With 2 separate dzVents scripts?
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Run dzVents script every 10 seconds?

Post by waaren »

McJohn wrote: Sunday 30 May 2021 19:20 Is there a way (via a Dummy switch) to switch On and Off these kind of scripts?
You could check on the state of a dummy swithch to decide if you want to trigger the script the extra 5 times.
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
McJohn
Posts: 91
Joined: Wednesday 10 October 2018 17:01
Target OS: Raspberry Pi / ODroid
Domoticz version: 2020.2
Location: The Netherlands
Contact:

Re: Run dzVents script every 10 seconds?

Post by McJohn »

Thanks for the explanation.
Could you please adjust the script top here with the status of the switch "powercheck" ?
(On = script=On , Off = script disabled)
(Math is not my best side..)

Thanks in advance.

John
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Run dzVents script every 10 seconds?

Post by waaren »

McJohn wrote: Monday 31 May 2021 8:59 Thanks for the explanation.
Could you please adjust the script top here with the status of the switch "powercheck" ?
Could look like below

Code: Select all


local scriptVar = 'every10Seconds'

return
{
    on =
    {
        timer =
        {
            'every minute',
        },
        customEvents =
        {
            scriptVar,
        },
    },

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

    execute = function( dz, item )

        if dz.devices('powercheck').state == 'Off' then return end -- If powercheck device state is Off the script will not do anything,

        if item.isTimer then
            for delay = 10, 50, 10 do
                dz.emitEvent(scriptVar).afterSec(delay)
            end
        end

        local voltage = dz.devices('Voltage')
        local power= dz.variables('Power')

        local function setvar(var, to)
            if var.value ~= to and dz.startTime.secondsAgo > 60 then var.set(to) end
        end

        if voltage.lastUpdate.secondsAgo > 60 then
            setvar(power,'Off')
        else
            setvar(power,'On')
        end
    end
}


Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
McJohn
Posts: 91
Joined: Wednesday 10 October 2018 17:01
Target OS: Raspberry Pi / ODroid
Domoticz version: 2020.2
Location: The Netherlands
Contact:

Re: Run dzVents script every 10 seconds?

Post by McJohn »

Yes, it's working!
Thank you very much, also for all your time and patience.
I've learned a lot from you these days! :D

Case closed.
Post Reply

Who is online

Users browsing this forum: No registered users and 0 guests