Timer trigger over midnight (and some other script problems)

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

Moderator: leecollings

Post Reply
AllesVanZelf
Posts: 265
Joined: Monday 05 February 2018 8:42
Target OS: Raspberry Pi / ODroid
Domoticz version: 12467
Location: Netherlands, near Haarlem
Contact:

Timer trigger over midnight (and some other script problems)

Post by AllesVanZelf »

From Blockly I know that if you want to have a time span over midnight, you have to do this as:
> sunset OR < sunrise.
How would I do this in DzVents?
Can I do this as:
Between sunset and sunrise?
Last edited by AllesVanZelf on Monday 08 June 2020 16:02, edited 2 times in total.
Domoticz 2020.1 (12230) on Raspberry Pi 3B with Raspian Buster. Besides Domoticz, Rpi is running Pi-Hole.
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Timer trigger over midnight

Post by waaren »

AllesVanZelf wrote: Monday 08 June 2020 13:08 > sunset OR < sunrise.
How would I do this in DzVents?
Can I do this as:
Between sunset and sunrise?
From the wiki chapter on timer trigger rules
'between aa and bb' -- aa/bb can be sunrise/sunset
and
'at nighttime', -- between sunset and sunrise
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
AllesVanZelf
Posts: 265
Joined: Monday 05 February 2018 8:42
Target OS: Raspberry Pi / ODroid
Domoticz version: 12467
Location: Netherlands, near Haarlem
Contact:

Re: Timer trigger over midnight

Post by AllesVanZelf »

Yes I've read that, but it was not clear to me that this would also work over midnight. (i missed that comments :( )
at nighttime is a good one!
Thanks again!
Domoticz 2020.1 (12230) on Raspberry Pi 3B with Raspian Buster. Besides Domoticz, Rpi is running Pi-Hole.
AllesVanZelf
Posts: 265
Joined: Monday 05 February 2018 8:42
Target OS: Raspberry Pi / ODroid
Domoticz version: 12467
Location: Netherlands, near Haarlem
Contact:

Re: Timer trigger over midnight

Post by AllesVanZelf »

These are some of my first steps in DzVents. A few scripts are working well and a little better then blockly's.
But this one gives me (almost) headache. ;)

Code: Select all

return
{
    on =
    {
        timer = 
        {
            'at nighttime',
        },
        devices =
        {
            'MS-Buiten',
        },
    },

    logging =
    {
        level = domoticz.LOG_DEBUG,
    },
    
    execute = function(dz)
        local voordeurlamp = dz.devices('Lamp Voordeur Schakelaar')
        local keukenlamp = dz.devices('Lamp-Keuken-Sier-433')
        local voorhuis = dz.devices('Lamp Voorhuis Powerplug')
        local motion = dz.devices('MS-Buiten').state
        
        if  dz.time.matchesRule('at daytime') and voordeurlamp == 'Off' and motion == 'On' then

            voordeurlamp.switchOn().silent().afterSec(20)
            keukenlamp.switchOn().silent().afterSec(40)
            voorhuis.switchOn().silent().afterSec(60)
            voorhuis.switchOff().afterSec(250)
            keukenlamp.switchOff().afterSec(300)
            voordeurlamp.switchOff().afterSec(400)

            dz.utils.osExecute('/home/pi/bin/sendsnapshot-nacht.sh')

        end
    end
}
This script should turn on some light in a special order and then turn them off after some time. In the same time the sh script should send me a snapshot. This snapshot works from commandline in raspberry os.
But nothing happens. No light, no message. (I tested with daytime.)

Log does not help me a lot:

Code: Select all

2020-06-08 15:40:34.018 Status: dzVents: Info: ------ Start internal script: Beweging buiten Licht DzVents: Device: "MS-Buiten (Virtuele schakelaarrs)", Index: 7
2020-06-08 15:40:34.019 Status: dzVents: Debug: Processing device-adapter for Lamp Voordeur Schakelaar: Switch device adapter
2020-06-08 15:40:34.021 Status: dzVents: Debug: Processing device-adapter for Lamp-Keuken-Sier-433: Switch device adapter
2020-06-08 15:40:34.023 Status: dzVents: Debug: Processing device-adapter for Lamp Voorhuis Powerplug: Switch device adapter
2020-06-08 15:40:34.023 Status: dzVents: Info: ------ Finished Beweging Licht DzVents 
There is something wrong in the code. but what?
Domoticz 2020.1 (12230) on Raspberry Pi 3B with Raspian Buster. Besides Domoticz, Rpi is running Pi-Hole.
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Timer trigger over midnight

Post by waaren »

AllesVanZelf wrote: Monday 08 June 2020 15:44 But this one gives me (almost) headache. ;)
There is something wrong in the code. but what?
First some remarks / questions.

this script will be triggered every minute at nighttime and every time MS-Buiten is updated. Is that your intention or do you only want it to be triggered when MS-Buiten is updated during nighttime?
You check if voordeurlamp is 'Off' but voordeurlamp is a device object. You probably want to check it's state like you do for motion.


it then would be something like below

Code: Select all

return
{
    on =
    {
        devices = 
		{
			['MS-Buiten'] = 
			{
				'at nighttime'
			},
		},
    },

    logging =
    {
        level = domoticz.LOG_DEBUG,
    },
    
    execute = function(dz)
        local voordeurlamp = dz.devices('Lamp Voordeur Schakelaar')
        local keukenlamp = dz.devices('Lamp-Keuken-Sier-433')
        local voorhuis = dz.devices('Lamp Voorhuis Powerplug')
        local motion = dz.devices('MS-Buiten').state
        
        if voordeurlamp.state == 'Off' and motion == 'On' then

            voordeurlamp.switchOn().silent().afterSec(20)
            keukenlamp.switchOn().silent().afterSec(40)
            voorhuis.switchOn().silent().afterSec(60)
            voorhuis.switchOff().afterSec(250)
            keukenlamp.switchOff().afterSec(300)
            voordeurlamp.switchOff().afterSec(400)

            os.execute('/home/pi/bin/sendsnapshot-nacht.sh')

        end
    end
}
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Timer trigger over midnight

Post by waaren »

AllesVanZelf wrote: Monday 08 June 2020 15:44 Log does not help me a lot:
If you need more logging, add some lines at relevant places in your script like

Code: Select all

dz.log('State of voordeurlamp is '  .. voordeurlamp.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
AllesVanZelf
Posts: 265
Joined: Monday 05 February 2018 8:42
Target OS: Raspberry Pi / ODroid
Domoticz version: 12467
Location: Netherlands, near Haarlem
Contact:

Re: Timer trigger over midnight

Post by AllesVanZelf »

waaren wrote: Monday 08 June 2020 17:36 this script will be triggered every minute at nighttime and every time MS-Buiten is updated. Is that your intention or do you only want it to be triggered when MS-Buiten is updated during nighttime?
Yes the latter. When MS Buiten is triggered at nighttime.
What do the brackets [] arround MS-Buiten mean in this case?
waaren wrote: Monday 08 June 2020 17:36You check if voordeurlamp is 'Off' but voordeurlamp is a device object. You probably want to check it's state like you do for motion.
That is true. That means that I forgot the .state part. isn't it?

I did not find the os.execute part in wiki. I did see something about osExecute(cmd) and read about it in some other posts. But I find it hard to use in code.
If the code works well do I change the log line:

Code: Select all

dz.log('State of voordeurlamp is '  .. voordeurlamp.state, dz.LOG_DEBUG )
to dz.LOG_INFO or just dz.LOG? Or just rmeove the line?

and what about this part:

Code: Select all

 logging =
    {
        level = domoticz.LOG_DEBUG,
    },
Domoticz 2020.1 (12230) on Raspberry Pi 3B with Raspian Buster. Besides Domoticz, Rpi is running Pi-Hole.
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Timer trigger over midnight

Post by waaren »

AllesVanZelf wrote: Monday 08 June 2020 18:17 [Yes the latter. When MS Buiten is triggered at nighttime.
What do the brackets [] arround MS-Buiten mean in this case?
it means that MS-Buiten is to be considered as a key in a Lua table.
That is true. That means that I forgot the .state part. isn't it?
Yes
I did not find the os.execute part in wiki. I did see something about osExecute(cmd) and read about it in some other posts. But I find it hard to use in code.
os.execute is standard Lua. dzVents is 100% Lua
In the dzVents wiki you will see the specific dzVents API (functions, attributes, structures) Guides on all standard Lua commands and structures are freely and in large amounts available on the web. As a start you can look here
If the code works well do I change the log line:

Code: Select all

dz.log('State of voordeurlamp is '  .. voordeurlamp.state, dz.LOG_DEBUG )
to dz.LOG_INFO or just dz.LOG? Or just rmeove the line?
Just leave it as it is
and what about this part:

Code: Select all

 logging =
    {
        level = domoticz.LOG_DEBUG,
    },
change that to

Code: Select all

 logging =
    {
        level = domoticz.LOG_ERROR,
    },
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
AllesVanZelf
Posts: 265
Joined: Monday 05 February 2018 8:42
Target OS: Raspberry Pi / ODroid
Domoticz version: 12467
Location: Netherlands, near Haarlem
Contact:

Re: Timer trigger over midnight (and some other script problems)

Post by AllesVanZelf »

Ok Thanks for these explanations!!
Domoticz 2020.1 (12230) on Raspberry Pi 3B with Raspian Buster. Besides Domoticz, Rpi is running Pi-Hole.
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest