Does timer overrule specific time  [Solved]

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

Moderator: leecollings

Post Reply
drwurn
Posts: 68
Joined: Sunday 10 June 2018 16:59
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Does timer overrule specific time

Post by drwurn »

Hi,

I have a script where my pool pump goes on/off at specific times.

Code: Select all

return { 
    on = { 
        timer = {   "at 12:00", 
                            "at 16:00",
                            "at 20:00",
                            "at 23:59",
                        },
        devices = { 'ZwembadPH' }
        },

    execute = function(dz)
        local Zwembadpomp                   = dz.devices(47) 
        local ZwembadPH                     = dz.devices(108)

        if dz.time.matchesRule("at 12:00") then
            Zwembadpomp.switchOn()
        elseif dz.time.matchesRule("at 16:00") then
            Zwembadpomp.switchOff().checkFirst()
        end

        if dz.time.matchesRule("at 20:00") then
            Zwembadpomp.switchOn()
        elseif dz.time.matchesRule("at 23:59") then
            Zwembadpomp.switchOff().checkFirst()
        end

        if ZwembadPH.state == "On" then
            Zwembadpomp.switchOn().forHour(5)
        end 
    end
}
When I have to adjust my pool values (PH) then I want my pump to work for 5 hours. I made a dummy switch, when I activate that, the pump works for 5 hours. But what happens when I activate my dummy switch on 13.00 and it needs to pump until 18.00 but it becomes 16.00.
Do I have to make another 'if' in the specific time rule? Something like:

Code: Select all

        if dz.time.matchesRule("at 12:00") then
            Zwembadpomp.switchOn()
        elseif dz.time.matchesRule("at 16:00")  and
            ZwembadPH.state == "Off" then
            Zwembadpomp.switchOff().checkFirst()
        end
Or does the timer 'overrule' the specific time?
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Does timer overrule specific time

Post by waaren »

drwurn wrote: Tuesday 12 May 2020 13:32 I have a script where my pool pump goes on/off at specific times.
When I have to adjust my pool values (PH) then I want my pump to work for 5 hours. I made a dummy switch, when I activate that, the pump works for 5 hours.
You need something like

Code: Select all

return {
    on = {
        timer = {   "at 12:00",
                            "at 16:00",
                            "at 20:00",
                            "at 23:59",
                        },
        devices = { 'ZwembadPH' }
        },

    execute = function(dz, item)
        local Zwembadpomp                   = dz.devices(47)
        local ZwembadPH                     = dz.devices(108)

        if item.isDevice and item.active then
            Zwembadpomp.cancelQueuedCommands()
            Zwembadpomp.switchOn()
            Zwembadpomp.switchOff().afterHours(5)
            item.cancelQueuedCommands()
            item.switchOff().silent().afterHours(5)
        end

        if not(ZwembadPH.active) then
            if dz.time.matchesRule("at 12:00") then
                Zwembadpomp.switchOn()
            elseif dz.time.matchesRule("at 16:00") then
                Zwembadpomp.switchOff().checkFirst()
            end

            if dz.time.matchesRule("at 20:00") then
                Zwembadpomp.switchOn()
            elseif dz.time.matchesRule("at 23:59") then
                Zwembadpomp.switchOff().checkFirst()
            end
        end

    end
}
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
drwurn
Posts: 68
Joined: Sunday 10 June 2018 16:59
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Does timer overrule specific time

Post by drwurn »

Thanks, can you explain the steps a bit, so I can understand what is happening? Did some reading on the wiki but have a little trouble understanding it :)

1 Where does this refer to "if item.isDevice and item.active then"
2 "cancelQueuedCommands()" does cancel every command further in the script? Why is it mentioned twice?
3 "silent()" What is the difference between this and checkFirst()?
drwurn
Posts: 68
Joined: Sunday 10 June 2018 16:59
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Does timer overrule specific time

Post by drwurn »

The pump doesn't go on when I activate 'ZwembadPH'.
When I switch the pump on and I activate 'ZwembadPH', the pump goes off.
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Does timer overrule specific time

Post by waaren »

drwurn wrote: Tuesday 12 May 2020 14:39 Thanks, can you explain the steps a bit, so I can understand what is happening? Did some reading on the wiki but have a little trouble understanding it :)

1 Where does this refer to "if item.isDevice and item.active then"
2 "cancelQueuedCommands()" does cancel every command further in the script? Why is it mentioned twice?
3 "silent()" What is the difference between this and checkFirst()?
item is the second parm of the execute function. In this case it can be a device or a timer rule

1. item.isDevice evaluates to true if the script is triggered by a device and false otherwise
item.active evaluates to true if the device is switched to "On"

2. cancelQueuedCommands() cancels all scheduled commands in domoticz for this device. So if there was a afterMin() command waiting until the time passed it will no longer be executed.
It is used twice because a cancelQueuedCommands() should be issued for both devices (zwembadPomp and zwembadPH )

3. silent() does send the command but do not trigger any follow up eventscripts. checkFirst() does not send the command it the state is already the same.

I added some logging to the script so you can see in the log what happens.

Code: Select all

return 
{
    on = 
    {
        timer = 
        {   
            'at 12:00',
            'at 16:00',
            'at 20:00',
            'at 23:59',
        },
        
        devices = 
        { 
            'ZwembadPH', 
        },
    },

    logging = 
    {
        level = domoticz.LOG_DEBUG,
    },
    
    execute = function(dz, item)
        local Zwembadpomp                   = dz.devices(47)
        local ZwembadPH                     = dz.devices(108)

        if item.isDevice then 
            dz.log(item.name .. '; active = ' .. item.active .. '. state = ' .. item.state, dz.LOG.DEBUG)
        end

        if item.isDevice and item.active then
            Zwembadpomp.cancelQueuedCommands()
            Zwembadpomp.switchOn()
            Zwembadpomp.switchOff().afterHours(5)
            item.cancelQueuedCommands()
            item.switchOff().silent().afterHours(5)
        end

        if not(ZwembadPH.active) then
            if dz.time.matchesRule('at 12:00') then
                Zwembadpomp.switchOn()
            elseif dz.time.matchesRule('at 16:00') then
                Zwembadpomp.switchOff().checkFirst()
            end

            if dz.time.matchesRule('at 20:00') then
                Zwembadpomp.switchOn()
            elseif dz.time.matchesRule('at 23:59') then
                Zwembadpomp.switchOff().checkFirst()
            end
        end

    end
}
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
drwurn
Posts: 68
Joined: Sunday 10 June 2018 16:59
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Does timer overrule specific time

Post by drwurn »

waaren wrote: Tuesday 12 May 2020 16:49
drwurn wrote: Tuesday 12 May 2020 14:39 Thanks, can you explain the steps a bit, so I can understand what is happening? Did some reading on the wiki but have a little trouble understanding it :)

1 Where does this refer to "if item.isDevice and item.active then"
2 "cancelQueuedCommands()" does cancel every command further in the script? Why is it mentioned twice?
3 "silent()" What is the difference between this and checkFirst()?
item is the second parm of the execute function. In this case it can be a device or a timer rule

1. item.isDevice evaluates to true if the script is triggered by a device and false otherwise
item.active evaluates to true if the device is switched to "On"

2. cancelQueuedCommands() cancels all scheduled commands in domoticz for this device. So if there was a afterMin() command waiting until the time passed it will no longer be executed.
It is used twice because a cancelQueuedCommands() should be issued for both devices (zwembadPomp and zwembadPH )

3. silent() does send the command but do not trigger any follow up eventscripts. checkFirst() does not send the command it the state is already the same.

I added some logging to the script so you can see in the log what happens.

Code: Select all

return 
{
    on = 
    {
        timer = 
        {   
            'at 12:00',
            'at 16:00',
            'at 20:00',
            'at 23:59',
        },
        
        devices = 
        { 
            'ZwembadPH', 
        },
    },

    logging = 
    {
        level = domoticz.LOG_DEBUG,
    },
    
    execute = function(dz, item)
        local Zwembadpomp                   = dz.devices(47)
        local ZwembadPH                     = dz.devices(108)

        if item.isDevice then 
            dz.log(item.name .. '; active = ' .. item.active .. '. state = ' .. item.state, dz.LOG.DEBUG)
        end

        if item.isDevice and item.active then
            Zwembadpomp.cancelQueuedCommands()
            Zwembadpomp.switchOn()
            Zwembadpomp.switchOff().afterHours(5)
            item.cancelQueuedCommands()
            item.switchOff().silent().afterHours(5)
        end

        if not(ZwembadPH.active) then
            if dz.time.matchesRule('at 12:00') then
                Zwembadpomp.switchOn()
            elseif dz.time.matchesRule('at 16:00') then
                Zwembadpomp.switchOff().checkFirst()
            end

            if dz.time.matchesRule('at 20:00') then
                Zwembadpomp.switchOn()
            elseif dz.time.matchesRule('at 23:59') then
                Zwembadpomp.switchOff().checkFirst()
            end
        end

    end
}

Code: Select all

2020-05-14 15:43:54.956 Error: dzVents: Error: (3.0.2) An error occurred when calling event handler Zwembad PH
2020-05-14 15:43:54.956 Error: dzVents: Error: (3.0.2) ...omoticz/scripts/dzVents/generated_scripts/Zwembad PH.lua:29: attempt to concatenate a boolean value (field 'active')
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Does timer overrule specific time

Post by waaren »

drwurn wrote: Thursday 14 May 2020 15:45

Code: Select all

2020-05-14 15:43:54.956 Error: dzVents: Error: (3.0.2) An error occurred when calling event handler Zwembad PH
2020-05-14 15:43:54.956 Error: dzVents: Error: (3.0.2) ...omoticz/scripts/dzVents/generated_scripts/Zwembad PH.lua:29: attempt to concatenate a boolean value (field 'active')
is this a question? If it is then change line 29 to

Code: Select all

            dz.log(item.name .. '; active = ' .. tostring(item.active) .. '. state = ' .. item.state, dz.LOG.DEBUG)
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
drwurn
Posts: 68
Joined: Sunday 10 June 2018 16:59
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Does timer overrule specific time

Post by drwurn »

Sorry, that was kinda short and not very friendly..

Btw thanks for your previous explanation to my questions.
I have changed line 29 and it gives another error.

Code: Select all

2020-05-14 17:08:55.822 Error: dzVents: Error: (3.0.2) An error occurred when calling event handler Zwembad PH
2020-05-14 17:08:55.822 Error: dzVents: Error: (3.0.2) ...omoticz/scripts/dzVents/generated_scripts/Zwembad PH.lua:29: attempt to index a nil value (field 'LOG')
I can do some basic dzvents but this is going to far for me, not sure what's happening and how to solve it.
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Does timer overrule specific time

Post by waaren »

drwurn wrote: Thursday 14 May 2020 17:11 I have changed line 29 and it gives another error.
My mistake. Replied a bit to quickly without double checking the lines I added.

If you change line 29 to this it should at least pass this line :)

Code: Select all

            dz.log(item.name .. '; active = ' .. tostring(item.active) .. '. state = ' .. item.state, dz.LOG_DEBUG)
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
drwurn
Posts: 68
Joined: Sunday 10 June 2018 16:59
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Does timer overrule specific time  [Solved]

Post by drwurn »

Thanks, just found 2 typo's.
'afterHours' needs to be 'afterHour'.
Thanks for your help :)
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest