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:
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
}
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 ?
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
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 ?
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
}
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
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)
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
}
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)
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
}
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)
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
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?
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
}
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?
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?
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.
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.
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
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
}
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
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.
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
}
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.