Help needed with script  [Solved]

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

Moderator: leecollings

Fredom
Posts: 160
Joined: Saturday 19 September 2020 21:02
Target OS: Raspberry Pi / ODroid
Domoticz version: 2025.1
Location: Krimpen aan den IJssel
Contact:

Help needed with script

Post by Fredom »

Dear forum members who can get this script working for me.
I have already tried everything but unfortunately I cannot find it.
Thanks for the help.

Code: Select all

--(dzVentz)--
--Door open  and 'DS' is On then no telegram
--Door open  and 'DS' is Off then send telegram with name of the door
--DS is Dummy switch that is on when smartphone is at home
--WCD On if 'DS'is Off and door state is Alarm, and switch Off when door state is Normal
--Door is an Kerui D026
--Telegram is not activated yet

local devicesToCheck = {
	 ['name'] = 'Voordeur', 
	 ['name'] = 'Garagedeur',
	 ['name'] = 'Schuifdeur',
	 ['name'] = 'test',
    }

return 
{
	active = true,
    on = 
    {
        devices = 
        
            'Voordeur','Garagedeur','Schuifdeur','test'
    },
 
        logging = {
        level = domoticz.LOG_DEBUG,
        marker = "deur"
    },    

	execute = function(domoticz)
		    --for i, deviceToCheck in pairs(devicesToCheck) do
			local name = deviceToCheck['name']
			local state = domoticz.devices(name).state
            local light = dz.devices('DS')
            local lamp = dz.devices('WCD')
            local door = 'Voordeur','Garagedeur','Schuifdeur','test'
            
    execute = function(dz, door)

			if state == 'Alarm' and light =='active' then
			dz.devices ('WCD').switch.Off()    
            return
        elseif not light.active then
               dz.devices ('WCD').switchOn()
    		   domoticz.telegram('Device ' .. name .. ' staat open ', domoticz.PRIORITY_HIGH)
    		end
        end
    end
}
And this is the log file from domoticz

Code: Select all

2020-11-15 13:25:20.544 Error: EventSystem: in /home/pi/domoticz/dzVents/runtime/dzVents.lua: /home/pi/domoticz/dzVents/runtime/EventHelpers.lua:661: bad argument #1 to 'pairs' (table expected, got string)
2020-11-15 13:25:21.609 Error: EventSystem: in /home/pi/domoticz/dzVents/runtime/dzVents.lua: /home/pi/domoticz/dzVents/runtime/EventHelpers.lua:661: bad argument #1 to 'pairs' (table expected, got string)
2020-11-15 13:25:22.566 Error: EventSystem: in /home/pi/domoticz/dzVents/runtime/dzVents.lua: /home/pi/domoticz/dzVents/runtime/EventHelpers.lua:661: bad argument #1 to 'pairs' (table expected, got string)
2020-11-15 13:25:23.600 Error: EventSystem: in /home/pi/domoticz/dzVents/runtime/dzVents.lua: /home/pi/domoticz/dzVents/runtime/EventHelpers.lua:661: bad argument #1 to 'pairs' (table expected, got string)
2020-11-15 13:25:24.579 Error: EventSystem: in /home/pi/domoticz/dzVents/runtime/dzVents.lua: /home/pi/domoticz/dzVents/runtime/EventHelpers.lua:661: bad argument #1 to 'pairs' (table expected, got string)
2020-11-15 13:25:25.545 Error: EventSystem: in /home/pi/domoticz/dzVents/runtime/dzVents.lua: /home/pi/domoticz/dzVents/runtime/EventHelpers.lua:661: bad argument #1 to 'pairs' (table expected, got string)
2020-11-15 13:25:26.546 Error: EventSystem: in /home/pi/domoticz/dzVents/runtime/dzVents.lua: /home/pi/domoticz/dzVents/runtime/EventHelpers.lua:661: bad argument #1 to 'pairs' (table expected, got string)
Yours sincerely,
Fred

Rasberry Pi 3B+ - Debian Trixie - Domoticz 2025.2
RFLink - RFXCom - Zigbee (CC2531)
P1 Smart Meter - KaKu
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Help needed with script

Post by waaren »

Fredom wrote: Sunday 15 November 2020 13:45 Dear forum members who can get this script working for me.
Your dzVents script does contain some syntax and logic errors.
I corrected some but not sure if I catched all. If it not working as expected then please try to add more log statements and start with smaller steps.

Code: Select all

--(dzVentz)--
--Door open  and 'DS' is On then no telegram
--Door open  and 'DS' is Off then send telegram with name of the door
--DS is Dummy switch that is on when smartphone is at home
--WCD On if 'DS'is Off and door state is Alarm, and switch Off when door state is Normal
--Door is an Kerui D026
--Telegram is not activated yet

local devicesToCheck =
{
    'Voordeur',
    'Garagedeur',
    'Schuifdeur',
    'test',
}

return
{
    on =
    {
        devices = devicesToCheck,
    },

    logging =
    {
        level = domoticz.LOG_DEBUG,
        marker = "deur",
    },

    execute = function(dz)
        local name, state

        local light = dz.devices('DS')
        local lamp = dz.devices('WCD')

        for _, deviceToCheck in ipairs(devicesToCheck) do
            state = dz.devices(deviceToCheck).state
            dz.log('state of ' .. deviceToCheck .. ' is ' .. state , dz.LOG_DEBUG)

            if state == 'Alarm' and light.active then
                lamp.switchOff()
                return
            elseif not light.active then
               lamp.switchOn()
               dz.notify('Devices', 'Device ' .. deviceToCheck .. ' staat open ')
            end
        end
    end
}
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
Fredom
Posts: 160
Joined: Saturday 19 September 2020 21:02
Target OS: Raspberry Pi / ODroid
Domoticz version: 2025.1
Location: Krimpen aan den IJssel
Contact:

Re: Help needed with script

Post by Fredom »

Hi Waaren the script works almost fine.
the WCD turns on nicely when the 'DS' is not at home, but stays on.
Is it possible to switch off this WCD with a timer and or when the 'DS' comes home.
So whichever comes first.
There are no errors now in the log file

I am already very happy with this (fast) result

Gr
Fred
Yours sincerely,
Fred

Rasberry Pi 3B+ - Debian Trixie - Domoticz 2025.2
RFLink - RFXCom - Zigbee (CC2531)
P1 Smart Meter - KaKu
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Help needed with script

Post by waaren »

Fredom wrote: Sunday 15 November 2020 20:32 Is it possible to switch off this WCD with a timer and or when the 'DS' comes home.
There are no errors now in the log file
It is not clear from your description what you mean with DS comes home.
Good that the script is now without errors in the log but it would also help if you would show the other messages in the log produced by this script.
It would help to understand what kind of devices are involved and what states the can have.
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
Fredom
Posts: 160
Joined: Saturday 19 September 2020 21:02
Target OS: Raspberry Pi / ODroid
Domoticz version: 2025.1
Location: Krimpen aan den IJssel
Contact:

Re: Help needed with script

Post by Fredom »

waaren wrote: Sunday 15 November 2020 22:17
Fredom wrote: Sunday 15 November 2020 20:32 Is it possible to switch off this WCD with a timer and or when the 'DS' comes home.
There are no errors now in the log file
It is not clear from your description what you mean with DS comes home.
Good that the script is now without errors in the log but it would also help if you would show the other messages in the log produced by this script.
It would help to understand what kind of devices are involved and what states the can have.
Okay I don't know yet where to find the other log files.
Or that must be the log files of the switches.
But I'll explain.

The 'DS' is a virtual on / off switch, which switches on as soon as a telephone comes into the house (via a ping script).
The 'WCD' is an on / off switch to switch lights (or something else).
The 'WCD' should then turn off with a timer or when 'DS' comes on.
I hope this is not too much to ask.
For me this is all (still) too difficult but i try to learn it.


Thank you in advance for your commitment
Yours sincerely,
Fred

Rasberry Pi 3B+ - Debian Trixie - Domoticz 2025.2
RFLink - RFXCom - Zigbee (CC2531)
P1 Smart Meter - KaKu
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Help needed with script

Post by waaren »

Fredom wrote: Sunday 15 November 2020 22:52 Okay I don't know yet where to find the other log files.
I do not refer to other log files. You should see the lines from the debug logging in the same log where you first saw the errors.
I hope this is not too much to ask.
What about the state "Alarm" ? Which device can report that state ?

if I understand it correctly below script should do what you describe.

Code: Select all

--(dzVents)--
--Door open  and 'DS' is On then no telegram
--Door open  and 'DS' is Off then send telegram with name of the door
--DS is Dummy switch that is on when smartphone is at home
--WCD On if 'DS'is Off and door state is Alarm, and switch Off when door state is Normal
--Door is an Kerui D026
--Telegram is not activated yet

local devicesToCheck =
{
    'Voordeur',
    'Garagedeur',
    'Schuifdeur',
    'test',
}

return
{
    on =
    {
        devices = devicesToCheck,
    },

    logging =
    {
        level = domoticz.LOG_DEBUG,
        marker = "deur",
    },

    execute = function(dz)
        local name, state
        local delay = 300 -- set to amount of seconds the light should stay on

        local atHome = dz.devices('DS').active
        local wcd = dz.devices('WCD')

        if atHome then
            wcd.switchOff().checkFirst() -- phone is atHome so we can switchOff the wcd if it's on.
        else
            for _, doorName in ipairs(devicesToCheck) do
                local door = dz.devices(doorName)
                dz.log('state of ' .. door .. ' is ' .. door.state , dz.LOG_DEBUG)

                if door.active or door.state == 'Open' then -- a door is open
                    wcd.cancelQueuedCommands()
                    wcd.switchOn()
                    wcd.switchOff().afterSec(delay)
                    dz.log('wcd activated for ' .. delay .. ' seconds.' , dz.LOG_DEBUG)
                    dz.notify('Devices', 'Device ' .. door .. ' staat open ')
                    break -- no need to look at the other doors
                end
            end
        end
    end
}
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
Fredom
Posts: 160
Joined: Saturday 19 September 2020 21:02
Target OS: Raspberry Pi / ODroid
Domoticz version: 2025.1
Location: Krimpen aan den IJssel
Contact:

Re: Help needed with script

Post by Fredom »

Hi Waaren,

The door switch is in an Kerui D026 it has an Alarm, a Normal, and a tamper alert.

Nothing happens when I open a door.
Not when phone is at home.
Not when the phone is out of the house.
No message on the screen (previous script is) and the WCD does not turn on.
There are now also errrors.

Domoticz log file:

2020-11-16 11:11:08.031 Error: dzVents: Error: (3.0.2) deur: An error occurred when calling event handler Deur open dan melding
2020-11-16 11:11:08.032 Error: dzVents: Error: (3.0.2) deur: ...ipts/dzVents/generated_scripts/Deur open dan melding.lua:42: attempt to concatenate a table value (local 'door')
2020-11-16 11:11:27.506 Error: dzVents: Error: (3.0.2) deur: An error occurred when calling event handler Deur open dan melding
2020-11-16 11:11:27.506 Error: dzVents: Error: (3.0.2) deur: ...ipts/dzVents/generated_scripts/Deur open dan melding.lua:42: attempt to concatenate a table value (local 'door')
2020-11-16 11:11:38.528 Error: dzVents: Error: (3.0.2) deur: An error occurred when calling event handler Deur open dan melding
2020-11-16 11:11:38.528 Error: dzVents: Error: (3.0.2) deur: ...ipts/dzVents/generated_scripts/Deur open dan melding.lua:42: attempt to concatenate a table value (local 'door')
2020-11-16 11:11:55.461 Error: dzVents: Error: (3.0.2) deur: An error occurred when calling event handler Deur open dan melding
2020-11-16 11:11:55.461 Error: dzVents: Error: (3.0.2) deur: ...ipts/dzVents/generated_scripts/Deur open dan melding.lua:42: attempt to concatenate a table value (local 'door')

Domoticz status file:

2020-11-16 11:18:43.765 Status: dzVents: Info: Handling events for: "test", value: "Alarm"
2020-11-16 11:18:43.765 Status: dzVents: Info: deur: ------ Start internal script: Deur open dan melding: Device: "test (RFX)", Index: 223
2020-11-16 11:18:43.767 Status: dzVents: Debug: deur: Processing device-adapter for DS: Switch device adapter
2020-11-16 11:18:43.768 Status: dzVents: Debug: deur: Processing device-adapter for WCD: Switch device adapter
2020-11-16 11:18:43.771 Status: dzVents: Info: deur: ------ Finished Deur open dan melding
2020-11-16 11:18:46.058 Status: dzVents: Info: Handling events for: "test", value: "Normal"
2020-11-16 11:18:46.058 Status: dzVents: Info: deur: ------ Start internal script: Deur open dan melding: Device: "test (RFX)", Index: 223
2020-11-16 11:18:46.059 Status: dzVents: Debug: deur: Processing device-adapter for DS: Switch device adapter
2020-11-16 11:18:46.060 Status: dzVents: Debug: deur: Processing device-adapter for WCD: Switch device adapter
2020-11-16 11:18:46.062 Status: dzVents: Info: deur: ------ Finished Deur open dan melding

I hope this help you
Yours sincerely,
Fred

Rasberry Pi 3B+ - Debian Trixie - Domoticz 2025.2
RFLink - RFXCom - Zigbee (CC2531)
P1 Smart Meter - KaKu
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Help needed with script

Post by waaren »

Can you try again after changing line 42 from

Code: Select all

dz.log('state of ' .. door .. ' is ' .. door.state , dz.LOG_DEBUG)
to

Code: Select all

dz.log('state of ' .. doorName .. ' is ' .. door.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
Fredom
Posts: 160
Joined: Saturday 19 September 2020 21:02
Target OS: Raspberry Pi / ODroid
Domoticz version: 2025.1
Location: Krimpen aan den IJssel
Contact:

Re: Help needed with script

Post by Fredom »

Hi Waaren,

The lamp now turns on nicely when opening the door.
But works only on the timer and not when closing the door before the timer.
I also changed line 44 from 'Open' to 'Alarm'
Also no notification on my screen.
Door open is 'Alarm'
Door closed is 'Normal'

There are also errors

Domoticz log file:

2020-11-16 14:11:24.144 Error: dzVents: Error: (3.0.2) deur: An error occurred when calling event handler Deur open dan melding
2020-11-16 14:11:24.144 Error: dzVents: Error: (3.0.2) deur: ...ipts/dzVents/generated_scripts/Deur open dan melding.lua:49: attempt to concatenate a table value (local 'door')
2020-11-16 14:11:35.730 Error: dzVents: Error: (3.0.2) deur: An error occurred when calling event handler Deur open dan melding
2020-11-16 14:11:35.730 Error: dzVents: Error: (3.0.2) deur: ...ipts/dzVents/generated_scripts/Deur open dan melding.lua:49: attempt to concatenate a table value (local 'door')


Domoticz status file:

2020-11-16 14:12:09.942 Status: dzVents: Info: Handling events for: "test", value: "Alarm"
2020-11-16 14:12:09.942 Status: dzVents: Info: deur: ------ Start internal script: Deur open dan melding: Device: "test (RFX)", Index: 223
2020-11-16 14:12:09.944 Status: dzVents: Debug: deur: Processing device-adapter for DS: Switch device adapter
2020-11-16 14:12:09.946 Status: dzVents: Debug: deur: Processing device-adapter for WCD: Switch device adapter
2020-11-16 14:12:09.946 Status: dzVents: Debug: deur: Constructed timed-command: Off
2020-11-16 14:12:09.947 Status: dzVents: Info: deur: ------ Finished Deur open dan melding
2020-11-16 14:12:20.763 Status: dzVents: Info: Handling events for: "test", value: "Normal"
2020-11-16 14:12:20.763 Status: dzVents: Info: deur: ------ Start internal script: Deur open dan melding: Device: "test (RFX)", Index: 223
2020-11-16 14:12:20.764 Status: dzVents: Debug: deur: Processing device-adapter for DS: Switch device adapter
2020-11-16 14:12:20.766 Status: dzVents: Debug: deur: Processing device-adapter for WCD: Switch device adapter
2020-11-16 14:12:20.766 Status: dzVents: Debug: deur: Constructed timed-command: Off
2020-11-16 14:12:20.766 Status: dzVents: Info: deur: ------ Finished Deur open dan melding
Yours sincerely,
Fred

Rasberry Pi 3B+ - Debian Trixie - Domoticz 2025.2
RFLink - RFXCom - Zigbee (CC2531)
P1 Smart Meter - KaKu
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Help needed with script

Post by waaren »

Fredom wrote: Monday 16 November 2020 14:38 The lamp now turns on nicely when opening the door. But works only on the timer and not when closing the door before the timer.
I was not aware of this requirement.
Also no notification on my screen.
The dz.notify does not send anything to the screen it sends something to all configured notification subsystems.

Can you try this one?

Code: Select all

--(dzVents)--
--Door open  and 'DS' is On then no telegram
--Door open  and 'DS' is Off then send telegram with name of the door
--DS is Dummy switch that is on when smartphone is at home
--WCD On if 'DS'is Off and door state is Alarm, and switch Off when door state is Normal
--Door is an Kerui D026
--Telegram is not activated yet

local devicesToCheck =
{
    'Voordeur',
    'Garagedeur',
    'Schuifdeur',
    'test',
}

return
{
    on =
    {
        devices = devicesToCheck,
    },

    logging =
    {
        level = domoticz.LOG_DEBUG,
        marker = "deur",
    },

    execute = function(dz)
        local name, state
        local delay = 300 -- set to amount of seconds the light should stay on
        local allDoorsClosed = true

        local atHome = dz.devices('DS').active
        local wcd = dz.devices('WCD')

        if atHome then
            wcd.switchOff().checkFirst() -- phone is atHome so we can switchOff the wcd if it's on.
        else
            for _, doorName in ipairs(devicesToCheck) do

                local door = dz.devices(doorName)
                dz.log('state of ' .. doorName .. ' is ' .. door.state , dz.LOG_DEBUG)

                if door.active or door.state == 'Open' or door.state == 'Alarm' -- a door is open
                    allDoorsClosed = false
                    wcd.cancelQueuedCommands()
                    wcd.switchOn()
                    wcd.switchOff().afterSec(delay)
                    dz.log('wcd activated for ' .. delay .. ' seconds.' , dz.LOG_DEBUG)
                    dz.notify('Devices', 'Device ' .. doorName .. ' staat open ')
                    break -- no need to look at the other doors
                end
            end
            if allDoorsClosed then
                wcd.switchOff().checkFirst() -- All doors are closed so we can switchOff the wcd when it is on
            end
        end
    end
}
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
Fredom
Posts: 160
Joined: Saturday 19 September 2020 21:02
Target OS: Raspberry Pi / ODroid
Domoticz version: 2025.1
Location: Krimpen aan den IJssel
Contact:

Re: Help needed with script

Post by Fredom »

Hi Waaren,

There is now an error for line 47
Also errors in the log files


This morning there was an outage with this forum.
Now I can no longer respond to your message and I can no longer use the select code, which is no longer there.
I also no longer get notifications of new messages.
Have restarted everything a few times but nothing helps.
Therefor, I send it this way.

Status

2020-11-16 15:28:04.829 Status: EventSystem: reset all events...
2020-11-16 15:28:04.831 Status: dzVents: Write file: /home/pi/domoticz/scripts/dzVents/generated_scripts/Energie.lua
2020-11-16 15:28:04.832 Status: dzVents: Write file: /home/pi/domoticz/scripts/dzVents/generated_scripts/Battery level.lua
2020-11-16 15:28:04.832 Status: dzVents: Write file: /home/pi/domoticz/scripts/dzVents/generated_scripts/Screen Slaapkamer.lua
2020-11-16 15:28:04.832 Status: dzVents: Write file: /home/pi/domoticz/scripts/dzVents/generated_scripts/Voordeur open licht aan.lua
2020-11-16 15:28:04.832 Status: dzVents: Write file: /home/pi/domoticz/scripts/dzVents/generated_scripts/Screen logeerkamer.lua

Log

2020-11-16 15:27:57.615 ...ipts/dzVents/generated_scripts/Deur open dan melding.lua:47: 'then' expected near 'allDoorsClosed'
2020-11-16 15:27:58.605 Error: dzVents: Error: (3.0.2) error loading module 'Deur open dan melding' from file '/home/pi/domoticz/scripts/dzVents/generated_scripts/Deur open dan melding.lua':
2020-11-16 15:27:58.605 ...ipts/dzVents/generated_scripts/Deur open dan melding.lua:47: 'then' expected near 'allDoorsClosed'
2020-11-16 15:27:59.608 Error: dzVents: Error: (3.0.2) error loading module 'Deur open dan melding' from file '/home/pi/domoticz/scripts/dzVents/generated_scripts/Deur open dan melding.lua':
2020-11-16 15:27:59.608 ...ipts/dzVents/generated_scripts/Deur open dan melding.lua:47: 'then' expected near 'allDoorsClosed'
2020-11-16 15:28:00.501 Error: dzVents: Error: (3.0.2) error loading module 'Deur open dan melding' from file '/home/pi/domoticz/scripts/dzVents/generated_scripts/Deur open dan melding.lua':
2020-11-16 15:28:00.501 ...ipts/dzVents/generated_scripts/Deur open dan melding.lua:47: 'then' expected near 'allDoorsClosed'
Yours sincerely,
Fred

Rasberry Pi 3B+ - Debian Trixie - Domoticz 2025.2
RFLink - RFXCom - Zigbee (CC2531)
P1 Smart Meter - KaKu
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Help needed with script

Post by waaren »

Fredom wrote: Monday 16 November 2020 15:47There is now an error for line 47
Seems Murphy is visiting the forum... ;-)

corrected the error and added a log statement

Code: Select all

--(dzVents)--
--Door open  and 'DS' is On then no telegram
--Door open  and 'DS' is Off then send telegram with name of the door
--DS is Dummy switch that is on when smartphone is at home
--WCD On if 'DS'is Off and door state is Alarm, and switch Off when door state is Normal
--Door is an Kerui D026
--Telegram is not activated yet

local devicesToCheck =
{
    'Voordeur',
    'Garagedeur',
    'Schuifdeur',
    'test',
}

return
{
    on =
    {
        devices = devicesToCheck,
    },

    logging =
    {
        level = domoticz.LOG_DEBUG,
        marker = 'deur',
    },

    execute = function(dz)
        local name, state
        local delay = 300 -- set to amount of seconds the light should stay on
        local allDoorsClosed = true

        local atHome = dz.devices('DS').active
        local wcd = dz.devices('WCD')

        dz.log('state of WCD is ' .. wcd.state , dz.LOG_DEBUG)
        dz.log('state of DS is ' .. dz.devices('DS').state .. '; atHome is ' .. tostring(atHome), dz.LOG_DEBUG)

        if atHome then
            wcd.switchOff().checkFirst() -- phone is atHome so we can switchOff the wcd if it's on.
        else
            for _, doorName in ipairs(devicesToCheck) do

                local door = dz.devices(doorName)
                dz.log('state of ' .. doorName .. ' is ' .. door.state , dz.LOG_DEBUG)

                if door.active or door.state == 'Open' or door.state == 'Alarm' then -- a door is open
                    allDoorsClosed = false
                    wcd.cancelQueuedCommands()
                    wcd.switchOn()
                    wcd.switchOff().afterSec(delay)
                    dz.log('wcd activated for ' .. delay .. ' seconds.' , dz.LOG_DEBUG)
                    dz.notify('Devices', 'Device ' .. doorName .. ' staat open ')
                    break -- no need to look at the other doors
                end
            end
            if allDoorsClosed then
                wcd.switchOff().checkFirst() -- All doors are closed so we can switchOff the wcd when it is on
            end
        end
    end
}
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
Fredom
Posts: 160
Joined: Saturday 19 September 2020 21:02
Target OS: Raspberry Pi / ODroid
Domoticz version: 2025.1
Location: Krimpen aan den IJssel
Contact:

Re: Help needed with script

Post by Fredom »

Hi Waaren,

There are no more errors now.
But the lamp doesn't turn off on closing the door.
Lamp turns off only on the timer.
All other functions in order


I don't like Murphy
Yours sincerely,
Fred

Rasberry Pi 3B+ - Debian Trixie - Domoticz 2025.2
RFLink - RFXCom - Zigbee (CC2531)
P1 Smart Meter - KaKu
Fredom
Posts: 160
Joined: Saturday 19 September 2020 21:02
Target OS: Raspberry Pi / ODroid
Domoticz version: 2025.1
Location: Krimpen aan den IJssel
Contact:

Re: Help needed with script

Post by Fredom »

Hi Waaren,

This is the Status file:

2020-11-16 16:43:15.323 Status: dzVents: Info: Handling events for: "Voordeur", value: "Normal"
2020-11-16 16:43:15.323 Status: dzVents: Info: deur: ------ Start internal script: Deur open dan melding: Device: "Voordeur (RFX)", Index: 103
2020-11-16 16:43:15.325 Status: dzVents: Debug: deur: Processing device-adapter for DS: Switch device adapter
2020-11-16 16:43:15.326 Status: dzVents: Debug: deur: Processing device-adapter for WCD: Switch device adapter
2020-11-16 16:43:15.326 Status: dzVents: Debug: deur: state of WCD is On
2020-11-16 16:43:15.326 Status: dzVents: Debug: deur: state of DS is Off; atHome is false
2020-11-16 16:43:15.326 Status: dzVents: Debug: deur: state of Voordeur is Normal
2020-11-16 16:43:15.326 Status: dzVents: Debug: deur: Constructed timed-command: On
2020-11-16 16:43:15.326 Status: dzVents: Debug: deur: Constructed timed-command: Off
2020-11-16 16:43:15.326 Status: dzVents: Debug: deur: Constructed timed-command: Off AFTER 30 SECONDS
2020-11-16 16:43:15.326 Status: dzVents: Debug: deur: wcd activated for 30 seconds.
2020-11-16 16:43:15.326 Status: dzVents: Info: deur: ------ Finished Deur open dan melding
Yours sincerely,
Fred

Rasberry Pi 3B+ - Debian Trixie - Domoticz 2025.2
RFLink - RFXCom - Zigbee (CC2531)
P1 Smart Meter - KaKu
Fredom
Posts: 160
Joined: Saturday 19 September 2020 21:02
Target OS: Raspberry Pi / ODroid
Domoticz version: 2025.1
Location: Krimpen aan den IJssel
Contact:

Re: Help needed with script

Post by Fredom »

Hi Waaren,

After more testing.

If the telephone comes in and then closes the door, the lamp will go out immediately.
Otherwise, the lamp will only go out via the timer

I hope this helps you
Yours sincerely,
Fred

Rasberry Pi 3B+ - Debian Trixie - Domoticz 2025.2
RFLink - RFXCom - Zigbee (CC2531)
P1 Smart Meter - KaKu
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Help needed with script

Post by waaren »

Fredom wrote: Monday 16 November 2020 17:02If the telephone comes in and then closes the door, the lamp will go out immediately. Otherwise, the lamp will only go out via the timer
Seems like the 'normal' state is also interpreted as being active.

Code: Select all

--(dzVents)--
--Door open  and 'DS' is On then no telegram
--Door open  and 'DS' is Off then send telegram with name of the door
--DS is Dummy switch that is on when smartphone is at home
--WCD On if 'DS'is Off and door state is Alarm, and switch Off when door state is Normal
--Door is an Kerui D026
--Telegram is not activated yet

local devicesToCheck =
{
    'Voordeur',
    'Garagedeur',
    'Schuifdeur',
    'test',
}

return
{
    on =
    {
        devices = devicesToCheck,
    },

    logging =
    {
        level = domoticz.LOG_DEBUG,
        marker = 'deur',
    },

    execute = function(dz)
        local name, state
        local delay = 300 -- set to amount of seconds the light should stay on
        local allDoorsClosed = true

        local atHome = dz.devices('DS').active
        local wcd = dz.devices('WCD')

        dz.log('state of WCD is ' .. wcd.state , dz.LOG_DEBUG)
        dz.log('state of DS is ' .. dz.devices('DS').state .. '; atHome is ' .. tostring(atHome), dz.LOG_DEBUG)

        if atHome then
            wcd.switchOff().checkFirst() -- phone is atHome so we can switchOff the wcd if it's on.
        else
            for _, doorName in ipairs(devicesToCheck) do

                local door = dz.devices(doorName)
                dz.log('state of ' .. doorName .. ' is ' .. door.state .. ' (' .. ( ( door.active and 'active' ) or 'not active' ) .. ')', dz.LOG_DEBUG)

                if door.state == 'Open' or door.state == 'Alarm' then -- a door is open
                    allDoorsClosed = false
                    wcd.cancelQueuedCommands()
                    wcd.switchOn()
                    wcd.switchOff().afterSec(delay)
                    dz.log('wcd activated for ' .. delay .. ' seconds.' , dz.LOG_DEBUG)
                    dz.notify('Devices', 'Device ' .. doorName .. ' staat open ')
                    break -- no need to look at the other doors
                end
            end
            if allDoorsClosed then
                wcd.switchOff().checkFirst() -- All doors are closed so we can switchOff the wcd when it is on
            end
        end
    end
}

Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
Fredom
Posts: 160
Joined: Saturday 19 September 2020 21:02
Target OS: Raspberry Pi / ODroid
Domoticz version: 2025.1
Location: Krimpen aan den IJssel
Contact:

Re: Help needed with script

Post by Fredom »

Hi Waaren,

There are no more errors in the domoticz log file

The working is now

Phone off and door open then light on then door close light off (oke)
Phone off and door open then light on then light off on timer (oke)
Phone off and door open then light on then phone at home light off on timer (prefer light direct off)
Phone off and door open light on then phone at home and then door closed (befor timer) then light off (oke if the light goes off by phone)
Prefer if posible light goes always off by phone at home


Status

2020-11-16 20:05:03.784 Status: dzVents: Info: Handling events for: "test", value: "Normal"
2020-11-16 20:05:03.784 Status: dzVents: Info: deur: ------ Start internal script: Deur open melding: Device: "test (RFX)", Index: 223
2020-11-16 20:05:03.786 Status: dzVents: Debug: deur: Processing device-adapter for DS: Switch device adapter
2020-11-16 20:05:03.787 Status: dzVents: Debug: deur: Processing device-adapter for WCD: Switch device adapter
2020-11-16 20:05:03.787 Status: dzVents: Debug: deur: state of WCD is On
2020-11-16 20:05:03.787 Status: dzVents: Debug: deur: state of DS is On; atHome is true
2020-11-16 20:05:03.787 Status: dzVents: Debug: deur: Constructed timed-command: Off
2020-11-16 20:05:03.787 Status: dzVents: Debug: deur: Constructed timed-command: Off
2020-11-16 20:05:03.787 Status: dzVents: Info: deur: ------ Finished Deur open melding
Yours sincerely,
Fred

Rasberry Pi 3B+ - Debian Trixie - Domoticz 2025.2
RFLink - RFXCom - Zigbee (CC2531)
P1 Smart Meter - KaKu
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Help needed with script

Post by waaren »

Fredom wrote: Monday 16 November 2020 21:03 Prefer if posible light goes always off by phone at home
To do this the virtual phone switch should also trigger the script. Below modified version will do that.

Code: Select all

--(dzVents)--
--Door open  and 'DS' is On then no telegram
--Door open  and 'DS' is Off then send telegram with name of the door
--DS is Dummy switch that is on when smartphone is at home
--WCD On if 'DS'is Off and door state is Alarm, and switch Off when door state is Normal
--Door is an Kerui D026
--Telegram is not activated yet

local doors =
{
    'Voordeur',
    'Garagedeur',
    'Schuifdeur',
    'test',
}

local allDevices = doors -- copy the doors table to table allDevices
table.insert(allDevices, 'DS') -- this will add this deviceName to the table allDevices

return
{
    on =
    {
        devices = allDevices, 
    },

    logging =
    {
        level = domoticz.LOG_DEBUG,
        marker = 'deur',
    },

    execute = function(dz)
        local delay = 300 -- set to amount of seconds the light should stay on
        local allDoorsClosed = true

        local ds = dz.devices('DS')
        local wcd = dz.devices('WCD')

        local atHome = ds.active

        dz.log('state of WCD is ' .. wcd.state , dz.LOG_DEBUG)
        dz.log('state of DS is ' .. dz.devices('DS').state .. '; atHome is ' .. tostring(atHome), dz.LOG_DEBUG)

        if atHome then
            wcd.switchOff().checkFirst() -- phone is atHome so we can switchOff the wcd if it's on.
        else
            for _, doorName in ipairs(doors) do
                local door = dz.devices(doorName)
                dz.log('state of ' .. doorName .. ' is ' .. door.state .. ' (' .. ( ( door.active and 'active' ) or 'not active' ) .. ')', dz.LOG_DEBUG)

                if door.state == 'Open' or door.state == 'Alarm' then -- a door is open
                    allDoorsClosed = false
                    wcd.cancelQueuedCommands()
                    wcd.switchOn()
                    wcd.switchOff().afterSec(delay)
                    dz.log('wcd activated for ' .. delay .. ' seconds because ' .. doorName .. ' is open.' , dz.LOG_DEBUG)
                    dz.notify('Devices', 'Device ' .. doorName .. ' staat open ')
                    break -- no need to look at the other doors if one is open.
                end
            end
            if allDoorsClosed then
                wcd.switchOff().checkFirst() -- All doors are closed so we can switchOff the wcd when it is on
            end
        end
    end
}


Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
Fredom
Posts: 160
Joined: Saturday 19 September 2020 21:02
Target OS: Raspberry Pi / ODroid
Domoticz version: 2025.1
Location: Krimpen aan den IJssel
Contact:

Re: Help needed with script

Post by Fredom »

Hi Waaren,
Thank you so much for perfecting this script to work perfectly now.
I am very happy now.
I also learned something from this.

Regards

Fred
Yours sincerely,
Fred

Rasberry Pi 3B+ - Debian Trixie - Domoticz 2025.2
RFLink - RFXCom - Zigbee (CC2531)
P1 Smart Meter - KaKu
Fredom
Posts: 160
Joined: Saturday 19 September 2020 21:02
Target OS: Raspberry Pi / ODroid
Domoticz version: 2025.1
Location: Krimpen aan den IJssel
Contact:

Re: Help needed with script

Post by Fredom »

waaren wrote: Monday 16 November 2020 21:45
Fredom wrote: Monday 16 November 2020 21:03 Prefer if posible light goes always off by phone at home
To do this the virtual phone switch should also trigger the script.
Hello Waaren

The script works verry well.
The notifications always come when a door opens, even when the phone is at home.

Phone at home then no notification.
No errors in de logfile

The message text comes from the doorswitch (notifications) and not from the script.
The message text is not a problem, I get the message in telegram that (name of) the door is open.


Is it possible that the message only comes when the door is open and the phone is not at home?


This would be the cherry on the cake

Code: Select all

--(dzVents)--
--Door open  and 'Thuis' is On then no telegram
--Door open  and 'Thuis' is Off then send telegram with name of the door and door is open
--Thuis is Dummy switch that is on when smartphone is at home
--WCD On if 'Thuis'is Off and door state is Alarm, and switch Off when door state is Normal
--Door is an Kerui D026
--Telegram is activated

local doors =
{
    'Voordeur',
    'Garagedeur',
    'Schuifdeur',
    'Testdeur',  --for testing 
}

local allDevices = doors -- copy the doors table to table allDevices
table.insert(allDevices, 'Thuis') -- this will add this deviceName to the table allDevices

return
{
    on =
    {
        devices = allDevices, 
    },

    logging =
    {
        level = domoticz.LOG_DEBUG,
        marker = 'deur',
    },

    execute = function(dz)
        local delay = 300 -- set to amount of seconds the light should stay on
        local allDoorsClosed = true

        local Thuis = dz.devices('Thuis') --atHome
        local wcd = dz.devices('WCD') --Light switch

        local atHome = Thuis.active

        dz.log('state of WCD is ' .. wcd.state , dz.LOG_DEBUG)
        dz.log('state of Thuis is ' .. dz.devices('Thuis').state .. '; atHome is ' .. tostring(atHome), dz.LOG_DEBUG)

        if atHome then
            wcd.switchOff().checkFirst() -- phone is atHome so we can switchOff the wcd if it's on.
        else
            for _, doorName in ipairs(doors) do
                local door = dz.devices(doorName)
                dz.log('state of ' .. doorName .. ' is ' .. door.state .. ' (' .. ( ( door.active and 'active' ) or 'not active' ) .. ')', dz.LOG_DEBUG)

                if door.state == 'Open' or door.state == 'Alarm' then -- a door is open
                    allDoorsClosed = false
                    wcd.cancelQueuedCommands()
                    wcd.switchOn()
                    wcd.switchOff().afterSec(delay)
                    dz.log('wcd activated for ' .. delay .. ' seconds because ' .. doorName .. ' is open.' , dz.LOG_DEBUG)
                    dz.notify('Devices', 'Device ' .. doorName .. ' staat open ')
                    break -- no need to look at the other doors if one is open.
                end
            end
            if allDoorsClosed then
                wcd.switchOff().checkFirst() -- All doors are closed so we can switchOff the wcd when it is on
            end
        end
    end
}

This is what I have tried but with the same result

Code: Select all

dz.log('wcd activated for ' .. delay .. ' seconds because ' .. doorName .. ' is open.' , dz.LOG_DEBUG)
                    --dz.notify('Devices', 'Device ' .. doorName .. ' staat open ')
                    break -- no need to look at the other doors if one is open.
                elseif door.state =='Alarm' and not Thuis.active then
                    dz.notify('Devices', 'Device ' .. doorName .. ' staat open ')
And this is the Status file

Code: Select all

2020-11-18 20:28:45.951 Status: Notification: Testdeur geopend
2020-11-18 20:28:46.099 Status: dzVents: Info: Handling events for: "Testdeur", value: "Alarm"
2020-11-18 20:28:46.099 Status: dzVents: Info: deur: ------ Start internal script: Deur open melding telegram: Device: "Testdeur (RFX)", Index: 223
2020-11-18 20:28:46.100 Status: dzVents: Debug: deur: Processing device-adapter for Thuis: Switch device adapter
2020-11-18 20:28:46.102 Status: dzVents: Debug: deur: Processing device-adapter for WCD: Switch device adapter
2020-11-18 20:28:46.102 Status: dzVents: Debug: deur: state of WCD is Off
2020-11-18 20:28:46.102 Status: dzVents: Debug: deur: state of Thuis is On; atHome is true
2020-11-18 20:28:46.102 Status: dzVents: Debug: deur: Constructed timed-command: Off
2020-11-18 20:28:46.102 Status: dzVents: Info: deur: ------ Finished Deur open melding telegram
2020-11-18 20:28:55.392 Status: dzVents: Info: Handling events for: "Testdeur", value: "Normal"
2020-11-18 20:28:55.392 Status: dzVents: Info: deur: ------ Start internal script: Deur open melding telegram: Device: "Testdeur (RFX)", Index: 223
2020-11-18 20:28:55.395 Status: dzVents: Debug: deur: Processing device-adapter for Thuis: Switch device adapter
2020-11-18 20:28:55.397 Status: dzVents: Debug: deur: Processing device-adapter for WCD: Switch device adapter
2020-11-18 20:28:55.397 Status: dzVents: Debug: deur: state of WCD is Off
2020-11-18 20:28:55.397 Status: dzVents: Debug: deur: state of Thuis is On; atHome is true
2020-11-18 20:28:55.397 Status: dzVents: Debug: deur: Constructed timed-command: Off
2020-11-18 20:28:55.398 Status: dzVents: Info: deur: ------ Finished Deur open melding telegram
Hope you wil help me one more time

Thanks

Fred
Yours sincerely,
Fred

Rasberry Pi 3B+ - Debian Trixie - Domoticz 2025.2
RFLink - RFXCom - Zigbee (CC2531)
P1 Smart Meter - KaKu
Post Reply

Who is online

Users browsing this forum: Google [Bot] and 1 guest