Subversion Repositories rld.domoticz

Rev

Blame | Last modification | View Log | Download

-- local device id's, only used in this script
local cDevAnyoneHome = 158 
local cVirtTimer =     186
local cDevSunPower =   173
local cDevTuya =       184

-- local constants
local cMinSinPower = 9999 -- Watt/m2
local cHysSinPower = 9999 -- Watt/m2

-- domoticz user variables
uVarOverride = 'dmLampjesBoom_Override'


return {
    on = {
      devices = {cDevAnyoneHome, cVirtTimer, cDevSunPower, cDevTuya},
    timer = {},
    variables = {},
    scenes = {},
    groups = {},
    security = {},
    httpResponses = {},
    customEvents = {},
    system = {}
    },
    logging = {
    --level = domoticz.LOG_DEBUG + domoticz.LOG_INFO,
    level = domoticz.LOG_ERROR,
        marker = "Tuya-Lampjes"
    },
    execute = function(domoticz, triggeredItem)

      -- domoticz.log('event file Tuya-152')
       
      -- check if device is turned on manual, if so just set Override
      if (triggeredItem.isDevice and triggeredItem.id == cDevTuya) then
          --commandArray['Variable:dmTuya152_Override']= tostring(((domoticz.devices(cDevTuya).active) and 1 or 0))
          domoticz.log('trigger is Tuya device: ' .. tostring(((domoticz.devices(cDevTuya).active) and 1 or 0)))
          --commandArray['Variable:dmTuya152_Override']= tostring(123)
          domoticz.variables(uVarOverride).set(((domoticz.devices(cDevTuya).active) and 1 or 0))
      end
      
      -- remove override if virtual timer changes
      if (triggeredItem.isDevice and triggeredItem.id == cVirtTimer) then
      
          --commandArray['Variable:dmTuya152_Override']= tostring(((domoticz.devices(cDevTuya).active) and 1 or 0))
          domoticz.log('trigger is virtual Tuya timer: ' .. tostring(((domoticz.devices(cVirtTimer).active) and 1 or 0)))
          domoticz.variables(uVarOverride).set(0) -- turn override off
      end
      
      local lOverRide = (domoticz.variables(uVarOverride).value ~= 0)
      local lTimerOn = domoticz.devices(cVirtTimer).active
      local lSunPowerOff = (tonumber(domoticz.devices(cDevSunPower).state) > (cMinSinPower+cHysSinPower)) and true or false --var = false and 20 or 30 return 30
      local lSunPowerOn = (tonumber(domoticz.devices(cDevSunPower).state) < cMinSinPower) and true or false --var = false and 20 or 30 return 30
      local lAnyoneHome = domoticz.devices(cDevAnyoneHome).active
      local lDeviceOn = domoticz.devices(cDevTuya).active
      domoticz.log('Sun power: ' .. tostring(domoticz.devices(cDevSunPower).state))
      domoticz.log('lOverRide, lTimerOn, lAnyoneHome, lSunPowerOn, lSunPowerOff: ' .. tostring(lOverRide) .. ', ' .. tostring(lTimerOn) .. ', ' .. tostring(lAnyoneHome) .. ', ' .. tostring(lSunPowerOn) .. ', ' .. tostring(lSunPowerOff))
      
      -- only change state of device if there is no override
      if not lOverRide then
          
        -- turn device on, if it is off
        if(lTimerOn and lSunPowerOn and lAnyoneHome) then
          
          -- turn device on
          domoticz.devices(cDevTuya).switchOn().silent()
        end
          
        -- turn device off if there is no override
        if(not lTimerOn or lSunPowerOff or not lAnyoneHome) then
            
          domoticz.devices(cDevTuya).switchOff().silent()
        end
      end
  end
}