Need to know if i'm in the night time or not

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

Moderator: leecollings

Post Reply
pipiche
Posts: 2027
Joined: Monday 02 April 2018 20:33
Target OS: Linux
Domoticz version:

Need to know if i'm in the night time or not

Post by pipiche »

I have found on the forum the idea to create a nighTime dzVents device. But I'm failing to make it working.
For sure I'm doing something wrong, but cannot find it

Code: Select all

cat test_nightTime.lua
local SCRIPT_NAME = 'testing_NightTime'

return {
    active = true,
    on = {
	timer = { 'every minute' },
        devices = {
		['nightTime'] = {'between 22:31 and 07:59'},
        },
    },

    logging =
    {
        level = domoticz.LOG_INFO,  -- change to LOG_ERROR when all OK
        marker = SCRIPT_NAME,
    },

    execute = function(domoticz, nightTime, item, timer)
	local time_window = domoticz.devices( 'nightTime' ).state

    	domoticz.log('system: '..domoticz.time.rawTime)
    	domoticz.log('Domoticz: '..domoticz.settings.domoticzVersion)
    	domoticz.log('domoticzvents: '..domoticz.settings.dzVentsVersion)
    	domoticz.log( ' --------the trigger---------' )
    	domoticz.log(item.name .. ' seconds ago ' ..item.lastUpdate.raw )

    	domoticz.log( ' nightTime ' ..time_window )
    end
}
Nov 06 17:57:00 pi3 domoticz[17721]: 2022-11-06 17:57:00.417 Status: dzVents: Info: testing_NightTime: ------ Start external script: test_nightTime.lua:, trigger: "every minute"
Nov 06 17:57:00 pi3 domoticz[17721]: 2022-11-06 17:57:00.418 Error: dzVents: Error: (3.1.8) testing_NightTime: There is no device with that name or id: nightTime
Nov 06 17:57:00 pi3 domoticz[17721]: 2022-11-06 17:57:00.419 Error: dzVents: Error: (3.1.8) testing_NightTime: An error occurred when calling event handler test_nightTime
Nov 06 17:57:00 pi3 domoticz[17721]: 2022-11-06 17:57:00.419 Error: dzVents: Error: (3.1.8) testing_NightTime: /var/lib/domoticz/scripts/dzVents/scripts/test_nightTime.lua:19: attempt to index a nil value
User avatar
boum
Posts: 136
Joined: Friday 18 January 2019 11:31
Target OS: Raspberry Pi / ODroid
Domoticz version: 4.10717
Location: France
Contact:

Re: Need to know if i'm in the night time or not

Post by boum »

Nov 06 17:57:00 pi3 domoticz[17721]: 2022-11-06 17:57:00.418 Error: dzVents: Error: (3.1.8) testing_NightTime: There is no device with that name or id: nightTime
There is no switch called nightTime, or there are several devices with that name and dzVents handles that badly.

If you want to test for night time in dzVent, just use the time property:

Code: Select all

	execute = function(dz, item)
	  -- [...]
		if dz.time.isNightTime then
		  -- do stuff only at night
		end
	  -- [...]
	end
Otherwise you must first create your device using the virtual hardware.
In your dzVents triggers, no need to add the device as trigger as nobody should change it. But then, you can also reset it to the correct value. But no point in putting a time interval.

Code: Select all

cat test_nightTime.lua
local SCRIPT_NAME = 'testing_NightTime'

return {
    active = true,
    on = {
	timer = { 'every minute' },
        devices = { 'nightTime' },
        },
    },

    logging =
    {
        level = domoticz.LOG_INFO,  -- change to LOG_ERROR when all OK
        marker = SCRIPT_NAME,
    },

    execute = function(domoticz, item)
	local device = domoticz.devices( 'nightTime' )
	if domoticz.time.isNightTime then
                device.switchOn().checkFirst()
	else
                device.switchOff().checkFirst()
	end
    end
}
pipiche
Posts: 2027
Joined: Monday 02 April 2018 20:33
Target OS: Linux
Domoticz version:

Re: Need to know if i'm in the night time or not

Post by pipiche »

in fact I was looking to define my own might time window. So probably I'll have to end up with testing the current time.
User avatar
boum
Posts: 136
Joined: Friday 18 January 2019 11:31
Target OS: Raspberry Pi / ODroid
Domoticz version: 4.10717
Location: France
Contact:

Re: Need to know if i'm in the night time or not

Post by boum »

This can be done easily too. You still need to create your virtual switch named nightTime (and have only 1 device with this name)

Code: Select all

local SCRIPT_NAME = 'testing_NightTime'
local DUSK_TIME = 'at 22:31'
local DAWN_TIME = 'at 7:58'

return {
    active = true,
    on = {
	timer = { DUSK_TIME, DAWN_TIME },
        },
    },

    logging =
    {
        level = domoticz.LOG_INFO,  -- change to LOG_ERROR when all OK
        marker = SCRIPT_NAME,
    },

    execute = function(domoticz, item)
	local device = domoticz.devices( 'nightTime' )
	if item.trigger == DUSK_TIME then
                device.switchOn().checkFirst()
	elseif item.trigger == DAWN_TIME then
                device.switchOff().checkFirst()
        else 
            	domoticz.log('Unknown trigger for nighTime ', domoticz.LOG_ERROR)
	end
    end
}
then, in your other scripts, you can test for:

Code: Select all

domoticz.devices('nightTime').active
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest