Does this script hold up?

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 this script hold up?

Post by drwurn »

Hi,

Not sure if this works but idea of this script is:
The 'Zwembadpomp' turns on between 12.00 and 16.00
If I'm going to swim then I want to turn off the 'Zwembadpomp', that goes manually. If I turn it off manually (probably somewhere between 12:01 and 16:00) the 'Zwembadpomp' has to turn on between 20:00 and 23:59.

Code: Select all

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

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

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

        if Zwembadpomp.lastUpdate.minutesAgo < 479
        and Zwembadpomp.lastUpdate.minutesAgo > 240
        and dz.time.matchesRule("at 20:00") then
            Zwembadpomp.switchOn()
        elseif dz.time.matchesRule("at 23:59") then
            Zwembadpomp.switchOff()
        end
    end
}
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Does this script hold up?

Post by waaren »

drwurn wrote: Monday 20 April 2020 14:33 Not sure if this works but idea of this script is:
The 'Zwembadpomp' turns on between 12.00 and 16.00
If I'm going to swim then I want to turn off the 'Zwembadpomp', that goes manually. If I turn it off manually (probably somewhere between 12:01 and 16:00) the 'Zwembadpomp' has to turn on between 20:00 and 23:59.
Your if statements are not correct
you use
if a and if b and if c then
dzVents (Lua) expects
if a and b and c then

Code: Select all

local onAfternoon = 'at 12:00'
local offAfternoon = 'at 16:00'
local onEvening = 'at 20:00'
local offEvening = 'at 23:59'

return 
{ 
    on = 
    { 
        timer = 
        {
            onAfternoon, 
            offAfternoon,
            onEvening,
            offEvening,            
        },
    },

    logging = 
    {
        level = domoticz.LOG_DEBUG,
        marker = 'zwembad',
    },
    
    execute = function(dz, item)
        local Zwembadpomp = dz.devices(47)                    
        
        dz.log('Zwembadpomp trigger: ' .. item.trigger, dz.LOG_DEBUG)
        if item.trigger == onAfternoon  then
            Zwembadpomp.switchOn()
        elseif item.trigger == offAfternoon then
            Zwembadpomp.switchOff().checkFirst()
        elseif item.trigger == onEvening and Zwembadpomp.lastUpdate.minutesAgo > 240 and Zwembadpomp.lastUpdate.minutesAgo < 479 then 
            Zwembadpomp.switchOn()
        elseif item.trigger == offEvening  then
            Zwembadpomp.switchOff().checkFirst()
        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 this script hold up?

Post by drwurn »

Hi Waaren,

I saw it and changed it before you posted your ( I guess improved) script.

I see you put in some logging, and further a lot has changed :)
Is that because my version is not working properly or because my poorly way of making a script? :)
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Does this script hold up?

Post by waaren »

drwurn wrote: Monday 20 April 2020 15:49 I saw it and changed it before you posted your ( I guess improved) script.
I see you put in some logging, and further a lot has changed :)
Is that because my version is not working properly or because my poorly way of making a script? :)
Every person has an own style of coding. One is not necessarily better then the other. Forums where code styles are debated typically turn into shouting games and I certainly want to stay far from that. I just want to show there are always other ways of coding a solution.
One thing I do believe in strongly is to include the option to produce a bit of debug logging. Especial when you create and test a new script you will find that the behavior deviates quite often from what you think it will do :D
Once all immediate bugs are out, I set the logging level to domoticz.LOG_ERROR so that it will stay quiet in my log. Only when I or someone else sees an unexpected behavior of a script, I change it back to domoticz.LOG_DEBUG to see what is happening.
Call it a personal best practice... ;)
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 this script hold up?

Post by drwurn »

Thanks for the feedback and help!
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest