script doesn't do what I want

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

Moderator: leecollings

Post Reply
Jan Jansen
Posts: 229
Joined: Wednesday 30 April 2014 20:27
Target OS: Raspberry Pi / ODroid
Domoticz version: Stable
Location: The Netherlands
Contact:

script doesn't do what I want

Post by Jan Jansen »

Code: Select all

return {
	on = {
		devices = 
		{
		    'Bed José (Action)_single',                                         -- zigbee push button
		    'Bed MJ (Action)_single',                                           -- zigbee push button
		    'Beweging slaapkamer (Pir)',                                        -- zigbee motion sensor
		},
	},
	
	    logging =
        {
            level = domoticz.LOG_DEBUG,                                         
            marker = 'nachtverlichting slaapkamer',
        },

	execute = function(dz, device)
	    
	    local bj = dz.devices('Bed José (Action)_single')
	    local bmj = dz.devices('Bed MJ (Action)_single')
	    local nvs = dz.devices('Nachtverlichting slaapkamer')         -- zigbee outlet
	    local pirslpkmr = dz.devices('Beweging slaapkamer (Pir)')
	    
	    dz.log('Beweging slaapkamer (Pir),                 state: ' .. pirslpkmr.state, dz.LOG_DEBUG)
	    
	    if  (bj.state == 'On' or bmj.state == 'On' or pirslpkmr.state == 'On') and nvs.state == 'Off' then
	         nvs.switchOn().forMin(10)
	    elseif (bj.state == 'On' or bmj.state == 'On') and nvs.state == 'On' then
	        nvs.switchOff()
	    end     
    end
}
When using the push buttons, the script works as intended. When the PIR is activated, the "Nachtverlichting" turns 'On' as intended.

Code: Select all

2024-04-15 13:16:39.655  Zigbee2mqtt: Light/Switch/Switch (Beweging slaapkamer (Pir))
2024-04-15 13:16:39.681  Zigbee2mqtt: General/Voltage (Beweging slaapkamer)
2024-04-15 13:16:39.821  Status: dzVents: Info: Handling events for: "Beweging slaapkamer (Pir)", value: "On"
2024-04-15 13:16:39.825  Status: dzVents: Info: nachtverlichting slaapkamer: ------ Start internal script: Script #1: Device: "Beweging slaapkamer (Pir) (Zigbee2mqtt)", Index: 251
2024-04-15 13:16:39.825  Status: dzVents: Debug: nachtverlichting slaapkamer: Beweging slaapkamer (Pir),                 state: On
2024-04-15 13:16:39.825  Status: dzVents: Debug: nachtverlichting slaapkamer: Constructed timed-command: On
2024-04-15 13:16:39.825  Status: dzVents: Debug: nachtverlichting slaapkamer: Constructed timed-command: On FOR 600 SECONDS
2024-04-15 13:16:39.825  Status: dzVents: Info: nachtverlichting slaapkamer: ------ Finished Script #1
2024-04-15 13:16:39.828  Status: EventSystem: Script event triggered: /home/mj/domoticz/dzVents/runtime/dzVents.lua
2024
After PIR activation, the Domoticz motion sensor (switch) remains 'On' for 1 minute. It then switches 'Off' and the "Nachtverlichting" also switches 'Off' (the 'Nachtverlichting' was intended to only switch off after 10 minutes). The log then shows

Code: Select all

2024-04-15 13:17:47.945  Zigbee2mqtt: Light/Switch/Switch (Beweging slaapkamer (Pir))
2024-04-15 13:17:47.959  Zigbee2mqtt: General/Voltage (Beweging slaapkamer)
2024-04-15 13:17:48.089  Status: dzVents: Info: Handling events for: "Beweging slaapkamer (Pir)", value: "Off"
2024-04-15 13:17:48.094  Status: dzVents: Info: nachtverlichting slaapkamer: ------ Start internal script: Script #1: Device: "Beweging slaapkamer (Pir) (Zigbee2mqtt)", Index: 251
2024-04-15 13:17:48.094  Status: dzVents: Debug: nachtverlichting slaapkamer: Beweging slaapkamer (Pir),                 state: Off
2024-04-15 13:17:48.094  Status: dzVents: Debug: nachtverlichting slaapkamer: Constructed timed-command: Off
2024-04-15 13:17:48.094  Status: dzVents: Info: nachtverlichting slaapkamer: ------ Finished Script #1
2024-04-15 13:17:48.097  Status: EventSystem: Script event triggered: /home/mj/domoticz/dzVents/runtime/dzVents.lua
2024
I don't understand why the "Nachtverlichting" turns 'Off' when the Domoticz motion sensor (switch) turns 'Off'. Nowhere in the script is this requested. What's going wrong?
Thanks in advance!
User avatar
jvdz
Posts: 2189
Joined: Tuesday 30 December 2014 19:25
Target OS: Raspberry Pi / ODroid
Domoticz version: 4.107
Location: Netherlands
Contact:

Re: script doesn't do what I want

Post by jvdz »

Jan Jansen wrote: Monday 15 April 2024 14:30 I don't understand why the "Nachtverlichting" turns 'Off' when the Domoticz motion sensor (switch) turns 'Off'. Nowhere in the script is this requested. What's going wrong?
Thanks in advance!
Well, the PIR Off does trigger an event, so the second if is somehow true at that time the PIR goes off:

Code: Select all

elseif (bj.state == 'On' or bmj.state == 'On') and nvs.state == 'On' then
   nvs.switchOff()
end
So could it be that either bj.state or bmj.state is still "On" at that time?
Maybe add a debug log message to the code to check that?
New Garbage collection scripts: https://github.com/jvanderzande/GarbageCalendar
Jan Jansen
Posts: 229
Joined: Wednesday 30 April 2014 20:27
Target OS: Raspberry Pi / ODroid
Domoticz version: Stable
Location: The Netherlands
Contact:

Re: script doesn't do what I want

Post by Jan Jansen »

@jvdz,

You're right, that's the problem.

I tried to solve it with:

Code: Select all

elseif (bj.state == 'On' or bmj.state == 'On') and pirslpkmr.lastUpdate.secondsAgo > 70 and nvs.state == 'On' then
	        nvs.switchOff()
But that change does not solve the problem
User avatar
jvdz
Posts: 2189
Joined: Tuesday 30 December 2014 19:25
Target OS: Raspberry Pi / ODroid
Domoticz version: 4.107
Location: Netherlands
Contact:

Re: script doesn't do what I want

Post by jvdz »

Just so I understand what you like to accomplish: This second test is to be able to interrupt the 10 minutes on by pressing one of the 2 buttons so it should only do something when the event is originating from either of those buttons?
New Garbage collection scripts: https://github.com/jvanderzande/GarbageCalendar
User avatar
habahabahaba
Posts: 192
Joined: Saturday 18 March 2023 14:44
Target OS: Windows
Domoticz version: 2024.4
Contact:

Re: script doesn't do what I want

Post by habahabahaba »

May be

Code: Select all

elseif (bj.state == 'On' or bmj.state == 'On') and nvs.state == 'On' then

            if pirslpkmr.state == 'Off' then
                nvs.cancelQueuedCommands()
	        nvs.switchOff().afterSec(600)
            else
                nvs.switchOff()
            end
end
?
Jan Jansen
Posts: 229
Joined: Wednesday 30 April 2014 20:27
Target OS: Raspberry Pi / ODroid
Domoticz version: Stable
Location: The Netherlands
Contact:

Re: script doesn't do what I want

Post by Jan Jansen »

First of all, thank you both for your time and attention.
What do I want to achieve:
1. Lighting on for a maximum of 10 minutes after PIR activation or after activation of 1 of the 2 push buttons.
2. Lighting off after activation of 1 of the 2 push buttons.

The adjustment suggested by @habahabahaba does not work as intended.

Code: Select all

2024-04-15 19:35:15.956  Zigbee2mqtt: Light/Switch/Switch (Beweging slaapkamer (Pir))
2024-04-15 19:35:15.988  Zigbee2mqtt: General/Voltage (Beweging slaapkamer)
2024-04-15 19:35:16.133  Status: dzVents: Info: Handling events for: "Beweging slaapkamer (Pir)", value: "On"
2024-04-15 19:35:16.138  Status: dzVents: Info: nachtverlichting slaapkamer: ------ Start internal script: Script #1: Device: "Beweging slaapkamer (Pir) (Zigbee2mqtt)", Index: 251
2024-04-15 19:35:16.138  Status: dzVents: Debug: nachtverlichting slaapkamer: Beweging slaapkamer (Pir),                 state: On
2024-04-15 19:35:16.138  Status: dzVents: Debug: nachtverlichting slaapkamer: Bed José (Action)_single,                  state: On
2024-04-15 19:35:16.138  Status: dzVents: Debug: nachtverlichting slaapkamer: Bed MJ (Action)_single,                    state: On
2024-04-15 19:35:16.138  Status: dzVents: Debug: nachtverlichting slaapkamer: Constructed timed-command: Off
2024-04-15 19:35:16.138  Status: dzVents: Info: nachtverlichting slaapkamer: ------ Finished Script #1
2024
User avatar
jvdz
Posts: 2189
Joined: Tuesday 30 December 2014 19:25
Target OS: Raspberry Pi / ODroid
Domoticz version: 4.107
Location: Netherlands
Contact:

Re: script doesn't do what I want

Post by jvdz »

Jan Jansen wrote: Monday 15 April 2024 19:53 2. Lighting off after activation of 1 of the 2 push buttons.
In that case I think you should test for the device that triggered the event by reading the event data.
Something like this maybe ( I am not able to test this easily)

Code: Select all

elseif (device.name == 'Bed José (Action)_single' or device.name == 'Bed MJ (Action)_single') and nvs.state == 'On' Then
	nvs.switchOff()
end
Maybe this is a nicer/shorter version of your logic using the Event device data:

Code: Select all

        if nvs.state == 'Off' and device.state == 'On' then
             nvs.switchOn().forMin(10)
        elseif nvs.state == 'On' and device.state == 'On' and device.name ~= 'Beweging slaapkamer (Pir)' Then
            nvs.switchOff()
        end
New Garbage collection scripts: https://github.com/jvanderzande/GarbageCalendar
User avatar
habahabahaba
Posts: 192
Joined: Saturday 18 March 2023 14:44
Target OS: Windows
Domoticz version: 2024.4
Contact:

Re: script doesn't do what I want

Post by habahabahaba »

Code: Select all

return {
	on = {
		devices = {58, 59, 68}, -- Idx of 2 buttons and Motion sensor, change to yours

	},
	
	logging = {
		level = domoticz.LOG_DEBUG,
		marker = 'nachtverlichting slaapkamer',
	},
	
	execute = function(domoticz, device)
            
        --local nvs = domoticz.devices(25) -- idx of your zigbee outlet
        local nvs = dz.devices('Nachtverlichting slaapkamer')
        
        if (device.name =='Bed José (Action)_single' or device.name =='Bed MJ (Action)_single'  and device.state == 'On' ) then 
            
            if nvs.state == 'On' then
                nvs.switchOff()
            else
                nvs.switchOn().forMin(10).checkFirst() -- CheckFirst is needed cause some time forMin() doesn't work without it
            end
            
        elseif (device.name =='Beweging slaapkamer (Pir)' and device.state == 'On') then
            nvs.switchOn().forMin(10).checkFirst()
        end
		
		
	end
}
Jan Jansen
Posts: 229
Joined: Wednesday 30 April 2014 20:27
Target OS: Raspberry Pi / ODroid
Domoticz version: Stable
Location: The Netherlands
Contact:

Re: script doesn't do what I want

Post by Jan Jansen »

jvdz wrote: Monday 15 April 2024 20:00
Maybe this is a nicer/shorter version of your logic using the Event device data:

Code: Select all

        if nvs.state == 'Off' and device.state == 'On' then
             nvs.switchOn().forMin(10)
        elseif nvs.state == 'On' and device.state == 'On' and device.name ~= 'Beweging slaapkamer (Pir)' Then
            nvs.switchOff()
        end
Thanks, this is what I was looking for. Using device.state was new to me
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest