Page 1 of 2

How to get status of a group

Posted: Saturday 09 January 2016 17:38
by JuanUil
Hi there all,

I am making a LUA-script to switch groups.
Now a group cAn have 3 states, on/off/mixed
how can I determine what stae the group is in in LUA?
I tried otherdevices_svalues but this gives an error, this is a table variable
Any help?
Thnx in advance

Jan

Re: How to get status of a group

Posted: Saturday 09 January 2016 17:59
by jvdz
From the Event Wiki page: https://www.domoticz.com/wiki/Events
To switch a scene or group, put prefix 'Scene:' or 'Group:' in front of the scene name. Note that it is not possible to check the state of a scene or group, since that is not its intended use. Check a (significant) single device from the scene/group table before switching scenes.
Jos

Re: How to get status of a group

Posted: Thursday 03 November 2016 19:48
by Andrex
Is there any plan to develop the ability to know the state of a group?

Re: How to get status of a group

Posted: Thursday 03 November 2016 20:04
by gertlind1
Try this and let me know how it works

Code: Select all

function libs.GroupState(sArray)
        -- v1.0. 2016-01-18
        -- Checks the state of a group of swithces.
        -- USAGE   : GroupState({'Switch1','Switch2','Switch3','Switch4','Switch5',more switches})
        -- RETURNS : 'On'       if all switches are on.
        --         : 'Off'      if all switches are off.
        --         : 'Mixed"    if one or more, but not all switches are on.
        --
        local iState = 0
        local iCount = 0
        local sState = ''
        for i,light in pairs(sArray) do
                if (otherdevices[light] == 'On') then
                        iState = iState + 1
                end
        iCount = iCount + 1
        end
        if(iState == 0) then sState = "Off" end
        if(iState > 0) then sState = 'Mixed' end
        if(iState == iCount) then sState = 'On' end
--      print("iCount   : " .. iCount)
--      print("iState   : " .. iState)
--      print("sState   : " .. sState)
        return sState
end

Re: How to get status of a group

Posted: Thursday 03 November 2016 20:15
by Andrex
Coool, it does work!
Thanks Gert!!!

Re: How to get status of a group

Posted: Thursday 03 November 2016 20:17
by Andrex
It changes state only when I open the floorplan. Is there a way to changes every x seconds?

Re: How to get status of a group

Posted: Thursday 03 November 2016 20:39
by gertlind1
Andrex wrote:It changes state only when I open the floorplan. Is there a way to changes every x seconds?
I don't get you?
It just checks the state of a group of devices.

I use it as below:

Code: Select all

if (devicechanged['Rörelse'] == 'On' and libs.GroupState(aLivingroom) == 'Off') then
If the "Group" of switches is "Off" (all switches is of) the do something.
If the return value is "On" or "Mixed" nothing will happen.

Re: How to get status of a group

Posted: Saturday 05 November 2016 9:51
by Andrex
In the log I've got this error every minute:

Code: Select all

Error: EventSystem: in Update groups state: [string "function libs.GroupState(sArray)..."]:1: attempt to index global 'libs' (a nil value)

Re: How to get status of a group

Posted: Saturday 05 November 2016 10:15
by gertlind1
Hmm. Remove the libs.

Make it look like this :
function GroupState(sArray)

I have my functions scripts in a singel libs file.

Re: How to get status of a group

Posted: Saturday 05 November 2016 10:22
by Andrex
Now I get
Error: EventSystem: Lua script Update groups did not return a commandArray
But when does it run? Who tells it when to run?

Re: How to get status of a group

Posted: Saturday 05 November 2016 11:23
by Andrex
What I was saying previously is that in the floorplan the state of the group is updated only when i refresh the page (the single switches updates instantly).

Re: How to get status of a group

Posted: Saturday 05 November 2016 15:07
by Derik
what type of script is it?
lua with crontab?
lua with time?
Or ??

Re: How to get status of a group

Posted: Monday 07 November 2016 9:38
by gertlind1
It is not a script its a function:

Below is a part of my script script_device_motion.lua

The function checks if a group if switches is on, off or mixed.

Code: Select all

GroupState(sArray)
        -- v1.0. 2016-01-18
        -- Checks the state of a group of swithces.
        -- USAGE   : GroupState({'Switch1','Switch2','Switch3','Switch4','Switch5',more switches})
        -- RETURNS : 'On'       if all switches are on.
        --         : 'Off'      if all switches are off.
        --         : 'Mixed"    if one or more, but not all switches are on.
        --
        local iState = 0
        local iCount = 0
        local sState = ''
        for i,light in pairs(sArray) do
                if (otherdevices[light] == 'On') then
                        iState = iState + 1
                end
        iCount = iCount + 1
        end
        if(iState == 0) then sState = "Off" end
        if(iState > 0) then sState = 'Mixed' end
        if(iState == iCount) then sState = 'On' end
--      print("iCount   : " .. iCount)
--      print("iState   : " .. iState)
--      print("sState   : " .. sState)
        return sState
end



aLivingroom = {'Hörnbord','Soffbord','Blomma','Fönster','Bokhylla'}

if (devicechanged['Rörelse'] == 'On') then
        time = os.date("*t")
        if (GroupState(aLivingroom) == 'Off') then
                if (libs.SunSet() < 30 and time.hour <= 22) then
                        commandArray['Group:Alla Vardagsrum']='On'
                end
        end
    difference = libs.TimeDifference('Rörelse')
    myTime = libs.SecondsToClock(tostring(difference))
    tSunset = libs.SecondsToClock(tostring(libs.SunSet()))
    commandArray[1] = {['UpdateDevice'] = '10|0|'..'Senaste rörelsen &nbsp &nbsp: '..myTime..'<br>Tid till Solnedgång : '..tSunset}
    commandArray[2] = {['UpdateDevice'] = '42' .. '|0|' .. tostring(libs.UpdateCounter('Movement Vardagsrum'))}
    commandArray[3] = {['UpdateDevice'] = '43' .. '|0|' .. tostring(libs.UpdateCounter('Movement Vardagsrum1'))}
end

Re: How to get status of a group

Posted: Wednesday 24 January 2018 10:13
by jesses
gertlind1 wrote: Monday 07 November 2016 9:38 It is not a script its a function:

Below is a part of my script script_device_motion.lua

The function checks if a group if switches is on, off or mixed.
Hi Gert,

Thanks for the script function. Unfortunately I get some errors using it.
The log error is:
Error: EventSystem: in script_device_motion.lua: [string "-- script_device_motion.lua ..."]:3: attempt to index global 'libs' (a nil value)

Please see the complete script below:

Code: Select all

-- script_device_motion.lua

function libs.GroupState(sArray)
        -- v1.0. 2016-01-18
        -- Checks the state of a group of swithces.
        -- USAGE   : GroupState({'Switch1','Switch2','Switch3','Switch4','Switch5',more switches})
        -- RETURNS : 'On'       if all switches are on.
        --         : 'Off'      if all switches are off.
        --         : 'Mixed"    if one or more, but not all switches are on.
        --
        local iState = 0
        local iCount = 0
        local sState = ''
        for i,light in pairs(sArray) do
                if (otherdevices[light] == 'On') then
                        iState = iState + 1
                end
        iCount = iCount + 1
        end
        if(iState == 0) then sState = "Off" end
        if(iState > 0) then sState = 'Mixed' end
        if(iState == iCount) then sState = 'On' end
--      print("iCount   : " .. iCount)
--      print("iState   : " .. iState)
--      print("sState   : " .. sState)
        return sState
end

aLivingroom = {'hue_eettafel','hue_woonkamer_1','hue_keuken_1','hue_keuken_2'}


local motion_switch = 'WoonkamerPIR'
local time_switch = 'schema2'

commandArray = {}

if devicechanged[motion_switch] then
	if otherdevices[motion_switch] == 'On' and (GroupState(aLivingroom) == 'Off') and otherdevices[time_switch] == 'On' then
 	commandArray['Group:Woonkamer']='On'
 	end
end
return commandArray
If I remove 'libs' I dont get any error but the script does not work:

Code: Select all

-- script_device_motion.lua

function GroupState(sArray)
        -- v1.0. 2016-01-18
        -- Checks the state of a group of swithces.
        -- USAGE   : GroupState({'Switch1','Switch2','Switch3','Switch4','Switch5',more switches})
        -- RETURNS : 'On'       if all switches are on.
        --         : 'Off'      if all switches are off.
        --         : 'Mixed"    if one or more, but not all switches are on.
        --
        local iState = 0
        local iCount = 0
        local sState = ''
        for i,light in pairs(sArray) do
                if (otherdevices[light] == 'On') then
                        iState = iState + 1
                end
        iCount = iCount + 1
        end
        if(iState == 0) then sState = "Off" end
        if(iState > 0) then sState = 'Mixed' end
        if(iState == iCount) then sState = 'On' end
--      print("iCount   : " .. iCount)
--      print("iState   : " .. iState)
--      print("sState   : " .. sState)
        return sState
end

aLivingroom = {'hue_eettafel','hue_woonkamer_1','hue_keuken_1','hue_keuken_2'}


local motion_switch = 'WoonkamerPIR'
local time_switch = 'schema2'

commandArray = {}

if devicechanged[motion_switch] then
	if otherdevices[motion_switch] == 'On' and (GroupState(aLivingroom) == 'Off') and otherdevices[time_switch] == 'On' then
 	commandArray['Group:Woonkamer']='On'
 	end
end
return commandArray
Can someone help me with this?

Thanks!

Regards,
Jesse

Re: How to get status of a group

Posted: Wednesday 24 January 2018 15:10
by waaren
I did something similar in dzVents. Hope this will help. Please note that checkFirst() is available in dzVents 2.3.0 onwards which is part of domoticz Beta

Code: Select all

-- CheckFirst() available in dzVents 2.3.0

return { 
--		active = false,
    
on = { devices = { "WoonkamerPIR"  } },
        		
execute = function( domoticz,motionDetector )
	local timeSwitchState = domoticz.devices("schema2").state
	local myGroup = domoticz.groups("Woonkamer")

	if motionDetector.state == "On" and timeSwitchState == "On" then 
		myGroup.switchOn().checkFirst()
	end
end
}

Re: How to get status of a group

Posted: Wednesday 24 January 2018 20:06
by jesses
waaren wrote: Wednesday 24 January 2018 15:10 I did something similar in dzVents. Hope this will help. Please note that checkFirst() is available in dzVents 2.3.0 onwards which is part of domoticz Beta

Code: Select all

-- CheckFirst() available in dzVents 2.3.0

return { 
--		active = false,
    
on = { devices = { "WoonkamerPIR"  } },
        		
execute = function( domoticz,motionDetector )
	local timeSwitchState = domoticz.devices("schema2").state
	local myGroup = domoticz.groups("Woonkamer")

	if motionDetector.state == "On" and timeSwitchState == "On" then 
		myGroup.switchOn().checkFirst()
	end
end
}
Thanks a lot for taking the time to help me! I need to update to beta but I can't do this (running Pi2b) because get errors when I want to update.
I am going to fix the update first, then try the script and let you know! :)

Regards,
Jesse

Re: How to get status of a group

Posted: Wednesday 24 January 2018 20:09
by jesses
waaren wrote: Wednesday 24 January 2018 15:10 I did something similar in dzVents. Hope this will help. Please note that checkFirst() is available in dzVents 2.3.0 onwards which is part of domoticz Beta

Code: Select all

-- CheckFirst() available in dzVents 2.3.0

return { 
--		active = false,
    
on = { devices = { "WoonkamerPIR"  } },
        		
execute = function( domoticz,motionDetector )
	local timeSwitchState = domoticz.devices("schema2").state
	local myGroup = domoticz.groups("Woonkamer")

	if motionDetector.state == "On" and timeSwitchState == "On" then 
		myGroup.switchOn().checkFirst()
	end
end
}
Thanks a lot for taking the time to help me! I need to update to beta but I can't do this (running Pi2b) because get errors when I want to update.
I am going to fix the update first, then try the script and let you know! :)

Regards,
Jesse

Re: How to get status of a group

Posted: Tuesday 30 January 2018 20:15
by jesses
waaren wrote: Wednesday 24 January 2018 15:10 I did something similar in dzVents. Hope this will help. Please note that checkFirst() is available in dzVents 2.3.0 onwards which is part of domoticz Beta

So it took me some time to fix the upgrade. Now i am running dzvents 2.4.1 (this should support checkfirst).
The script is not working, I see no errors in the log but nothings happens when the motionsensor is activated (and the scheme is on)?

Should I choose a dzvents device script as type? Because i tried with dzvents device and dzvents group?

Regards,
Jesse

Re: How to get status of a group

Posted: Tuesday 30 January 2018 20:32
by waaren
<Edit> I don't use the internal script editor so I did not understood your question after first reading.
Script is a Device type and make sure you set the event to active before saving.
<end Edit>

I tested the script (with different names for sensor and group ) in 2.4.0 and it did what I expected.
If you have the Log Level for dzVents (setup, settings, other) set to "Errors + minimal execution level" you should see if the script is executed when the sensor is activated.
To see how the script run you could add some print commands and check the log when you activate the sensor. Like below

Code: Select all

return { 
    
on = { devices = { "WoonkamerPIR"  } },   -- case sensitive must be the exact name of you sensor
        		
execute = function( domoticz,motionDetector )
	
	print ("function started")
	local timeSwitchState = domoticz.devices("schema2").state
	local myGroup = domoticz.groups("Woonkamer")

	if motionDetector.state == "On" and timeSwitchState == "On" then 
		print ("schema2 and WoonkamerPIR both On"
		myGroup.switchOn().checkFirst()
	else
		print ("schema2 and / or WoonkamerPIR Off"
	end
end
}

Re: How to get status of a group

Posted: Sunday 04 February 2018 20:43
by jesses
I tried it for a few days.
When the sensor detects motion, i see the following in the log:
2018-02-04 20:37:00.799 EventSystem: Script event triggered: slaapkamerjm_uit
2018-02-04 20:37:00.802 EventSystem: Script event triggered: woonkamer_uit
2018-02-04 20:37:39.593 dzVents: Info: ------ Start internal script: slaapkamerjm_aan: Device: "PIRslaapkamerJM (zwave_aeon)", Index: 68
2018-02-04 20:37:39.598 dzVents: Info: ------ Finished slaapkamerjm_aan

But the light does not switch on.
Can it be that the script I use to turn the lights off causes an error or something?

Code: Select all

-- script_time_nomotion.lua

local motion_switch = 'PIRslaapkamerJM'
local nomotion_uservar = 'slaapkamerjm'
local status_switch = 'Slaapkamer JM lamp'

commandArray = {}

no_motion_minutes = tonumber(uservariables[nomotion_uservar])

if (otherdevices[motion_switch] == 'Off') then
	no_motion_minutes = no_motion_minutes + 1
	--print('<font color="red">No motion has been detected for: ' ..no_motion_minutes.. ' minutes</font>')
else 
	no_motion_minutes = 0	
end 

commandArray['Variable:' .. nomotion_uservar] = tostring(no_motion_minutes)

if no_motion_minutes > 15 then --change the 30 to the amount of minutes you prefer
 	commandArray[status_switch]='Off'
end

return commandArray