Page 1 of 1

How to set timer to last day of the month

Posted: Thursday 28 January 2021 18:53
by Kranendijk
Hi all,

Thanks to the great help of this forum Inwas able to use dzvents for my energy meter. I have a script to copy the current week total to another device to store it as last week. A timer running each saturday at 23:55 does that job.
Now I want something similar for the month values. Like “rune every last day of the month at 23:55”. Should be possible but I am not able to figure that out.

Can’t be difficult, but I don’t see it...

Help appeciated. As always :D

Re: How to set timer to last day of the month

Posted: Thursday 28 January 2021 20:01
by HvdW
Cron does the job.
Take a look over here.

I'm testing this one:
If you're lucky to be using one of those implementations, it's as simple as:
55 23 L * * ?

Re: How to set timer to last day of the month

Posted: Thursday 28 January 2021 20:33
by waaren
Kranendijk wrote: Thursday 28 January 2021 18:53 Now I want something similar for the month values. Like “rune every last day of the month at 23:55”. Should be possible but I am not able to figure that out.
This could do it

Code: Select all

return
{
    on =
    {
        timer =
        {
            'at 23:55 on 28/2,29/2,30/*,31/*',
        },
    },

    execute = function(dz, item)
        if os.date("%d", os.time() + 24 * 3600 )  ~= '01' then
            dz.log(item.trigger .. " ==>> today is not the last day of the month" , dz.LOG_FORCE)
            return
        else
            dz.log(item.trigger .. " ==>> today is the last day of the month" , dz.LOG_FORCE)
        end

        dz.log('rest of the script below this line', dz.LOG_FORCE )
    end
}

Re: How to set timer to last day of the month

Posted: Thursday 28 January 2021 20:37
by Kranendijk
Really. That simple??
I’ll give it a shot, big thanks!

Re: How to set timer to last day of the month

Posted: Thursday 28 January 2021 21:21
by HvdW
That's 7 times twice on 30th and 31th

Re: How to set timer to last day of the month

Posted: Thursday 28 January 2021 21:40
by waaren
HvdW wrote: Thursday 28 January 2021 21:21 That's 7 times twice on 30th and 31th
True but in the execute part of the script there is an extra check that will stop the execution of the script if it is not the last day of the month, before the scripts real work starts.
Do you know a better way in pure dzVents ?

Re: How to set timer to last day of the month

Posted: Thursday 28 January 2021 22:12
by HvdW
Correct @waaren
For simplicity you could trigger it every day as well, like:

Code: Select all

return
{
    on =
    {
        timer =
        {
            'at 23:55',
        },
    },

    execute = function(dz, item)
        if os.date("%d", os.time() + 24 * 3600 )  ~= '01' then
            dz.log(item.trigger .. " ==>> today is not the last day of the month" , dz.LOG_FORCE)
            return
        else
            dz.log(item.trigger .. " ==>> today is the last day of the month" , dz.LOG_FORCE)
        end

        dz.log('rest of the script below this line', dz.LOG_FORCE )
    end
}

Re: How to set timer to last day of the month

Posted: Thursday 28 January 2021 23:07
by waaren
HvdW wrote: Thursday 28 January 2021 22:12 For simplicity you could trigger it every day as well, like:
The time rule looks indeed a bit intricate at first but that is only once and you save 365 - 19 = 346 unnecessary script triggers yearly by using it.
I can live with both approaches :D

Re: How to set timer to last day of the month

Posted: Friday 29 January 2021 22:21
by HvdW
Correct again @Waaren
I like script simplicity and computers don't care about repetition.
How many times a day is this script being run to check if it's 23:55?