Well...
Code: Select all
return {
on = {
timer = {'every 1 minutes', }, -- at daytime
},
logging = {
level = domoticz.LOG_DEBUG,
marker = 'Car charging on PV Power'
},
data = {
desiredLevel = { initial = 'Off' },
lastChangeTime = { initial = 0 },
},
execute = function(domoticz, triggeredItem)
if triggeredItem then
domoticz.log('#11111 triggeeredItem ', domoticz.LOG_DEBUG)
end
-- domoticz.devices('Charging Level').dump()
local ChargingSwitchState = domoticz.devices('Charging Level').levelName
local EVSwitch_16A = domoticz.devices('EVSE Switch 16A')
local chargeLevelSet = domoticz.devices('XC40 battery charge set').level
local chargeLevelActual = domoticz.devices('XC40-ChargeLevel').nValue -- is een int en niet iets met een komma XC40-ChargeLevel
local EVSE_Connected = domoticz.globalData.EVSE_Connected
local newLevel = 'Off'
-- Define power levels
local powerLevels = {
Off = 0,
['6A'] = 1400,
['8A'] = 1850,
['10A'] = 2300,
['13A'] = 3000,
['16A'] = 3700
}
-- Get power level based on charging switch state
local PowerLaadpaal = powerLevels[ChargingSwitchState] or 0
-- Get power data
local PowerReturn = tonumber(domoticz.devices('Power').rawData[6]) -- Solar power generation
local PowerConsumption = tonumber(domoticz.devices('Power').rawData[5]) -- Home power consumption
local availableEnergy = PowerLaadpaal + PowerReturn - PowerConsumption
-- Logging
--domoticz.log('#0 EVSE connected: ' .. domoticz.globalData.EVSE_Connected, domoticz.LOG_DEBUG)
domoticz.log('#1 Energy usage from the network: ' .. PowerConsumption, domoticz.LOG_DEBUG)
domoticz.log('#2 Energy return to the network: ' .. PowerReturn, domoticz.LOG_DEBUG)
domoticz.log('#3 Actual power on laadpaal: ' .. PowerLaadpaal, domoticz.LOG_DEBUG)
domoticz.log('#4 Available energy: ' .. availableEnergy, domoticz.LOG_DEBUG)
-- Check if SmartEVSE is OK
if domoticz.globalData.EVSE_CommunicationError ~= 'None' then
domoticz.notify('@1 There is an EVSE Communication Error', domoticz.PRIORITY_NORMAL)
domoticz.devices('Auto laden').switchOff().forSec(30)
end
-- Determine the desired charging level
local newLevel = 'Off'
local currentLevel = domoticz.devices('Charging Level').state
domoticz.log('###### currentLevel : ' .. currentLevel, domoticz.LOG_DEBUG)
for level, power in pairs(powerLevels) do
if availableEnergy >= power then
newLevel = level
end
end
domoticz.log('###### newLevel : ' .. newLevel, domoticz.LOG_DEBUG)
-- Check if chargeLevelActual >= chargeLevelSet
if chargeLevelActual >= chargeLevelSet and EVSE_Connected == 'Connected' then
domoticz.devices('Charging Level').switchSelector('Off')
domoticz.log('#5 Set to: ' .. newLevel, domoticz.LOG_DEBUG)
-- Check if chargeLevelActual < chargeLevelSet
elseif chargeLevelActual < chargeLevelSet and EVSE_Connected == 'Connected' and EVSwitch_16A.state == 'Off' then
local currentTime = os.time()
if availableEnergy < 900 then -- 1400 - 500 = 900
newLevel = 'Off'
end
domoticz.devices('Charging Level').switchSelector(newLevel)
domoticz.log('#5 Set to: ' .. newLevel, domoticz.LOG_DEBUG)
-- Notify charging state
-- It sends the notification one time when the level has changed
local current_time = os.date('%d-%m-%Y %H:%M')
local subject, message
if newLevel == 'Off' and (currentLevel ~= newLevel) then
subject = '@2 Charging on PV power has ended.'
message = 'Charging Off, Energy : ' .. availableEnergy .. '\nTime : ' .. current_time
if domoticz.LOG_DEBUG then
domoticz.notify(subject, message, domoticz.PRIORITY_NORMAL)
domoticz.log('#6 Notification sent: ' .. subject, domoticz.LOG_DEBUG)
end
elseif newLevel ~= 'Off'and (currentLevel ~= newLevel) then
subject = '@3 We are charging on PV power.'
message = 'Charging ' .. newLevel .. ', Energy : ' .. availableEnergy .. '\nTime : ' .. current_time
if domoticz.LOG_DEBUG then
domoticz.notify(subject, message, domoticz.PRIORITY_NORMAL)
domoticz.log('#7 Notification sent: ' .. subject, domoticz.LOG_DEBUG)
end
end
domoticz.log('#7 Car charging on PV Power END of test loop', domoticz.LOG_DEBUG)
end
end
}
Bugs bug me.