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

Innen: IT documentation
54. sor: 54. sor:


:local searchMac
:local searchMac
:global PRESENCE
# Set interface in the global variable if not exsist
  :if ( [:typeof $PRESENCE]!="array" ) do={
      :set $PRESENCE ({})
  }


## Logic
## Logic
60. sor: 66. sor:


   :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")") ) 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


  }
   }
   }



A lap 2022. november 5., 16:00-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) 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)
  • 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 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.

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.

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.0

# 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 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
:global PRESENCE

# Set interface in the global variable if not exsist
   :if ( [:typeof $PRESENCE]!="array" ) do={
      :set $PRESENCE ({})
   }

## Logic

:foreach item in=$items do={

   :set searchMac [/ip dhcp-server lease find mac-address=($item->"mac") ]
  
   if ( ([:len $searchMac]>0) != ($PRESENCE->"$($item->"pres")") ) 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 1.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
--
-- NAME         User's name (free string)
-- 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)

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
--
-- ABSENCEDEVICE    A dummy thermostat setpoint device
-- DEVICENAME       Thermostat setpoint device name
-- STOREVAR         The variable in what the normal degree be stored in absence mode

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

-- Logic

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

-- Users' presence changed

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

-- Arriving occurred

if ( ARRIVING == true ) then

-- LIGHT set on

    if ( timeofday['Nighttime'] == true ) 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

    if (tonumber(uservariables[PRESENCEVAR]) == 0) then

-- 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
		
-- PRESENCE set on
		
        commandArray['Variable:' .. PRESENCEVAR ] = '1'
        print ('Presence set on')

    end

end

if (tonumber(uservariables[PRESENCEVAR]) == 1 and PRESENCE == false ) then

-- LIGHT set off

    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

-- PRESENCE set off

	commandArray['Variable:' .. PRESENCEVAR ] = '0'
	print ('Presence set off')

end

return commandArray