„Domoticz - MikroTik presence management” változatai közötti eltérés

Innen: IT documentation
Nincs szerkesztési összefoglaló
 
(3 közbenső módosítás ugyanattól a szerkesztőtől nincs mutatva)
10. sor: 10. sor:


* Multiple users usage.
* Multiple users usage.
* Turn on specified light(s) after sunset when somebody arrives.
* Turn on specified light(s) (or any swiches) after sunset when somebody arrives.
* Turn off specified light(s) and set the heating to tempering mode when there is nobody at home.
* Turn off specified light(s) and set the heating to tempering mode when there is nobody at home.
* Set the heating to normal mode (previous temperature values per thermostat) when somebody arrives.
* Set the heating to normal mode (previous temperature values per thermostat) when somebody arrives.
16. sor: 16. sor:
== Prerequisites ==
== Prerequisites ==


* Have to know the mac address of the mobile device(s)
* Have to know the mac address of the mobile device(s). Probable you can turn off the Randomized MAC on your devices at the home wifi network.
* Have to create a dummy "thermostat setpoint" device in the Domoticz, this is going to control the temperature of absence.
* Have to create an integer user variable, what is going to store that the system is presence (1) or absence (0) mode.
* Have to create an integer user variable, what is going to store that the system is presence (1) or absence (0) mode.
* Have to create two integer user variable per user, these are going to store that thes user is present or not, and the previous state, that the change can be perceptible.
* Have to create two integer user variable per user, these are going to store that thes user is present or not, and the previous state, that the change can be perceptible.
* Have to create an integer user variable per thermostat, in what the temperature value of presence mode will be stored.
* Have to create an Float user variable per thermostat, in what the temperature value of presence mode will be stored.
* Have to create a dummy "thermostat setpoint" device in the Domoticz, this is going to control the temperature of absence.
* Have to create a selector switch device with Off, Presence, Auto, Absence levels. This device will define the mode of the operation, like:
** Off: the script will be inactive
** Presence: always presence mode, regardless of the position of the mobile devices
** Auto: If at least one devide is at home, the mode is presence
** Absence: always absence mode, regardless of the position of the mobile devices.


== MikroTik ==
== MikroTik ==
The MikroTik router can trigger a script when DHCP leases change, so enough that the current script run at this event, although may occur in communication between MikroTik-Domoticz an error, so it is worth the script running by scheluded too. Recommended schelude: the DHCP lease time.
The MikroTik router can trigger a script when DHCP leases change, so enough that the current script run at this event. Although may occur in communication between MikroTik-Domoticz an error, so it is worth the script running by scheluded too. We can set in the Telemetry varaiable that how many seconds to be force refresh, although there was no change. Recommended that the schelude time and the value of the Telemetry variable be equal.


Put the datas of the users into the below script logically like mac address of mobile device and the domoticz user variable belonging to user and the domoticz api URL.
Put the datas of the users into the below script logically like mac address of mobile device and the domoticz user variable belonging to user and the domoticz api URL.
33. sor: 38. sor:
# @copyright 2022 moszat
# @copyright 2022 moszat
# @license GPL 3
# @license GPL 3
# @version 1.0
# @version 1.1


# See the https://doc.onlinesoft.org/index.php?title=Domoticz_-_MikroTik_presence_management
# See the https://doc.onlinesoft.org/index.php?title=Domoticz_-_MikroTik_presence_management
40. sor: 45. sor:


:local DomoticzURL "http://192.168.1.10:8080/json.htm"
:local DomoticzURL "http://192.168.1.10:8080/json.htm"
:local Telemetry 600s
:local items {
:local items {
{
{
54. sor: 60. sor:


:local searchMac
:local searchMac
:local ctime [/system clock get time]
:local forceupdate false
:global PRESENCE
# Set the global variable if not exsist
if ( [:typeof $PRESENCE]!="array" ) do={
  :set $PRESENCE ({})
}
# Init last update time
if ( [:len ($PRESENCE->"lastupdate")]=0 || ($PRESENCE->"lastupdate")>$ctime ) do={
  :set ($PRESENCE->"lastupdate") 00:00:00
}


## Logic
## Logic
:set $forceupdate ((($PRESENCE->"lastupdate")+$Telemetry)<$ctime)
if ( $forceupdate ) do={
    :set ($PRESENCE->"lastupdate") $ctime
}


:foreach item in=$items do={
:foreach item in=$items do={


   :set searchMac [/ip dhcp-server lease find mac-address=($item->"mac") ]
   :set searchMac [/ip dhcp-server lease find mac-address=($item->"mac") ]
 
  if ( ([:len $searchMac]>0) != ($PRESENCE->"$($item->"pres")") || $forceupdate ) do={
 
      :set ($PRESENCE->"$($item->"pres")") ([:len $searchMac]>0)


  if ([:len $searchMac]>0) do={
      if ($PRESENCE->"$($item->"pres")") do={


      /tool fetch mode=http url=$DomoticzURL http-data="type=command&param=updateuservariable&vname=$($item->"pres")&vtype=INTEGER&vvalue=1" output=none
        /tool fetch mode=http url=$DomoticzURL http-data="type=command&param=updateuservariable&vname=$($item->"pres")&vtype=INTEGER&vvalue=1" output=none


  } else={
      } else={


      /tool fetch mode=http url=$DomoticzURL http-data="type=command&param=updateuservariable&vname=$($item->"pres")&vtype=INTEGER&vvalue=0" output=none
        /tool fetch mode=http url=$DomoticzURL http-data="type=command&param=updateuservariable&vname=$($item->"pres")&vtype=INTEGER&vvalue=0" output=none


      }
   }
   }
}
}
</syntaxhighlight>Assign the script to the DHCP server (in our example: LAN)<syntaxhighlight lang="bash">
</syntaxhighlight>Assign the script to the DHCP server (in our example: LAN)<syntaxhighlight lang="bash">
/ip dhcp-server add address-pool=LAN bootp-support=none disabled=no interface=LAN lease-script="/system script run Domoticz_Presence" name=LAN
/ip dhcp-server add address-pool=LAN bootp-support=none disabled=no interface=LAN lease-script="/system script run Domoticz_Presence" name=LAN
92. sor: 127. sor:
@copyright 2022 moszat
@copyright 2022 moszat
@license GPL 3
@license GPL 3
@version 1.0
@version 2.0


See the https://doc.onlinesoft.org/index.php?title=Domoticz_-_MikroTik_presence_management
See the https://doc.onlinesoft.org/index.php?title=Domoticz_-_MikroTik_presence_management
108. sor: 143. sor:
-- Users' data
-- Users' data
--
--
-- NAME        User's name (free string)
-- USERS[NAME]         User's name (free string)
-- TRIGGERVAR  The variable what the external system is triggered. (integer, 1 = presence, 0 = absence)
-- USERS[TRIGGERVAR]   The variable what the external system is triggered. (integer, 1 = presence, 0 = absence)
-- STOREVAR    The variable in what the previous state be stored (integer)
-- USERS[STOREVAR]     The variable in what the previous state be stored (integer)


local USERS = {
local USERS = {
135. sor: 170. sor:
-- Heating contol
-- Heating contol
--
--
-- ABSENCEDEVICE   A dummy thermostat setpoint device
-- PRESENCEDEVICE          A dummy selector device for set to presence mode
-- DEVICENAME       Thermostat setpoint device name
-- ABSENCEDEVICE           A dummy thermostat setpoint device for set to absence temperature
-- STOREVAR         The variable in what the normal degree be stored in absence mode
-- THERMOSTATS[DEVICENAMEThermostat setpoint device name
-- THERMOSTATS[STOREVAR]    The variable in what the normal degree be stored in absence mode


local PRESENCEDEVICE = 'Presence'
local ABSENCEDEVICE = 'Thermostat Absence'
local ABSENCEDEVICE = 'Thermostat Absence'
local THERMOSTATS = {
local THERMOSTATS = {
165. sor: 202. sor:
local ARRIVING = false
local ARRIVING = false
local PRESENCE = false
local PRESENCE = false
local STOREDPRESENCE = false


-- Logic
-- Logic
175. sor: 213. sor:
end
end


-- Users' presence changed
if ( otherdevices[PRESENCEDEVICE] == nil ) then
print( PRESENCEDEVICE .. ' device does not exist' )
return commandArray
end


for key, value in pairs (USERS) do
if ( otherdevices[PRESENCEDEVICE] == 'Off' ) then
   
if ( uservariables[value['TRIGGERVAR']] == nil ) then
print( value['TRIGGERVAR'] .. ' value does not exist' )
print( 'Presence handle is off. Exiting..' )
return commandArray
elseif ( uservariables[value['STOREVAR']] == nil ) then
print( value['STOREVAR'] .. ' value does not exist' )
end
 
    elseif ( uservariables[value['TRIGGERVAR']] ~=   uservariables[value['STOREVAR']] ) then  
if ( tonumber(uservariables[PRESENCEVAR]) == 0) then
   
    STOREDPRESENCE = false
   
else
   
    STOREDPRESENCE = true
      
      
        if ( tonumber(uservariables[value['TRIGGERVAR']]) == 1 ) then
end
 
commandArray['SetSetPoint:' .. otherdevices_idx[ABSENCEDEVICE]] = tostring(otherdevices[ABSENCEDEVICE])
 
-- Users' presence changed
 
if ( otherdevices[PRESENCEDEVICE] == 'Auto' ) then  
 
    for key, value in pairs (USERS) do
          
          
            print ( value['NAME'] .. ' is arriving ... ')
    if ( uservariables[value['TRIGGERVAR']] == nil ) then
            ARRIVING = true
   
    print( value['TRIGGERVAR'] .. ' value does not exist' )
   
    elseif ( uservariables[value['STOREVAR']] == nil ) then
   
    print( value['STOREVAR'] .. ' value does not exist' )
   
        elseif ( uservariables[value['TRIGGERVAR']] ~=   uservariables[value['STOREVAR']] ) then
          
          
        else
            if ( tonumber(uservariables[value['TRIGGERVAR']]) == 1 ) then
           
                print ( value['NAME'] .. ' is arriving ... ')
                ARRIVING = true
           
            else
           
                print ( value['NAME'] .. ' is leaving ... ')
           
            end
          
          
             print ( value['NAME'] .. ' is leaving ... ')
             commandArray['Variable:' .. value['STOREVAR'] ] = tostring(uservariables[value['TRIGGERVAR']])
   
        end
   
        if ( tonumber(uservariables[value['TRIGGERVAR']]) == 1 ) then
   
            PRESENCE = true
          
          
         end
         end
      
      
     commandArray['Variable:' .. value['STOREVAR'] ] = tostring(uservariables[value['TRIGGERVAR']])
     end
 
elseif ( otherdevices[PRESENCEDEVICE] == 'Presence' ) then


     end
     PRESENCE = true
    print( 'Forced presence is on.' )


    if ( tonumber(uservariables[value['TRIGGERVAR']]) == 1 ) then
elseif ( otherdevices[PRESENCEDEVICE] == 'Absence' ) then


        PRESENCE = true
    PRESENCE = false
   
     print( 'Forced absence is on.' )
     end


end
end
216. sor: 294. sor:
if ( ARRIVING == true ) then
if ( ARRIVING == true ) then


-- LIGHT set on
    -- LIGHT set on


     if ( timeofday['Nighttime'] == true ) then
     if ( timeofday['Nighttime'] ) then


         for key, value in pairs (SWITCHESON) do
         for key, value in pairs (SWITCHESON) do
236. sor: 314. sor:
          
          
     end
     end
   
end


    if (tonumber(uservariables[PRESENCEVAR]) == 0) then
-- Presence / Absence handle


-- HEATING set presence mode
if ( PRESENCE ~= STOREDPRESENCE ) then
   
   
    if ( PRESENCE ) then
       
        -- Set Presence
       
        -- Writes back the new state to PRESENCEVAR user variable
       
        commandArray['Variable:' .. PRESENCEVAR ] = '1'
        print ('Presence set on')
       
        -- HEATING set presence mode


         for key, value in pairs (THERMOSTATS) do
         for key, value in pairs (THERMOSTATS) do
254. sor: 346. sor:
              
              
             commandArray['SetSetPoint:' .. otherdevices_idx[value['DEVICENAME']]] = tostring(uservariables[value['STOREVAR']])
             commandArray['SetSetPoint:' .. otherdevices_idx[value['DEVICENAME']]] = tostring(uservariables[value['STOREVAR']])
             print ( value['DEVICENAME'] .. ' set ' .. uservariables[value['STOREVAR']])  
             print ( value['DEVICENAME'] .. ' set ' .. uservariables[value['STOREVAR']])
end
end
              
              
         end
         end
       
-- PRESENCE set on
    else
       
         commandArray['Variable:' .. PRESENCEVAR ] = '1'
        -- Set Absence
        print ('Presence set on')
       
        -- Writes back the new state to PRESENCEVAR user variable
       
         commandArray['Variable:' .. PRESENCEVAR ] = '0'
    print ('Presence set off')
   
    -- LIGHTIG set absence mode


    end
        for key, value in pairs (SWITCHESOFF) do
 
   
end
    if (  otherdevices[value] == nil ) then
 
   
if (tonumber(uservariables[PRESENCEVAR]) == 1 and PRESENCE == false ) then
    print( otherdevices[value] .. ' device does not exist' )
 
   
-- LIGHT set off
            elseif ( otherdevices[value] == 'On') then  
 
   
    for key, value in pairs (SWITCHESOFF) do
                commandArray[value] = 'Off'
 
                print ( value .. ' set off')
if (  otherdevices[value] == nil ) then
   
            end
print( otherdevices[value] .. ' device does not exist' )
           
        elseif ( otherdevices[value] == 'On') then  
            commandArray[value] = 'Off'
            print ( value .. ' set off')
         end
         end
          
          
    end
        -- HEATING set absence mode
-- HEATING set absence mode
    if ( otherdevices[ABSENCEDEVICE] == nil ) then
   
if ( otherdevices[ABSENCEDEVICE] == nil ) then
    print( ABSENCEDEVICE .. ' device does not exist' )
   
    else
   
    for key, value in pairs (THERMOSTATS) do
   
    if ( uservariables[value['STOREVAR']] == nil ) then
   
    print( value['STOREVAR'] .. ' value does not exist' )
   
    elseif ( otherdevices[value['DEVICENAME']] == nil ) then
   
    print( value['DEVICENAME'] .. ' device does not exist' )
   
    else
   
    commandArray['Variable:' .. value['STOREVAR']] = otherdevices[value['DEVICENAME']]
    commandArray['SetSetPoint:' .. otherdevices_idx[value['DEVICENAME']]] = tostring(otherdevices[ABSENCEDEVICE])
    print ( value['DEVICENAME'] .. ' set ' .. otherdevices[ABSENCEDEVICE] )
   
    end
   
    end
print( ABSENCEDEVICE .. ' device does not exist' )
    end
else


for key, value in pairs (THERMOSTATS) do
    end
 
if ( uservariables[value['STOREVAR']] == nil ) then
 
print( value['STOREVAR'] .. ' value does not exist' )
 
elseif ( otherdevices[value['DEVICENAME']] == nil ) then
 
print( value['DEVICENAME'] .. ' device does not exist' )
 
else
 
commandArray['Variable:' .. value['STOREVAR']] = otherdevices[value['DEVICENAME']]
commandArray['SetSetPoint:' .. otherdevices_idx[value['DEVICENAME']]] = tostring(otherdevices[ABSENCEDEVICE])
print ( value['DEVICENAME'] .. ' set ' .. otherdevices[ABSENCEDEVICE] )
 
end
 
end
end
 
-- PRESENCE set off
 
commandArray['Variable:' .. PRESENCEVAR ] = '0'
print ('Presence set off')


end
end

A lap jelenlegi, 2022. november 9., 08:15-kori változata


Summary

In this method the base of presence will be a mobile device of the user. If the device connected to the home network then we consider that the user is at home.

A MikroTik router script is watching that the mac address of the device is present in the network and report this to the Domoticz Home Automation System. A Domoticz Lua script manages the arrives and the leaves and related events.

Features

  • Multiple users usage.
  • Turn on specified light(s) (or any swiches) after sunset when somebody arrives.
  • Turn off specified light(s) and set the heating to tempering mode when there is nobody at home.
  • Set the heating to normal mode (previous temperature values per thermostat) when somebody arrives.

Prerequisites

  • Have to know the mac address of the mobile device(s). Probable you can turn off the Randomized MAC on your devices at the home wifi network.
  • Have to create an integer user variable, what is going to store that the system is presence (1) or absence (0) mode.
  • Have to create two integer user variable per user, these are going to store that thes user is present or not, and the previous state, that the change can be perceptible.
  • Have to create an Float user variable per thermostat, in what the temperature value of presence mode will be stored.
  • Have to create a dummy "thermostat setpoint" device in the Domoticz, this is going to control the temperature of absence.
  • Have to create a selector switch device with Off, Presence, Auto, Absence levels. This device will define the mode of the operation, like:
    • Off: the script will be inactive
    • Presence: always presence mode, regardless of the position of the mobile devices
    • Auto: If at least one devide is at home, the mode is presence
    • Absence: always absence mode, regardless of the position of the mobile devices.

MikroTik

The MikroTik router can trigger a script when DHCP leases change, so enough that the current script run at this event. Although may occur in communication between MikroTik-Domoticz an error, so it is worth the script running by scheluded too. We can set in the Telemetry varaiable that how many seconds to be force refresh, although there was no change. Recommended that the schelude time and the value of the Telemetry variable be equal.

Put the datas of the users into the below script logically like mac address of mobile device and the domoticz user variable belonging to user and the domoticz api URL.

After save the script as "Domoticz_Presence", the script policy is enought if it is read only (read, policy, test)

## Domoticz presence handle MikroTik script

# @author moszat <moszat@onlinesoft.org>
# @copyright 2022 moszat
# @license GPL 3
# @version 1.1

# See the https://doc.onlinesoft.org/index.php?title=Domoticz_-_MikroTik_presence_management

## Parameters

:local DomoticzURL "http://192.168.1.10:8080/json.htm"
:local Telemetry 600s
:local items {
{
   pres="USER1_PRESENCE";
   mac="AA:BB:CC:DD:EE:FF";
};
{
   pres="USER2_PRESENCE";
   mac="00:11:22:33:44:55";
};
}

## Assistant variables

:local searchMac
:local ctime [/system clock get time]
:local forceupdate false
:global PRESENCE

# Set the global variable if not exsist

if ( [:typeof $PRESENCE]!="array" ) do={

   :set $PRESENCE ({})

}

# Init last update time

if ( [:len ($PRESENCE->"lastupdate")]=0 || ($PRESENCE->"lastupdate")>$ctime ) do={

   :set ($PRESENCE->"lastupdate") 00:00:00

}

## Logic

:set $forceupdate ((($PRESENCE->"lastupdate")+$Telemetry)<$ctime)

if ( $forceupdate ) do={

    :set ($PRESENCE->"lastupdate") $ctime

}

:foreach item in=$items do={

   :set searchMac [/ip dhcp-server lease find mac-address=($item->"mac") ]
  
   if ( ([:len $searchMac]>0) != ($PRESENCE->"$($item->"pres")") || $forceupdate ) do={
   
      :set ($PRESENCE->"$($item->"pres")") ([:len $searchMac]>0)

      if ($PRESENCE->"$($item->"pres")") do={

         /tool fetch mode=http url=$DomoticzURL http-data="type=command&param=updateuservariable&vname=$($item->"pres")&vtype=INTEGER&vvalue=1" output=none

      } else={

        /tool fetch mode=http url=$DomoticzURL http-data="type=command&param=updateuservariable&vname=$($item->"pres")&vtype=INTEGER&vvalue=0" output=none

      }
   }
}

Assign the script to the DHCP server (in our example: LAN)

/ip dhcp-server add address-pool=LAN bootp-support=none disabled=no interface=LAN lease-script="/system script run Domoticz_Presence" name=LAN

Schedule the script

/system scheduler add interval=10m name=Domoticz_Presence on-event="/system script run Domoticz_Presence" policy=read,policy,test start-date=jan/01/2022 start-time=00:00:00

Domoticz

After you create the user variables and the device, like at prerequisites, in the Domoticz, you can create the below Lua script at Domoticz events. The script trigger will be "User Variable".

Change the variables' and the devices' name according to your enviorment as descripted by script comment.

--[[

Domoticz presence handle Lua script

@author moszat <moszat@onlinesoft.org>
@copyright 2022 moszat
@license GPL 3
@version 2.0

See the https://doc.onlinesoft.org/index.php?title=Domoticz_-_MikroTik_presence_management

--]]

commandArray = {}

-- Parameters

-- PRESENCEVAR	The variable in what the current state of the house be stored (integer, 1 = presence, 0 = absence)

local PRESENCEVAR = 'PRESENCE'

-- Users' data
--
-- USERS[NAME]         User's name (free string)
-- USERS[TRIGGERVAR]   The variable what the external system is triggered. (integer, 1 = presence, 0 = absence)
-- USERS[STOREVAR]     The variable in what the previous state be stored (integer)

local USERS = {
    { 
        ["NAME"]        = "John Doe", 
        ["TRIGGERVAR"]  = "USER1_PRESENCE", 
        ["STOREVAR"]    = "USER1_PRESENCE_PREV" 
    },
    {
        ["NAME"]        = "Jane Doe", 
        ["TRIGGERVAR"]  = "USER2_PRESENCE", 
        ["STOREVAR"]    = "USER2_PRESENCE_PREV" 
    }
}

-- Light control
--
-- SWITCHESOFF  Switches what switch off when there is no presence (device names)
-- SWITCHESON   Switches what switch on when an arriving is occurred (device names)

local SWITCHESOFF   = { 'Bedroom switch', 'Livingroom switch', 'Childrenroom switch', 'Larder switch', 'Bathroom switch', 'Garden switch'}
local SWITCHESON    = { 'Garden switch' }

-- Heating contol
--
-- PRESENCEDEVICE           A dummy selector device for set to presence mode
-- ABSENCEDEVICE            A dummy thermostat setpoint device for set to absence temperature
-- THERMOSTATS[DEVICENAME]  Thermostat setpoint device name
-- THERMOSTATS[STOREVAR]    The variable in what the normal degree be stored in absence mode

local PRESENCEDEVICE = 'Presence'
local ABSENCEDEVICE = 'Thermostat Absence'
local THERMOSTATS = {
    
    {
      ['DEVICENAME']        = 'Kitchen thermostat - Setpoint',
      ['STOREVAR']          = 'TEMP_KITHCEN',
    },
    {
      ['DEVICENAME']        = 'Livingroom thermostat - Setpoint',
      ['STOREVAR']          = 'TEMP_LIVINGROOM',
    },
    {
      ['DEVICENAME']        = 'Bedroom thermostat - Setpoint',
      ['STOREVAR']          = 'TEMP_BEDROOM',
    },
    {
      ['DEVICENAME']        = 'Childrenroom thermostat - Setpoint',
      ['STOREVAR']          = 'TEMP_CHILDRENROOM',
    }, 
    
}

-- Assistant variables

local ARRIVING = false
local PRESENCE = false
local STOREDPRESENCE = false

-- Logic

if ( uservariables[PRESENCEVAR] == nil ) then
		
	print( PRESENCEVAR .. ' value does not exist' )
	return commandArray
		
end

if ( otherdevices[PRESENCEDEVICE] == nil ) then
		
	print( PRESENCEDEVICE .. ' device does not exist' )
	return commandArray
		
end

if ( otherdevices[PRESENCEDEVICE] == 'Off' ) then
		
	print( 'Presence handle is off. Exiting..' )
	return commandArray
		
end

if ( tonumber(uservariables[PRESENCEVAR]) == 0) then
    
    STOREDPRESENCE = false
    
else
    
    STOREDPRESENCE = true
    
end

commandArray['SetSetPoint:' .. otherdevices_idx[ABSENCEDEVICE]] = tostring(otherdevices[ABSENCEDEVICE])

-- Users' presence changed

if ( otherdevices[PRESENCEDEVICE] == 'Auto' ) then 

    for key, value in pairs (USERS) do
        
    	if ( uservariables[value['TRIGGERVAR']] == nil ) then
    		
    		print( value['TRIGGERVAR'] .. ' value does not exist' )
    	
    	elseif ( uservariables[value['STOREVAR']] == nil ) then
    		
    		print( value['STOREVAR'] .. ' value does not exist' )
    		
        elseif ( uservariables[value['TRIGGERVAR']] ~=   uservariables[value['STOREVAR']] ) then 
        
            if ( tonumber(uservariables[value['TRIGGERVAR']]) == 1 ) then
            
                print ( value['NAME'] .. ' is arriving ... ')
                ARRIVING = true
            
            else
            
                print ( value['NAME'] .. ' is leaving ... ')
            
            end
        
            commandArray['Variable:' .. value['STOREVAR'] ] = tostring(uservariables[value['TRIGGERVAR']])
    
        end
    
        if ( tonumber(uservariables[value['TRIGGERVAR']]) == 1 ) then
    
            PRESENCE = true
        
        end
    
    end

elseif ( otherdevices[PRESENCEDEVICE] == 'Presence' ) then

    PRESENCE = true
    print( 'Forced presence is on.' )

elseif ( otherdevices[PRESENCEDEVICE] == 'Absence' ) then

    PRESENCE = false
    print( 'Forced absence is on.' )

end

-- Arriving occurred

if ( ARRIVING == true ) then

    -- LIGHT set on

    if ( timeofday['Nighttime'] ) then

        for key, value in pairs (SWITCHESON) do
    
    		if ( otherdevices[value] == nil ) then
    			
    			print( otherdevices[value] .. ' device does not exist' )
    			
            elseif ( otherdevices[value] == 'Off') then 
    
    			commandArray[value] = 'On'
    			print ( value .. ' set on')
    
    		end
            
        end
        
    end
    
end

-- Presence / Absence handle

if ( PRESENCE ~= STOREDPRESENCE ) then
    
    
    if ( PRESENCE ) then
        
        -- Set Presence
        
        -- Writes back the new state to PRESENCEVAR user variable
        
        commandArray['Variable:' .. PRESENCEVAR ] = '1'
        print ('Presence set on')
        
        -- HEATING set presence mode

        for key, value in pairs (THERMOSTATS) do
			
			if ( uservariables[value['STOREVAR']] == nil ) then
				
				print( value['STOREVAR'] .. ' value does not exist' )
				
			elseif ( otherdevices[value['DEVICENAME']] == nil ) then
			
				print( value['DEVICENAME'] .. ' device does not exist' )
				
			else
            
            	commandArray['SetSetPoint:' .. otherdevices_idx[value['DEVICENAME']]] = tostring(uservariables[value['STOREVAR']])
            	print ( value['DEVICENAME'] .. ' set ' .. uservariables[value['STOREVAR']])
				
			end
             
        end
        
    else
        
        -- Set Absence
        
        -- Writes back the new state to PRESENCEVAR user variable
        
        commandArray['Variable:' .. PRESENCEVAR ] = '0'
	    print ('Presence set off')
	    
	    -- LIGHTIG set absence mode

        for key, value in pairs (SWITCHESOFF) do
    
    		if (  otherdevices[value] == nil ) then
    			
    			print( otherdevices[value] .. ' device does not exist' )
    		
            elseif ( otherdevices[value] == 'On') then 
    			
                commandArray[value] = 'Off'
                print ( value .. ' set off')
    			
            end
            
        end
        
        -- HEATING set absence mode
	
    	if ( otherdevices[ABSENCEDEVICE] == nil ) then
    		
    		print( ABSENCEDEVICE .. ' device does not exist' )
    		
    	else
    
    		for key, value in pairs (THERMOSTATS) do
    
    			if ( uservariables[value['STOREVAR']] == nil ) then
    
    				print( value['STOREVAR'] .. ' value does not exist' )
    
    			elseif ( otherdevices[value['DEVICENAME']] == nil ) then
    
    				print( value['DEVICENAME'] .. ' device does not exist' )
    
    			else
    
    				commandArray['Variable:' .. value['STOREVAR']] = otherdevices[value['DEVICENAME']]
    				commandArray['SetSetPoint:' .. otherdevices_idx[value['DEVICENAME']]] = tostring(otherdevices[ABSENCEDEVICE])
    				print ( value['DEVICENAME'] .. ' set ' .. otherdevices[ABSENCEDEVICE] ) 
    
    			end
    
		    end
		
    	end

    end

end

return commandArray