How to get status of a group

Moderator: leecollings

JuanUil
Posts: 500
Joined: Friday 22 May 2015 12:21
Target OS: Raspberry Pi / ODroid
Domoticz version: 4.11083
Location: Asten NB Nederland
Contact:

How to get status of a group

Post 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
Your mind is like a parachute,
It only works when it is opened!

RPI4 several Fibaro, KaKu, Neocoolcam switches, Z-Wave, Zigbee2Mqtt, Ikea bulbs and remote, Zigbee temp nodes
User avatar
jvdz
Posts: 2333
Joined: Tuesday 30 December 2014 19:25
Target OS: Raspberry Pi / ODroid
Domoticz version: 4.107
Location: Netherlands
Contact:

Re: How to get status of a group

Post 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
New Garbage collection scripts: https://github.com/jvanderzande/GarbageCalendar
Andrex
Posts: 92
Joined: Thursday 18 February 2016 9:11
Target OS: Linux
Domoticz version:
Contact:

Re: How to get status of a group

Post by Andrex »

Is there any plan to develop the ability to know the state of a group?
gertlind1
Posts: 29
Joined: Wednesday 25 December 2013 12:17
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: How to get status of a group

Post 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
---
Gert
Andrex
Posts: 92
Joined: Thursday 18 February 2016 9:11
Target OS: Linux
Domoticz version:
Contact:

Re: How to get status of a group

Post by Andrex »

Coool, it does work!
Thanks Gert!!!
Andrex
Posts: 92
Joined: Thursday 18 February 2016 9:11
Target OS: Linux
Domoticz version:
Contact:

Re: How to get status of a group

Post by Andrex »

It changes state only when I open the floorplan. Is there a way to changes every x seconds?
gertlind1
Posts: 29
Joined: Wednesday 25 December 2013 12:17
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: How to get status of a group

Post 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.
---
Gert
Andrex
Posts: 92
Joined: Thursday 18 February 2016 9:11
Target OS: Linux
Domoticz version:
Contact:

Re: How to get status of a group

Post 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)
gertlind1
Posts: 29
Joined: Wednesday 25 December 2013 12:17
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: How to get status of a group

Post by gertlind1 »

Hmm. Remove the libs.

Make it look like this :
function GroupState(sArray)

I have my functions scripts in a singel libs file.
---
Gert
Andrex
Posts: 92
Joined: Thursday 18 February 2016 9:11
Target OS: Linux
Domoticz version:
Contact:

Re: How to get status of a group

Post 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?
Andrex
Posts: 92
Joined: Thursday 18 February 2016 9:11
Target OS: Linux
Domoticz version:
Contact:

Re: How to get status of a group

Post 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).
Derik
Posts: 1602
Joined: Friday 18 October 2013 23:33
Target OS: Raspberry Pi / ODroid
Domoticz version: BETA
Location: Arnhem/Nijmegen Nederland
Contact:

Re: How to get status of a group

Post by Derik »

what type of script is it?
lua with crontab?
lua with time?
Or ??
Xu4: Beta Extreme antenna RFXcomE,WU Fi Ping ip P1 Gen5 PVOutput Harmony HUE SolarmanPv OTG Winddelen Alive ESP Buienradar MySensors WOL Winddelen counting RPi: Beta SMAspot RFlinkTest Domoticz ...Different backups
gertlind1
Posts: 29
Joined: Wednesday 25 December 2013 12:17
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: How to get status of a group

Post 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
---
Gert
jesses
Posts: 7
Joined: Tuesday 23 January 2018 11:54
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: How to get status of a group

Post 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
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: How to get status of a group

Post 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
}
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
jesses
Posts: 7
Joined: Tuesday 23 January 2018 11:54
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: How to get status of a group

Post 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
Last edited by jesses on Saturday 27 January 2018 20:44, edited 1 time in total.
jesses
Posts: 7
Joined: Tuesday 23 January 2018 11:54
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: How to get status of a group

Post 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
jesses
Posts: 7
Joined: Tuesday 23 January 2018 11:54
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: How to get status of a group

Post 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
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: How to get status of a group

Post 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
}
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
jesses
Posts: 7
Joined: Tuesday 23 January 2018 11:54
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: How to get status of a group

Post 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 
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest