Page 1 of 1

Doorbell routine

Posted: Tuesday 07 January 2020 20:45
by fvdp80
Can somebody please help me figuring out how to make a dzvents script?

Devices: Doorbell, Door_Sensor, Outdoor_Light, Hallway_Light
At Nighttime the Outdoor_Light will be On at 20%.

The routine i'm looking for is when the Doorbell is pressed at Nightime. Then the following needs to happen:

Outdoor_Light set to 100%.
Hallway_Light set to On.
If Door_Sensor stays 'closed' for 1 minute then turn Off Hallway_Light and set Outdoor_Light to 20%.
If Door_Sensor is 'open' then leave Hallway_Light On and Outdoor_Light to 100%. When Door_Sensor is 'closed' again
turn Off Hallway_Light after 30 seconds and set Outdoor_Light to 20% after 1 minute.

Re: Doorbell routine

Posted: Tuesday 07 January 2020 21:03
by ronaldbro
I almost do the same only my hallway light is still old fashion and I replaced my doorbell with a motion sensor.
See my script below.

The variable var_idx_dagNacht indicated if it's day or night. The value of the variable is defined in two constants in global data. CONST_DAGNACHT_NACHT And CONST_DAGNACHT_DAG

Code: Select all

 local DIMLEVEL_IDLE = 1
local DIMLEVEL_ACTIVITY = 25
local SECONDS_ACTIVITY_LEVEL = 60

local idx_deurbel  = idx_gpio_26
local idx_voordeur = idx_hal_voordeur
local idx_beweging = idx_hal_voordeurBeweging

return {
	on = {
		devices = {
			idx_voordeur,
--			idx_deurbel,
			idx_beweging
		},

		variables = {
			var_idx_dagNacht
		}
	},

	logging = {
--        level = domoticz.LOG_DEBUG,
--        level = domoticz.LOG_INFO,
        level = domoticz.LOG_ERROR,
        marker = "Hal - Voordeurverlichting"
    },

	execute = function(dz, triggeredItem, info)

        local lamp = dz.devices(idx_hal_voordeurLuifel)
        local dagNacht = dz.variables(var_idx_dagNacht)
   
        if triggeredItem.isVariable then
            if dagNacht.value == CONST_DAGNACHT_NACHT then
                dz.log('NightTime: Set lvl to ' .. DIMLEVEL_IDLE)
                lamp.cancelQueuedCommands()
                lamp.dimTo(DIMLEVEL_IDLE)
            elseif dagNacht.value == CONST_DAGNACHT_DAG then
                dz.log('DayTime: switch lamp off.')
                lamp.cancelQueuedCommands()
                lamp.switchOff()
            end
        elseif triggeredItem.isDevice and dagNacht.value == CONST_DAGNACHT_NACHT then
            if triggeredItem.idx == idx_voordeur and triggeredItem.active then
                dz.log('Set lvl to ' .. DIMLEVEL_ACTIVITY)
                lamp.cancelQueuedCommands()
                lamp.dimTo(DIMLEVEL_ACTIVITY)
            elseif triggeredItem.idx == idx_voordeur and triggeredItem.active == false then
                dz.log('Set lvl to ' .. DIMLEVEL_IDLE .. 'after 15 seconds.')
                lamp.cancelQueuedCommands()
                lamp.dimTo(DIMLEVEL_IDLE).afterSec(15)
            elseif (triggeredItem.idx == idx_deurbel or idx_beweging) and triggeredItem.active and dz.devices(idx_voordeur).active == false then
                dz.log('Set lvl to ' .. DIMLEVEL_ACTIVITY .. ' for ' .. SECONDS_ACTIVITY_LEVEL .. ' seconds.')
                lamp.cancelQueuedCommands()
                lamp.dimTo(DIMLEVEL_ACTIVITY)
                lamp.dimTo(DIMLEVEL_IDLE).afterSec(SECONDS_ACTIVITY_LEVEL)
            end
        end

    end
}

Re: Doorbell routine

Posted: Tuesday 07 January 2020 23:48
by waaren
fvdp80 wrote: Tuesday 07 January 2020 20:45 Can somebody please help me figuring out how to make a dzvents script?
Can you try this one ?

Code: Select all

--[[

Devices: doorbell, Door_Sensor, Outdoor_Light, Hallway_Light
At Nighttime the Outdoor_Light will be On at 20%.

The routine i'm looking for is when the doorbell is pressed at Nightime. Then the following needs to happen:

Outdoor_Light set to 100%.
Hallway_Light set to On.
If Door_Sensor stays 'closed' for 1 minute then turn Off Hallway_Light and set Outdoor_Light to 20%.
If Door_Sensor is 'open' then leave Hallway_Light On and Outdoor_Light to 100%. When Door_Sensor is 'closed' again
turn Off Hallway_Light after 30 seconds and set Outdoor_Light to 20% after 1 minute.
  
]]--

local doorbell = 'Doorbell'
local doorsensor = 'Door_Sensor'

return
{
    on =
    {
        devices =
        {
            [doorbell] = { 'at nighttime' },
            [doorsensor] = { 'at nighttime' },
        },
        
        timer =
        {
            'at sunset',
            'at sunrise',
        },
    },

    logging =  
    {
        level = domoticz.LOG_DEBUG, -- set to LOG_ERROR when tested and OK
    },

    data =
    {
        doorbellPressedAt = { initial = 0 },
        doorsensorOpenedAt = { initial = 0 },
        hallwayLightManual = { initial = false },
    },
      
    execute = function(dz, item)
        _G.logMarker =  _G.moduleLabel -- set logmarker to scrptname
       
        outdoorLight = dz.devices('Outdoor_Light')
        hallwayLight = dz.devices('Hallway_Light')
        doorsensor = dz.devices(doorsensor)
        doorbell = dz.devices(doorbell)

        if _G.logLevel == dz.LOG_DEBUG then
            if item.isDevice then 
   		 dz.log(item.name .. ' is ' .. ( item.active and ' active.' or ' not active.' ) ,dz.LOG_DEBUG ) 
	    end
            dz.log(outdoorLight.name .. ' state is ' .. outdoorLight.state,dz.LOG_DEBUG )
            dz.log(hallwayLight.name .. ' state is ' .. hallwayLight.state,dz.LOG_DEBUG )
            dz.log(doorsensor.name .. ' state is ' .. doorsensor.state,dz.LOG_DEBUG )
            dz.log(doorbell.name .. ' state is ' .. doorbell.state,dz.LOG_DEBUG )
            dz.log('------ content of dz.data before script logic ----- ',dz.LOG_DEBUG)
            dz.utils.dumpTable(dz.data)
        end
       
        if item.isTimer then
            if dz.time.matchesRule('at sunrise') then 
                outdoorLight.dimTo(0)
            else
                outdoorLight.dimTo(20)
            end    
        -- when the doorbell is pressed at Nightime.
        elseif item == doorbell and item.active then
            -- clear any previous scheduled commands ( in case doorbell was pressed multiple times )
            outdoorLight.cancelQueuedCommands() -- no dimTo(20)   
            hallwayLight.cancelQueuedCommands() -- no switchOff()
           
            -- Outdoor_Light set to 100% and Hallway_Light set to On
            outdoorLight.dimTo(100)
            -- If Door_Sensor stays 'closed' for 1 minute then reset Outdoor_Light to 20%.
            outdoorLight.dimTo(20).afterSec(60)
           
            -- only when not already on
            dz.data.hallwayLightManual = hallwayLight.active
            if not hallwayLight.active then
                hallwayLight.switchOn()
                hallwayLight.switchOff().afterSec(60)
            end
           
            dz.data.doorbellPressedAt = dz.time.dDate
            dz.data.doorsensorOpenedAt = 0
       
        --If Door_Sensor is 'open' then leave Hallway_Light On and Outdoor_Light to 100%.
        elseif item == doorsensor and item.active and dz.data.doorbellPressedAt >= ( dz.time.dDate - 60 ) then -- doorbell was pressed within last minute
            outdoorLight.cancelQueuedCommands() -- no dimTo(20)   
            hallwayLight.cancelQueuedCommands() -- no switchOff()
            dz.data.doorsensorOpenedAt = dz.time.dDate
       
        -- When Door_Sensor is 'closed' again turn Off Hallway_Light after 30 seconds and set Outdoor_Light to 20% after 1 minute.
        elseif item == doorsensor and not(item.active) and dz.data.doorsensorOpenedAt ~= 0 then -- doorsensor was open after doorbell was pressed
            if outdoorLight.level ~= 20 then outdoorLight.dimTo(20).afterSec(60) end
            if not dz.data.hallwayLightManual then -- if manual switched then leave it on
                hallwayLight.switchOff().checkFirst().afterSec(30)
            end
            dz.data.doorsensorOpenedAt = 0
        end

        if _G.logLevel == dz.LOG_DEBUG then
            dz.log('------ content of dz.data after script logic ----- ',dz.LOG_DEBUG)
            dz.utils.dumpTable(dz.data)
        end

    end
}

Re: Doorbell routine

Posted: Wednesday 08 January 2020 20:21
by fvdp80
Hi waaren, thnx for the great script! Running into a few problems though.
I did some tweaking to make it work (dimTo doesn't work with the Ikea lights so i used a dummy selector switch).

This is my script thus far.

Code: Select all

local doorbell = 'Voordeur - Bel'
local doorsensor = 'Voordeur - Sensor'

return
{
    on =
    {
        devices =
        {
            [doorbell] = { 'at nighttime' },
            [doorsensor] = { 'at nighttime' },
        },
        
        timer =
        {
            'at sunset',
            'at sunrise',
        },
    },

    logging =  
    {
        level = domoticz.LOG_DEBUG, -- set to LOG_ERROR when tested and OK
    },

    data =
    {
        doorbellPressedAt = { initial = 0 },
        doorsensorOpenedAt = { initial = 0 },
        hallwayLightManual = { initial = false },
    },
      
    execute = function(dz, item)
        _G.logMarker =  _G.moduleLabel -- set logmarker to scrptname
       
        outdoorLight = dz.devices(113)
        hallwayLight = dz.groups(5)
        doorsensor = dz.devices(doorsensor)
        doorbell = dz.devices(doorbell)

        if _G.logLevel == dz.LOG_DEBUG then
            if item.isDevice then 
    		dz.log(item.name .. ' is ' .. ( item.active and ' active.' or ' not active.' ) ,dz.LOG_DEBUG ) 
	    end
            dz.log(outdoorLight.name .. ' state is ' .. outdoorLight.state,dz.LOG_DEBUG )
            dz.log(hallwayLight.name .. ' state is ' .. hallwayLight.state,dz.LOG_DEBUG )
            dz.log(doorsensor.name .. ' state is ' .. doorsensor.state,dz.LOG_DEBUG )
            dz.log(doorbell.name .. ' state is ' .. doorbell.state,dz.LOG_DEBUG )
            dz.log('------ content of dz.data before script logic ----- ',dz.LOG_DEBUG)
            dz.utils.dumpTable(dz.data)
        end
       
        if item.isTimer then
            if dz.time.matchesRule('at sunrise') then 
                outdoorLight.switchSelector(0)
            else
                outdoorLight.switchSelector(10)
            end    
        -- when the doorbell is pressed at Nightime.
        elseif item == doorbell and item.state == 'Click' or 'Long Click' or 'Double Click' then
            -- clear any previous scheduled commands ( in case doorbell was pressed multiple times )
            outdoorLight.cancelQueuedCommands() -- no dimTo(20)   
            hallwayLight.cancelQueuedCommands() -- no switchOff()
           
            -- Outdoor_Light set to 100% and Hallway_Light set to On
            outdoorLight.switchSelector(20)
            -- If Door_Sensor stays 'closed' for 1 minute then reset Outdoor_Light to 20%.
            outdoorLight.switchSelector(10).afterSec(60)
           
           -- reset state of doorbell
           item.switchOff().afterSec(60).silent() -- to prevent a retrigger of the script
           
            -- only when not already on
            dz.data.hallwayLightManual = hallwayLight.active
            if not hallwayLight.active then
                hallwayLight.switchOn()
                hallwayLight.switchOff().afterSec(60)
            end
           
            dz.data.doorbellPressedAt = dz.time.dDate
            dz.data.doorsensorOpenedAt = 0
       
        --If Door_Sensor is 'open' then leave Hallway_Light On and Outdoor_Light to 100%.
        elseif item == doorsensor and item.active and dz.data.doorbellPressedAt >= ( dz.time.dDate - 60 ) then -- doorbell was pressed within last minute
            outdoorLight.cancelQueuedCommands() -- no dimTo(20)   
            hallwayLight.cancelQueuedCommands() -- no switchOff()
            dz.data.doorsensorOpenedAt = dz.time.dDate
            
       
        -- When Door_Sensor is 'closed' again turn Off Hallway_Light after 30 seconds and set Outdoor_Light to 20% after 1 minute.
        elseif item == doorsensor and not(item.active) and dz.data.doorsensorOpenedAt ~= 0 then -- doorsensor was open after doorbell was pressed
            if outdoorLight.level ~= 20 then outdoorLight.switchSelector(10).afterSec(60) end
            if not dz.data.hallwayLightManual then -- if manual switched then leave it on
                hallwayLight.switchOff().checkFirst().afterSec(30)
            end
            dz.data.doorsensorOpenedAt = 0
        end

        if _G.logLevel == dz.LOG_DEBUG then
            dz.log('------ content of dz.data after script logic ----- ',dz.LOG_DEBUG)
            dz.utils.dumpTable(dz.data)
        end

    end
}
The script is partially working (ringing the doorbell without opening the door).
The following part does not:

When someone rings the doorbell and I open the door within 60 seconds, the Outdoor_Light is still set to switchSelector(10) and Hallway_Light is still set to off after 60 seconds instead of stay both on until I close the door.

Log:

Code: Select all

2020-01-08 20:11:01.772 Status: dzVents: Info: Handling events for: "Voordeur - Bel", value: "Click"
2020-01-08 20:11:01.773 Status: dzVents: Info: ------ Start internal script: Deurbel: Device: "Voordeur - Bel (Zigbee2MQTT)", Index: 112
2020-01-08 20:11:01.783 Status: dzVents: Debug: Deurbel: Processing device-adapter for Voordeur - Lamp - Selector: Switch device adapter
2020-01-08 20:11:01.789 Status: dzVents: Debug: Deurbel: Processing device-adapter for Hal - Verlichting: Group device adapter
2020-01-08 20:11:01.796 Status: dzVents: Debug: Deurbel: Processing device-adapter for Voordeur - Sensor: Switch device adapter
2020-01-08 20:11:01.797 Status: dzVents: Debug: Deurbel: Voordeur - Bel is not active.
2020-01-08 20:11:01.798 Status: dzVents: Debug: Deurbel: Voordeur - Lamp - Selector state is 20%
2020-01-08 20:11:01.798 Status: dzVents: Debug: Deurbel: Hal - Verlichting state is Off
2020-01-08 20:11:01.798 Status: dzVents: Debug: Deurbel: Voordeur - Sensor state is Closed
2020-01-08 20:11:01.799 Status: dzVents: Debug: Deurbel: Voordeur - Bel state is Click
2020-01-08 20:11:01.799 Status: dzVents: Debug: Deurbel: ------ content of dz.data before script logic -----
2020-01-08 20:11:01.799 Status: dzVents: > hallwayLightManual: false
2020-01-08 20:11:01.800 Status: dzVents: > doorsensorOpenedAt: 0
2020-01-08 20:11:01.800 Status: dzVents: > doorbellPressedAt: 1578510361
2020-01-08 20:11:01.800 Status: dzVents: > initialize()
2020-01-08 20:11:01.801 Status: dzVents: Debug: Deurbel: Constructed timed-command: Set Level 20
2020-01-08 20:11:01.802 Status: dzVents: Debug: Deurbel: Constructed timed-command: Set Level 10
2020-01-08 20:11:01.803 Status: dzVents: Debug: Deurbel: Constructed timed-command: Set Level 10 AFTER 60 SECONDS
2020-01-08 20:11:01.803 Status: dzVents: Debug: Deurbel: Constructed timed-command: Off
2020-01-08 20:11:01.804 Status: dzVents: Debug: Deurbel: Constructed timed-command: Off AFTER 60 SECONDS
2020-01-08 20:11:01.805 Status: dzVents: Debug: Deurbel: Constructed timed-command: Off AFTER 60 SECONDS NOTRIGGER
2020-01-08 20:11:01.806 Status: dzVents: Debug: Deurbel: Constructed timed-command: On
2020-01-08 20:11:01.807 Status: dzVents: Debug: Deurbel: Constructed timed-command: Off
2020-01-08 20:11:01.808 Status: dzVents: Debug: Deurbel: Constructed timed-command: Off AFTER 60 SECONDS
2020-01-08 20:11:01.809 Status: dzVents: Debug: Deurbel: ------ content of dz.data after script logic -----
2020-01-08 20:11:01.809 Status: dzVents: > hallwayLightManual: false
2020-01-08 20:11:01.809 Status: dzVents: > doorsensorOpenedAt: 0
2020-01-08 20:11:01.810 Status: dzVents: > doorbellPressedAt: 1578510661
2020-01-08 20:11:01.810 Status: dzVents: > initialize()
2020-01-08 20:11:01.814 Status: dzVents: Info: Deurbel: ------ Finished Deurbel
2020-01-08 20:11:01.846 Status: EventSystem: Script event triggered: /home/pi/domoticz/dzVents/runtime/dzVents.lua
2020-01-08 20:11:02.353 Status: User: Admin initiated a switch command (98/Voordeur - Lamp/Set Level)
2020-01-08 20:12:02.373 Status: User: Admin initiated a switch command (98/Voordeur - Lamp/Set Level)
2020-01-08 20:12:11.513 Status: dzVents: Info: ------ Start internal script: Deurbel: Device: "Voordeur - Sensor (Zigbee2MQTT)", Index: 109
2020-01-08 20:12:11.523 Status: dzVents: Debug: Deurbel: Processing device-adapter for Voordeur - Lamp - Selector: Switch device adapter
2020-01-08 20:12:11.529 Status: dzVents: Debug: Deurbel: Processing device-adapter for Hal - Verlichting: Group device adapter
2020-01-08 20:12:11.537 Status: dzVents: Debug: Deurbel: Processing device-adapter for Voordeur - Bel: Switch device adapter
2020-01-08 20:12:11.538 Status: dzVents: Debug: Deurbel: Voordeur - Sensor is not active.
2020-01-08 20:12:11.538 Status: dzVents: Debug: Deurbel: Voordeur - Lamp - Selector state is 20%
2020-01-08 20:12:11.538 Status: dzVents: Debug: Deurbel: Hal - Verlichting state is Off
2020-01-08 20:12:11.539 Status: dzVents: Debug: Deurbel: Voordeur - Sensor state is Closed
2020-01-08 20:12:11.539 Status: dzVents: Debug: Deurbel: Voordeur - Bel state is Off
2020-01-08 20:12:11.539 Status: dzVents: Debug: Deurbel: ------ content of dz.data before script logic -----
2020-01-08 20:12:11.539 Status: dzVents: > initialize()
2020-01-08 20:12:11.540 Status: dzVents: > doorbellPressedAt: 1578510661
2020-01-08 20:12:11.540 Status: dzVents: > doorsensorOpenedAt: 0
2020-01-08 20:12:11.540 Status: dzVents: > hallwayLightManual: false
2020-01-08 20:12:11.541 Status: dzVents: Debug: Deurbel: Constructed timed-command: Set Level 20
2020-01-08 20:12:11.542 Status: dzVents: Debug: Deurbel: Constructed timed-command: Set Level 10
2020-01-08 20:12:11.546 Status: dzVents: Debug: Deurbel: Constructed timed-command: Set Level 10 AFTER 60 SECONDS
2020-01-08 20:12:11.547 Status: dzVents: Debug: Deurbel: Constructed timed-command: Off
2020-01-08 20:12:11.548 Status: dzVents: Debug: Deurbel: Constructed timed-command: Off AFTER 60 SECONDS
2020-01-08 20:12:11.549 Status: dzVents: Debug: Deurbel: Constructed timed-command: Off AFTER 60 SECONDS NOTRIGGER
2020-01-08 20:12:11.550 Status: dzVents: Debug: Deurbel: Constructed timed-command: On
2020-01-08 20:12:11.551 Status: dzVents: Debug: Deurbel: Constructed timed-command: Off
2020-01-08 20:12:11.552 Status: dzVents: Debug: Deurbel: Constructed timed-command: Off AFTER 60 SECONDS
2020-01-08 20:12:11.552 Status: dzVents: Debug: Deurbel: ------ content of dz.data after script logic -----
2020-01-08 20:12:11.552 Status: dzVents: > initialize()
2020-01-08 20:12:11.553 Status: dzVents: > doorbellPressedAt: 1578510731
2020-01-08 20:12:11.553 Status: dzVents: > doorsensorOpenedAt: 0
2020-01-08 20:12:11.553 Status: dzVents: > hallwayLightManual: false
2020-01-08 20:12:11.557 Status: dzVents: Info: Deurbel: ------ Finished Deurbel
2020-01-08 20:12:11.576 Status: EventSystem: Script event triggered: /home/pi/domoticz/dzVents/runtime/dzVents.lua
2020-01-08 20:12:12.081 Status: User: Admin initiated a switch command (98/Voordeur - Lamp/Set Level)
2020-01-08 20:13:12.108 Status: User: Admin initiated a switch command (98/Voordeur - Lamp/Set Level)
Also when I open the door without the doorbell beeing pressed, both the lights go on after closing the door.

Code: Select all

2020-01-08 20:18:48.919 Status: dzVents: Debug: Deurbel: Processing device-adapter for Voordeur - Bel: Switch device adapter
2020-01-08 20:18:48.920 Status: dzVents: Debug: Deurbel: Voordeur - Sensor is not active.
2020-01-08 20:18:48.921 Status: dzVents: Debug: Deurbel: Voordeur - Lamp - Selector state is 20%
2020-01-08 20:18:48.921 Status: dzVents: Debug: Deurbel: Hal - Verlichting state is Off
2020-01-08 20:18:48.921 Status: dzVents: Debug: Deurbel: Voordeur - Sensor state is Closed
2020-01-08 20:18:48.922 Status: dzVents: Debug: Deurbel: Voordeur - Bel state is Off
2020-01-08 20:18:48.922 Status: dzVents: Debug: Deurbel: ------ content of dz.data before script logic -----
2020-01-08 20:18:48.922 Status: dzVents: > doorsensorOpenedAt: 0
2020-01-08 20:18:48.922 Status: dzVents: > initialize()
2020-01-08 20:18:48.923 Status: dzVents: > doorbellPressedAt: 1578510731
2020-01-08 20:18:48.923 Status: dzVents: > hallwayLightManual: false
2020-01-08 20:18:48.924 Status: dzVents: Debug: Deurbel: Constructed timed-command: Set Level 20
2020-01-08 20:18:48.925 Status: dzVents: Debug: Deurbel: Constructed timed-command: Set Level 10
2020-01-08 20:18:48.930 Status: dzVents: Debug: Deurbel: Constructed timed-command: Set Level 10 AFTER 60 SECONDS
2020-01-08 20:18:48.930 Status: dzVents: Debug: Deurbel: Constructed timed-command: Off
2020-01-08 20:18:48.931 Status: dzVents: Debug: Deurbel: Constructed timed-command: Off AFTER 60 SECONDS
2020-01-08 20:18:48.932 Status: dzVents: Debug: Deurbel: Constructed timed-command: Off AFTER 60 SECONDS NOTRIGGER
2020-01-08 20:18:48.933 Status: dzVents: Debug: Deurbel: Constructed timed-command: On
2020-01-08 20:18:48.934 Status: dzVents: Debug: Deurbel: Constructed timed-command: Off
2020-01-08 20:18:48.935 Status: dzVents: Debug: Deurbel: Constructed timed-command: Off AFTER 60 SECONDS
2020-01-08 20:18:48.935 Status: dzVents: Debug: Deurbel: ------ content of dz.data after script logic -----
2020-01-08 20:18:48.936 Status: dzVents: > doorsensorOpenedAt: 0
2020-01-08 20:18:48.936 Status: dzVents: > initialize()
2020-01-08 20:18:48.936 Status: dzVents: > doorbellPressedAt: 1578511128
2020-01-08 20:18:48.937 Status: dzVents: > hallwayLightManual: false
2020-01-08 20:18:48.944 Status: dzVents: Info: Deurbel: ------ Finished Deurbel
2020-01-08 20:18:48.963 Status: EventSystem: Script event triggered: /home/pi/domoticz/dzVents/runtime/dzVents.lua

Re: Doorbell routine

Posted: Wednesday 08 January 2020 21:16
by waaren
fvdp80 wrote: Wednesday 08 January 2020 20:21 Hi waaren, thnx for the great script! Running into a few problems though.
I did some tweaking to make it work (dimTo doesn't work with the Ikea lights so i used a dummy selector switch).
Sorry to see it is not working for you. I fully tested the script on lights that do switchOn / switchOff and react to dimTo() commands as they supposed to do in domoticz and I don't want to go into a route to tweak it for devices that do not follow this standard.
So it is either up to you to change it in a way that it works for your devices. But my advice would be to ask the developer of the interface to your Ikea bulbs to conform to the domoticz model. Too much frustration already for some (many ?) forum members if they find out stuff like silent(), cancelQueuedCommands() and now even dimTo() does not work as designed for their devices.

Re: Doorbell routine  [Solved]

Posted: Friday 10 January 2020 14:34
by fvdp80
Managed to get it to work with some dummy switches. Will contact the plugin developer to implement all the dzvents commands into the plugin.
Thanks for your help waaren.

This is the code i'm using right now.

Code: Select all

local doorbell = 'Voordeur - Bel'
local doorsensor = 'Voordeur - Sensor'

return
{
    on =
    {
        devices =
        {
            [doorbell] = { 'at nighttime' },
            [doorsensor] = { 'at nighttime' },
        },
        
        timer =
        {
            'at sunset',
            'at sunrise',
        },
    },

    logging =  
    {
        level = domoticz.LOG_DEBUG, -- set to LOG_ERROR when tested and OK
    },

    data =
    {
        doorbellPressedAt = { initial = 0 },
        doorsensorOpenedAt = { initial = 0 },
        hallwayLightManual = { initial = false },
    },
      
    execute = function(dz, item)
        _G.logMarker =  _G.moduleLabel -- set logmarker to scrptname
       
        outdoorLight = dz.devices(113)
        hallwayLight = dz.devices(114)
        doorsensor = dz.devices(doorsensor)
        doorbell = dz.devices(doorbell)

        if _G.logLevel == dz.LOG_DEBUG then
             if item.isDevice then 
                 dz.log(item.name .. ' is ' .. ( item.active and ' active.' or ' not active.' ) ,dz.LOG_DEBUG ) 
            end
            dz.log(outdoorLight.name .. ' state is ' .. outdoorLight.state,dz.LOG_DEBUG )
            dz.log(hallwayLight.name .. ' state is ' .. hallwayLight.state,dz.LOG_DEBUG )
            dz.log(doorsensor.name .. ' state is ' .. doorsensor.state,dz.LOG_DEBUG )
            dz.log(doorbell.name .. ' state is ' .. doorbell.state,dz.LOG_DEBUG )
            dz.log('------ content of dz.data before script logic ----- ',dz.LOG_DEBUG)
            dz.utils.dumpTable(dz.data)
        end
       
        if item.isTimer then
            if dz.time.matchesRule('at sunrise') then 
                outdoorLight.switchSelector(0)
            else
                outdoorLight.switchSelector(10)
            end    
            
        -- when the doorbell is pressed at Nightime.
        elseif item == doorbell and not(item.state == 'Off') then
            -- clear any previous scheduled commands ( in case doorbell was pressed multiple times )
            outdoorLight.cancelQueuedCommands() -- no dimTo(20)   
            hallwayLight.cancelQueuedCommands() -- no switchOff()
           
            -- Outdoor_Light set to 100% and Hallway_Light set to On
            outdoorLight.switchSelector(20)
            -- If Door_Sensor stays 'closed' for 1 minute then reset Outdoor_Light to 20%.
            outdoorLight.switchSelector(10).afterSec(60)
           
            -- only when not already on
            dz.data.hallwayLightManual = hallwayLight.active
            if not hallwayLight.active then
                hallwayLight.switchOn().afterSec(5)
                hallwayLight.switchOff().afterSec(60)
            end
           
            dz.data.doorbellPressedAt = dz.time.dDate
            dz.data.doorsensorOpenedAt = 0
       
        --If Door_Sensor is 'open' then leave Hallway_Light On and Outdoor_Light to 100%.
        elseif item == doorsensor and item.active and dz.data.doorbellPressedAt >= ( dz.time.dDate - 60 ) then -- doorbell was pressed within last minute
            outdoorLight.cancelQueuedCommands() -- no dimTo(20)   
            hallwayLight.cancelQueuedCommands() -- no switchOff()
            dz.data.doorsensorOpenedAt = dz.time.dDate
            
       
        -- When Door_Sensor is 'closed' again turn Off Hallway_Light after 30 seconds and set Outdoor_Light to 20% after 1 minute.
        elseif item == doorsensor and not(item.active) and dz.data.doorsensorOpenedAt ~= 0 then -- doorsensor was open after doorbell was pressed
            outdoorLight.switchSelector(10).afterSec(60)
            if not dz.data.hallwayLightManual then -- if manual switched then leave it on
                hallwayLight.switchOff().checkFirst().afterSec(10)
            end
            dz.data.doorsensorOpenedAt = 0
            dz.data.doorbellPressedAt = 0
        end

        if _G.logLevel == dz.LOG_DEBUG then
            dz.log('------ content of dz.data after script logic ----- ',dz.LOG_DEBUG)
            dz.utils.dumpTable(dz.data)
        end

    end
}

Re: Doorbell routine

Posted: Friday 10 January 2020 16:49
by waaren
fvdp80 wrote: Friday 10 January 2020 14:34 Managed to get it to work with some dummy switches. Will contact the plugin developer to implement all the dzvents commands into the plugin.
Good to see it works now.

I do not understand completely what the effect of the switchSelector is on the Ikea bulbs. Normal behavior should be similar to dimTo() so switchSelector(20) = dimTo(20) but here it looks like switchSelector(20) = dimTo(100) ?

Re: Doorbell routine

Posted: Friday 10 January 2020 19:13
by fvdp80
waaren wrote: Friday 10 January 2020 16:49 I do not understand completely what the effect of the switchSelector is on the Ikea bulbs. Normal behavior should be similar to dimTo() so switchSelector(20) = dimTo(20) but here it looks like switchSelector(20) = dimTo(100) ?
There is no direct connection. I created a dummy selector switch with 3 levels (0%, 20%, 100%). Each level fires a jason command to set the right dimlevel. (example: http://x.x.x.x:8080/json.htm?type=comma ... l&level=20)

I now see that with sunset I get an error on the script. Do you know what causes this?

Code: Select all

2020-01-10 16:53:00.406 Status: dzVents: Info: ------ Start internal script: Deurbel:, trigger: at sunset
2020-01-10 16:53:00.557 Status: dzVents: Debug: Deurbel: Processing device-adapter for Voordeur - Lamp - Selector: Switch device adapter
2020-01-10 16:53:00.565 Status: dzVents: Debug: Deurbel: Processing device-adapter for Hal - Lamp - Dummy: Switch device adapter
2020-01-10 16:53:00.573 Status: dzVents: Debug: Deurbel: Processing device-adapter for Voordeur - Sensor: Switch device adapter
2020-01-10 16:53:00.581 Status: dzVents: Debug: Deurbel: Processing device-adapter for Voordeur - Bel: Switch device adapter
2020-01-10 16:53:00.583 Status: dzVents: Info: Deurbel: ------ Finished Deurbel
2020-01-10 16:53:00.582 Error: dzVents: Error: (2.5.3) Deurbel: An error occurred when calling event handler Deurbel
2020-01-10 16:53:00.582 Error: dzVents: Error: (2.5.3) Deurbel: ...i/domoticz/scripts/dzVents/generated_scripts/Deurbel.lua:42: attempt to concatenate a nil value (field 'name')

Re: Doorbell routine

Posted: Saturday 11 January 2020 8:51
by waaren
fvdp80 wrote: Friday 10 January 2020 19:13 I now see that with sunset I get an error on the script. Do you know what causes this?

Code: Select all

1-10 16:53:00.582 Error: dzVents: Error: (2.5.3) Deurbel: ...i/domoticz/scripts/dzVents/generated_scripts/Deurbel.lua:42: attempt to concatenate a nil value (field 'name')
Yes. item is not a device when the script is triggered by a time event.
Change line 42 from

Code: Select all

dz.log(item.name .. ' is ' .. ( item.active and ' active.' or ' not active.' ) ,dz.LOG_DEBUG )
to

Code: Select all

if item.isDevice then 
    dz.log(item.name .. ' is ' .. ( item.active and ' active.' or ' not active.' ) ,dz.LOG_DEBUG ) 
end