Problem with two closet doors  [Solved]

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

Moderator: leecollings

Post Reply
MikiG
Posts: 19
Joined: Wednesday 04 December 2019 6:33
Target OS: Raspberry Pi / ODroid
Domoticz version: 15182
Contact:

Problem with two closet doors

Post by MikiG »

Hi, I'm new here and also at Domoticz. I have a problem with a script that I have to turn the lights on and off in a two-door closet. I do the control using two Kerui 026 door detectors linked poe rf to a Sonoff Bridge and a Sonoff Basic. I want when I open either door the light will turn on, and when i close the door it will turn off after a while, but if I close a door the light will not turn off until I close the other door. Here's my script:

Code: Select all

return {
    on = {
    	 devices = {'Sonoff_Bridge_127'}
    	 
    	 
    },
   
    execute = function(domoticz, item)
       local switch1 = domoticz.devices('Sonoff_basic_101')
   	   
   	
   	if (item.rawData[1] == '9234606' or item.rawData[1] == '14885486') then 
   	    switch1.switchOn()
   	     domoticz.log('LOG: Vestidor encendido')
     end
     
     if (item.rawData[1] == '9234599' or item.rawData[1] == '14885479') then
   	     switch1.switchOff().afterSec(2)
         end
   
   
 
    end
}
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Problem with two closet doors

Post by waaren »

MikiG wrote: Thursday 05 December 2019 18:39 Hi, I'm new here and also at Domoticz. I have a problem with a script that I have to turn the lights on and off in a two-door closet. I do the control using two Kerui 026 door detectors linked poe rf to a Sonoff Bridge and a Sonoff Basic. I want when I open either door the light will turn on, and when i close the door it will turn off after a while, but if I close a door the light will not turn off until I close the other door. Here's my script:
Sorry but more information is needed to be able to help.
  • What are the names, id's types and subtypes of the door detectors and of the light ?
  • What do the numbers in your script for rawData[1] mean ?
  • What is your definition of 'a while' is this 30 seconds, 5 minutes, a half hour ?
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
MikiG
Posts: 19
Joined: Wednesday 04 December 2019 6:33
Target OS: Raspberry Pi / ODroid
Domoticz version: 15182
Contact:

Re: Problem with two closet doors

Post by MikiG »

Sorry and thank you for answering.

The devices that the script uses are the Sonoff bridge and the sonoff basic. Both have their IDX in Domoticz. Door detectors do not have IDX, are connected via bridge and send a signal according to the door is open '9234606' or the door is closed'9234599'.

The item.rawData[1] refers to the detector signals and copied it from a video I saw on youtube.

The time is written in (afterSec(2)), i.e. 2 seconds after closing the door
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Problem with two closet doors

Post by waaren »

MikiG wrote: Thursday 05 December 2019 20:14 The devices that the script uses are the Sonoff bridge and the sonoff basic. Both have their IDX in Domoticz. Door detectors do not have IDX, are connected via bridge and send a signal according to the door is open '9234606' or the door is closed'9234599'.
If the door contacts are no devices in domoticz but only signal via Sonoff bridge, then the current state can only be determined if it is stored somewhere.
In dzVents I would choose for persistent data but before showing an example I want to be sure what signals are sent by these contacts

Can you use below script and collect relevant loglines when opening and closing the doors ?

Code: Select all

return {
    on = {
    	 devices = {'Sonoff_Bridge_127'}
    },
   
    execute = function(dz, item)
		item.dump()
    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: Problem with two closet doors

Post by waaren »

MikiG wrote: Thursday 05 December 2019 20:14 he item.rawData[1] refers to the detector signals and copied it from a video I saw on youtube.
Thx I have enough information now (deleted the 4 posts with data but I have it)
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: Problem with two closet doors

Post by waaren »

MikiG wrote: Thursday 05 December 2019 20:14 door is open '9234606' or the door is closed'9234599'.
Can you try this one ? (untested)

Code: Select all

return 
{
    on = 
    {
         devices = { 'Sonoff_Bridge_127' }
    },
   
    data =    { 
                states = 
                { 
                    initial = {}
                },
            },
            
    execute = function(dz, item)
        local switch1 = dz.devices('Sonoff_basic_101')

        local openDoor1 = 9234606
        local openDoor2 = 14885486 
        local closedDoor1 = 9234599 
        local closedDoor2 = 14885479
        
        if dz.data.states.door1 == nil then dz.data.states.door1 = 'unknown' end -- initialize persistent data
        if dz.data.states.door2 == nil then dz.data.states.door2 = 'unknown' end -- initialize persistent data
        
        if item.state == openDoor1 then dz.data.states.door1 = 'open'
        elseif item.state == openDoor2 then dz.data.states.door2 = 'open'
        elseif item.state == closedDoor1 then dz.data.states.door1 = 'closed'
        elseif item.state == closedDoor2 then dz.data.states.door2 = 'closed'
        end
        
        if item.state == openDoor1 or item.state == openDoor2 then 
            switch1.switchOn()
            dz.log('LOG: Vestidor encendido')
        elseif dz.data.states.door1 == 'closed' and dz.data.states.door2 == 'closed' then
            switch1.switchOff().afterSec(2)
        end

    end
}
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
MikiG
Posts: 19
Joined: Wednesday 04 December 2019 6:33
Target OS: Raspberry Pi / ODroid
Domoticz version: 15182
Contact:

Re: Problem with two closet doors

Post by MikiG »

Thank you for your work. I just tried it and it doesn't work
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Problem with two closet doors

Post by waaren »

MikiG wrote: Thursday 05 December 2019 22:10 Thank you for your work. I just tried it and it doesn't work
Maybe you can elaborate a bit on this. 'It doesn't work' does not help lot in finding a solution.
What does not work, What do you see happening, What do you see in the logfile
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
MikiG
Posts: 19
Joined: Wednesday 04 December 2019 6:33
Target OS: Raspberry Pi / ODroid
Domoticz version: 15182
Contact:

Re: Problem with two closet doors

Post by MikiG »

Excuse me again. I mean you don't activate the basic sonoff, but it also doesn't give me any mistake





2019-12-05 23:08:45.674 Status: dzVents: Info: Handling events for: "Sonoff_Bridge_127", value: "9234606"
2019-12-05 23:08:45.674 Status: dzVents: Info: ------ Start internal script: Luz entrada2: Device: "Sonoff_Bridge_127 (Sonoff)", Index: 20
2019-12-05 23:08:45.675 Status: dzVents: Info: ------ Finished Luz entrada2
2019-12-05 23:08:45.675 Status: dzVents: Info: ------ Start internal script: Script #2: Device: "Sonoff_Bridge_127 (Sonoff)", Index: 20
2019-12-05 23:08:45.677 Status: dzVents: Info: ------ Finished Script #2

2019-12-05 23:08:54.555 MQTT: Topic: domoticz/in, Message: {"idx":20,"nvalue":0,"svalue":"9234599","Battery":200,"RSSI":5}
2019-12-05 23:08:54.720 Status: dzVents: Info: Handling events for: "Sonoff_Bridge_127", value: "9234599"
2019-12-05 23:08:54.720 Status: dzVents: Info: ------ Start internal script: Luz entrada2: Device: "Sonoff_Bridge_127 (Sonoff)", Index: 20
2019-12-05 23:08:54.721 Status: dzVents: Info: ------ Finished Luz entrada2
2019-12-05 23:08:54.721 Status: dzVents: Info: ------ Start internal script: Script #2: Device: "Sonoff_Bridge_127 (Sonoff)", Index: 20
2019-12-05 23:08:54.723 Status: dzVents: Info: ------ Finished Script #2

2019-12-05 23:09:05.224 MQTT: Topic: domoticz/in, Message: {"idx":20,"nvalue":0,"svalue":"14885486","Battery":200,"RSSI":5}
2019-12-05 23:09:05.363 Status: dzVents: Info: Handling events for: "Sonoff_Bridge_127", value: "14885486"
2019-12-05 23:09:05.363 Status: dzVents: Info: ------ Start internal script: Luz entrada2: Device: "Sonoff_Bridge_127 (Sonoff)", Index: 20
2019-12-05 23:09:05.364 Status: dzVents: Info: ------ Finished Luz entrada2
2019-12-05 23:09:05.364 Status: dzVents: Info: ------ Start internal script: Script #2: Device: "Sonoff_Bridge_127 (Sonoff)", Index: 20
2019-12-05 23:09:05.366 Status: dzVents: Info: ------ Finished Script #2

2019-12-05 23:09:10.485 MQTT: Topic: domoticz/in, Message: {"idx":20,"nvalue":0,"svalue":"14885479","Battery":200,"RSSI":5}
2019-12-05 23:09:10.600 Status: dzVents: Info: Handling events for: "Sonoff_Bridge_127", value: "14885479"
2019-12-05 23:09:10.600 Status: dzVents: Info: ------ Start internal script: Luz entrada2: Device: "Sonoff_Bridge_127 (Sonoff)", Index: 20
2019-12-05 23:09:10.603 Status: dzVents: Info: ------ Finished Luz entrada2
2019-12-05 23:09:10.603 Status: dzVents: Info: ------ Start internal script: Script #2: Device: "Sonoff_Bridge_127 (Sonoff)", Index: 20
2019-12-05 23:09:10.607 Status: dzVents: Info: ------ Finished Script #2
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Problem with two closet doors  [Solved]

Post by waaren »

MikiG wrote: Thursday 05 December 2019 23:10 Excuse me again. I mean you don't activate the basic sonoff, but it also doesn't give me any mistake
You have two script executing when the Sonoff Bridge sends something to domoticz please de-activate the old ones.
Please use this one (it should produce some extra messages to the log)

Code: Select all

return 
{
    on = 
    {
         devices = { 'Sonoff_Bridge_127' }
    },
   
    data =    { 
                states = 
                { 
                    initial = {}
                },
            },
            
    execute = function(dz, item)
        local switch1 = dz.devices('Sonoff_basic_101')
        
		local state = tonumber(item.state)
		
        local openDoor1 = 9234606
        local openDoor2 = 14885486 
        local closedDoor1 = 9234599 
        local closedDoor2 = 14885479
        
        if dz.data.states.door1 == nil then dz.data.states.door1 = 'unknown' end -- initialize persistent data
        if dz.data.states.door2 == nil then dz.data.states.door2 = 'unknown' end -- initialize persistent data
        
        if state == openDoor1 then dz.data.states.door1 = 'open'
        elseif state == openDoor2 then dz.data.states.door2 = 'open'
        elseif state == closedDoor1 then dz.data.states.door1 = 'closed'
        elseif state == closedDoor2 then dz.data.states.door2 = 'closed'
        end
        
        if state == openDoor1 or state == openDoor2 then 
            switch1.switchOn()
            dz.log('LOG: Vestidor encendido')
        elseif dz.data.states.door1 == 'closed' and dz.data.states.door2 == 'closed' then
            switch1.switchOff().afterSec(2)
        end

		dz.utils.dumpTable(dz.data)
    end
}
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
MikiG
Posts: 19
Joined: Wednesday 04 December 2019 6:33
Target OS: Raspberry Pi / ODroid
Domoticz version: 15182
Contact:

Re: Problem with two closet doors

Post by MikiG »

Hahahaha, it works!!! Thank you very much, you are my hero. Thank you!!

Could you explain to me a little bit what you've done?

explanation for dummys, of course

I'd like to put a timer, would it be very complicated?
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Problem with two closet doors

Post by waaren »

MikiG wrote: Friday 06 December 2019 0:05 Could you explain to me a little bit what you've done?
See my comment in the script below.
I'd like to put a timer, would it be very complicated?
It depends on what you want to achieve with the timer.

(probably a language thingy but please provide enough information I your posts so that other forum members do not have to guess what you see or mean. For you it is probably all clear but we have to work with the text / code / log that you put in the posts)

Code: Select all

return 
{
    on = 
    {
         devices = { 'Sonoff_Bridge_127' }
    },
   
    data =    {                  -- In this section I declare a persistent table called states
                states =                   
                { 
                    initial = {} -- and initialize it as en empty table
                },
            },
            
    execute = function(dz, item)
        local switch1 = dz.devices('Sonoff_basic_101')
        
        local state = tonumber(item.state) -- here I convert item.state (which is the same as rawData[1]) from a string to a number
        
        local openDoor1 = 9234606       -- just giving a name to the numbers to understand what they mean
        local openDoor2 = 14885486 
        local closedDoor1 = 9234599 
        local closedDoor2 = 14885479
        
        if dz.data.states.door1 == nil then dz.data.states.door1 = 'unknown' end -- initialize persistent data  -- make sure there are values for the table variables door1 and door2
        if dz.data.states.door2 == nil then dz.data.states.door2 = 'unknown' end -- initialize persistent data
        
        if state == openDoor1 then dz.data.states.door1 = 'open'              -- store the state of Sonoff bridge in the right table varaiable 
        elseif state == openDoor2 then dz.data.states.door2 = 'open'
        elseif state == closedDoor1 then dz.data.states.door1 = 'closed'
        elseif state == closedDoor2 then dz.data.states.door2 = 'closed'
        end
        
        if state == openDoor1 or state == openDoor2 then  -- if one or both doors are open, switch on the light
            switch1.switchOn()
            dz.log('LOG: Vestidor encendido')
        elseif dz.data.states.door1 == 'closed' and dz.data.states.door2 == 'closed' then -- if a door is closed check the last state of the other door to see if it also closed
            switch1.switchOff().afterSec(2)
        end

        dz.utils.dumpTable(dz.data) -- just to show the content of the persistent data. Can be removed if all is working as expected
    end
}
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
MikiG
Posts: 19
Joined: Wednesday 04 December 2019 6:33
Target OS: Raspberry Pi / ODroid
Domoticz version: 15182
Contact:

Re: Problem with two closet doors

Post by MikiG »

waaren wrote: Friday 06 December 2019 0:36
MikiG wrote: Friday 06 December 2019 0:05 Could you explain to me a little bit what you've done?
See my comment in the script below.
I'd like to put a timer, would it be very complicated?
It depends on what you want to achieve with the timer.

(probably a language thingy but please provide enough information I your posts so that other forum members do not have to guess what you see or mean. For you it is probably all clear but we have to work with the text / code / log that you put in the posts)

Code: Select all

return 
{
    on = 
    {
         devices = { 'Sonoff_Bridge_127' }
    },
   
    data =    {                  -- In this section I declare a persistent table called states
                states =                   
                { 
                    initial = {} -- and initialize it as en empty table
                },
            },
            
    execute = function(dz, item)
        local switch1 = dz.devices('Sonoff_basic_101')
        
        local state = tonumber(item.state) -- here I convert item.state (which is the same as rawData[1]) from a string to a number
        
        local openDoor1 = 9234606       -- just giving a name to the numbers to understand what they mean
        local openDoor2 = 14885486 
        local closedDoor1 = 9234599 
        local closedDoor2 = 14885479
        
        if dz.data.states.door1 == nil then dz.data.states.door1 = 'unknown' end -- initialize persistent data  -- make sure there are values for the table variables door1 and door2
        if dz.data.states.door2 == nil then dz.data.states.door2 = 'unknown' end -- initialize persistent data
        
        if state == openDoor1 then dz.data.states.door1 = 'open'              -- store the state of Sonoff bridge in the right table varaiable 
        elseif state == openDoor2 then dz.data.states.door2 = 'open'
        elseif state == closedDoor1 then dz.data.states.door1 = 'closed'
        elseif state == closedDoor2 then dz.data.states.door2 = 'closed'
        end
        
        if state == openDoor1 or state == openDoor2 then  -- if one or both doors are open, switch on the light
            switch1.switchOn()
            dz.log('LOG: Vestidor encendido')
        elseif dz.data.states.door1 == 'closed' and dz.data.states.door2 == 'closed' then -- if a door is closed check the last state of the other door to see if it also closed
            switch1.switchOff().afterSec(2)
        end

        dz.utils.dumpTable(dz.data) -- just to show the content of the persistent data. Can be removed if all is working as expected
    end
}


Hello again, and I apologize again for my English, it is very basic. I do the translations on Google and I don't know if what I write in Spanish translates correctly into English.

The reason I want to put a timer in the script is that the room where the closet is, is a dormitory shared with another person and I don't want the light to come on when the other person is sleeping. Could it be said that only the script will work between 7 in the morning and 23:30 at night? Thank you very much for your help. a greeting
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Problem with two closet doors

Post by waaren »

MikiG wrote: Saturday 07 December 2019 0:47 The reason I want to put a timer in the script is that the room where the closet is, is a dormitory shared with another person and I don't want the light to come on when the other person is sleeping. Could it be said that only the script will work between 7 in the morning and 23:30 at night?
Below script should do just that.

Code: Select all

return 
{
    on = 
    {
         devices = { ['Sonoff_Bridge_127'] = 'at 07:00-23:30' }, -- trigger the script on updates of the Sonoff bridge but only between 7:00 and 23:30
    },
   
    data =    {                  -- In this section I declare a persistent table called states
                states =                   
                { 
                    initial = {} -- and initialize it as en empty table
                },
            },
            
    execute = function(dz, item)
        local switch1 = dz.devices('Sonoff_basic_101')
        
        local state = tonumber(item.state) -- here I convert item.state (which is the same as rawData[1]) from a string to a number
        
        local openDoor1 = 9234606       -- just giving a name to the numbers to understand what they mean
        local openDoor2 = 14885486 
        local closedDoor1 = 9234599 
        local closedDoor2 = 14885479
        
        if dz.data.states.door1 == nil then dz.data.states.door1 = 'unknown' end -- initialize persistent data  -- make sure there are values for the table variables door1 and door2
        if dz.data.states.door2 == nil then dz.data.states.door2 = 'unknown' end -- initialize persistent data
        
        if state == openDoor1 then dz.data.states.door1 = 'open'              -- store the state of Sonoff bridge in the right table varaiable 
        elseif state == openDoor2 then dz.data.states.door2 = 'open'
        elseif state == closedDoor1 then dz.data.states.door1 = 'closed'
        elseif state == closedDoor2 then dz.data.states.door2 = 'closed'
        end
        
        if state == openDoor1 or state == openDoor2 then  -- if one or both doors are open, switch on the light
            switch1.switchOn()
            dz.log('LOG: Vestidor encendido')
        elseif dz.data.states.door1 == 'closed' and dz.data.states.door2 == 'closed' then -- if a door is closed check the last state of the other door to see if it also closed
            switch1.switchOff().afterSec(2)
        end

        dz.utils.dumpTable(dz.data) -- just to show the content of the persistent data. Can be removed if all is working as expected
    end
}
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
MikiG
Posts: 19
Joined: Wednesday 04 December 2019 6:33
Target OS: Raspberry Pi / ODroid
Domoticz version: 15182
Contact:

Re: Problem with two closet doors

Post by MikiG »

waaren wrote: Saturday 07 December 2019 1:07
MikiG wrote: Saturday 07 December 2019 0:47 The reason I want to put a timer in the script is that the room where the closet is, is a dormitory shared with another person and I don't want the light to come on when the other person is sleeping. Could it be said that only the script will work between 7 in the morning and 23:30 at night?
Below script should do just that.

Code: Select all

return 
{
    on = 
    {
         devices = { ['Sonoff_Bridge_127'] = 'at 07:00-23:30' }, -- trigger the script on updates of the Sonoff bridge but only between 7:00 and 23:30
    },
   
    data =    {                  -- In this section I declare a persistent table called states
                states =                   
                { 
                    initial = {} -- and initialize it as en empty table
                },
            },
            
    execute = function(dz, item)
        local switch1 = dz.devices('Sonoff_basic_101')
        
        local state = tonumber(item.state) -- here I convert item.state (which is the same as rawData[1]) from a string to a number
        
        local openDoor1 = 9234606       -- just giving a name to the numbers to understand what they mean
        local openDoor2 = 14885486 
        local closedDoor1 = 9234599 
        local closedDoor2 = 14885479
        
        if dz.data.states.door1 == nil then dz.data.states.door1 = 'unknown' end -- initialize persistent data  -- make sure there are values for the table variables door1 and door2
        if dz.data.states.door2 == nil then dz.data.states.door2 = 'unknown' end -- initialize persistent data
        
        if state == openDoor1 then dz.data.states.door1 = 'open'              -- store the state of Sonoff bridge in the right table varaiable 
        elseif state == openDoor2 then dz.data.states.door2 = 'open'
        elseif state == closedDoor1 then dz.data.states.door1 = 'closed'
        elseif state == closedDoor2 then dz.data.states.door2 = 'closed'
        end
        
        if state == openDoor1 or state == openDoor2 then  -- if one or both doors are open, switch on the light
            switch1.switchOn()
            dz.log('LOG: Vestidor encendido')
        elseif dz.data.states.door1 == 'closed' and dz.data.states.door2 == 'closed' then -- if a door is closed check the last state of the other door to see if it also closed
            switch1.switchOff().afterSec(2)
        end

        dz.utils.dumpTable(dz.data) -- just to show the content of the persistent data. Can be removed if all is working as expected
    end
}
Thank you very much. I have a doubt. The timer you set to Sonoff_bridge_127 activates it at the specified time, but if I use the same device in other scripts and the timer is not activated, will it work the same way or will I have problems?
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Problem with two closet doors

Post by waaren »

MikiG wrote: Saturday 07 December 2019 21:58 Thank you very much. I have a doubt. The timer you set to Sonoff_bridge_127 activates it at the specified time, but if I use the same device in other scripts and the timer is not activated, will it work the same way or will I have problems?
The timer I set here is not meant for activating the Sonoff. It defines the time window where this script will trigger when activated by the Sonoff. It is not influenced by other scripts outside this time window.
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
MikiG
Posts: 19
Joined: Wednesday 04 December 2019 6:33
Target OS: Raspberry Pi / ODroid
Domoticz version: 15182
Contact:

Re: Problem with two closet doors

Post by MikiG »

waaren wrote: Saturday 07 December 2019 22:14
MikiG wrote: Saturday 07 December 2019 21:58 Thank you very much. I have a doubt. The timer you set to Sonoff_bridge_127 activates it at the specified time, but if I use the same device in other scripts and the timer is not activated, will it work the same way or will I have problems?
The timer I set here is not meant for activating the Sonoff. It defines the time window where this script will trigger when activated by the Sonoff. It is not influenced by other scripts outside this time window.
OK, thank you very much.
MikiG
Posts: 19
Joined: Wednesday 04 December 2019 6:33
Target OS: Raspberry Pi / ODroid
Domoticz version: 15182
Contact:

Re: Problem with two closet doors

Post by MikiG »

waaren wrote: Saturday 07 December 2019 22:14
MikiG wrote: Saturday 07 December 2019 21:58 Thank you very much. I have a doubt. The timer you set to Sonoff_bridge_127 activates it at the specified time, but if I use the same device in other scripts and the timer is not activated, will it work the same way or will I have problems?
The timer I set here is not meant for activating the Sonoff. It defines the time window where this script will trigger when activated by the Sonoff. It is not influenced by other scripts outside this time window.
Hello again waaren. I keep working with closet doors. I want to make sure that if a door doesn't close properly and the light stays on, then it goes out after 5 minutes. I'm using lastUpdate.minutes Aug> 5, but something is wrong because the light doesn't go out. What I can do? thank you very much for your great work

Code: Select all




return 
{
    on = 
    {
         devices = { ['Sonoff_Bridge_127'] = {'at 07:00-23:30' }} -- trigger the script on updates of the Sonoff bridge but only between 7:00 and 23:30
    },
   
    data =    {                  -- In this section I declare a persistent table called states
                states =                   
                { 
                    initial = {} -- and initialize it as en empty table
                },
            },
            
    execute = function(dz, item)
        local switch1 = dz.devices('Sonoff_basic_101')
        
        local state = tonumber(item.state) -- here I convert item.state (which is the same as rawData[1]) from a string to a number
        
        local openDoor1 = 9234606       -- just giving a name to the numbers to understand what they mean
        local openDoor2 = 14885486 
        local closedDoor1 = 9234599 
        local closedDoor2 = 14885479
        
        if dz.data.states.door1 == nil then dz.data.states.door1 = 'unknown' end -- initialize persistent data  -- make sure there are values for the table variables door1 and door2
        if dz.data.states.door2 == nil then dz.data.states.door2 = 'unknown' end -- initialize persistent data
        
        if state == openDoor1 then dz.data.states.door1 = 'open'              -- store the state of Sonoff bridge in the right table varaiable 
        elseif state == openDoor2 then dz.data.states.door2 = 'open'
        elseif state == closedDoor1 then dz.data.states.door1 = 'closed'
        elseif state == closedDoor2 then dz.data.states.door2 = 'closed'
        end
        
        if state == openDoor1 or state == openDoor2 then  -- if one or both doors are open, switch on the light
            switch1.switchOn()
            dz.log('LOG: Vestidor encendido')
        elseif dz.data.states.door1 == 'closed' and dz.data.states.door2 == 'closed' then -- if a door is closed check the last state of the other door to see if it also closed
            switch1.switchOff().afterSec(2)
        end
        
        if switch1.state == 'On' and (switc1h.lastUpdate.minutesAgo > 5) then
            switch1.switchOff()
            dz.log('LOG: Hasta los huevos')
        end

        dz.utils.dumpTable(dz.data) -- just to show the content of the persistent data. Can be removed if all is working as expected
    end
}
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Problem with two closet doors

Post by waaren »

MikiG wrote: Thursday 12 December 2019 18:41 I keep working with closet doors. I want to make sure that if a door doesn't close properly and the light stays on, then it goes out after 5 minutes. I'm using

Code: Select all

 lastUpdate.minutesAgo> 5,
        if switch1.state == 'On' and (switc1h.lastUpdate.minutesAgo > 5) then
            switch1.switchOff()
            dz.log('LOG: Hasta los huevos')
        end
The issue with this part of your modification is that the script is not triggered at moments the light is on for more then 5 minutes. It is only triggered when a door is opened or closed. That is also the reason why your typing error (You typed switc1h where it should be switch1) is not noticed.

Try this one

Code: Select all

return 
{
    on = 
    {
         devices = { ['Sonoff_Bridge_127'] = {'at 07:00-23:30' }} -- trigger the script on updates of the Sonoff bridge but only between 7:00 and 23:30
    },
   
    data =    {                  -- In this section I declare a persistent table called states
                states =                   
                { 
                    initial = {} -- and initialize it as en empty table
                },
            },
            
    execute = function(dz, item)
        local switch1 = dz.devices('Sonoff_basic_101')
        
        local state = tonumber(item.state) -- here I convert item.state (which is the same as rawData[1]) from a string to a number
        
        local openDoor1 = 9234606       -- just giving a name to the numbers to understand what they mean
        local openDoor2 = 14885486 
        local closedDoor1 = 9234599 
        local closedDoor2 = 14885479
        
        if dz.data.states.door1 == nil then dz.data.states.door1 = 'unknown' end -- initialize persistent data  -- make sure there are values for the table variables door1 and door2
        if dz.data.states.door2 == nil then dz.data.states.door2 = 'unknown' end -- initialize persistent data
        
        if state == openDoor1 then dz.data.states.door1 = 'open'              -- store the state of Sonoff bridge in the right table varaiable 
        elseif state == openDoor2 then dz.data.states.door2 = 'open'
        elseif state == closedDoor1 then dz.data.states.door1 = 'closed'
        elseif state == closedDoor2 then dz.data.states.door2 = 'closed'
        end
        
        switch1.cancelQueuedCommands() -- after this a new scheduled switchOff will be send 
        if state == openDoor1 or state == openDoor2 then  -- if one or both doors are open, switch on the light
            switch1.switchOn() 
            dz.log('LOG: Vestidor encendido')
            
            switch1.switchOff().afterSec(300) -- just to be sure the light will be switched off regardless of the doors are closed 
            dz.log('scheduled switchOff for 5 minutes after switching it on. Just be sure you do not read a newspaper that long...')
        elseif dz.data.states.door1 == 'closed' and dz.data.states.door2 == 'closed' then -- if a door is closed check the last state of the other door to see if it also closed
            switch1.switchOff().afterSec(2)
        end

        dz.utils.dumpTable(dz.data) -- just to show the content of the persistent data. Can be removed if all is working as expected
    end
}
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
MikiG
Posts: 19
Joined: Wednesday 04 December 2019 6:33
Target OS: Raspberry Pi / ODroid
Domoticz version: 15182
Contact:

Re: Problem with two closet doors

Post by MikiG »

waaren wrote: Thursday 12 December 2019 19:32 The issue with this part of your modification is that the script is not triggered at moments the light is on for more then 5 minutes. It is only triggered when a door is opened or closed. That is also the reason why your typing error (You typed switc1h where it should be switch1) is not noticed.
Thank you very much, it works perfect
Post Reply

Who is online

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