SmartEVSE script for car charging
Posted: Saturday 26 October 2024 0:34
Hi,
I wrote a script for car charging using the SmartEVSE device.
Our EVBox is > 10 years old and I decided to make it 'smart'.
Michael Stegen gave me some clues how to open and change the EVBox and put the SmartEVSE device in place and it worked right from the start.
The script I wrote functioned like it shoud but I wanted to have it rewritten.
So I asked Microsoft CoPilot to do so.
The result is a script that goes beyond my programming skills, like the use of powerLevels in a table and the in pairs construct.
CoPilot added a time 'buffer' as well to hev the script not change the carging level for every single cloud passing by.
Here's the code:
Switching is done using a selector switch and the 'EVSE Switch 16A' is used to be able to override PV power charging.
There is more to come.
I'll show you how to switch the SmartEVSE using mosquitto, how to set the charging level for the car using the Python Plugin: Volvo EV and a script that shows how much charging time and percentage is used for each charhing level.
Before publishing the scripts I'll consult CoPilot to enhance the scripts I wrote.
I wrote a script for car charging using the SmartEVSE device.
Our EVBox is > 10 years old and I decided to make it 'smart'.
Michael Stegen gave me some clues how to open and change the EVBox and put the SmartEVSE device in place and it worked right from the start.
The script I wrote functioned like it shoud but I wanted to have it rewritten.
So I asked Microsoft CoPilot to do so.
The result is a script that goes beyond my programming skills, like the use of powerLevels in a table and the in pairs construct.
Code: Select all
for level, power in pairs(powerLevels) do
if availableEnergy >= power then
newLevel = level
end
Here's the code:
Code: Select all
return {
on = {
timer = {'every 1 minutes at daytime'}, -- at daytime
},
logging = {
level = domoticz.LOG_ERROR,
marker = 'Car charging on PV Power',
},
data = {
desiredLevel = { initial = 'Off' },
lastChangeTime = { initial = 0 }
},
execute = function(domoticz, triggeredItem)
local ChargingSwitchState = domoticz.devices('Charging Level').levelName
local EVSwitch_16A = domoticz.devices('EVSE Switch 16A')
-- 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('#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)
if domoticz.globalData.EVSE_CommunicationError ~= 'None' then
domoticz.notify('There is an EVSE Communication Error', domoticz.PRIORITY_NORMAL)
end
-- Determine the desired charging level
local newLevel = 'Off'
for level, power in pairs(powerLevels) do
if availableEnergy >= power then
newLevel = level
end
end
-- Check if EV is connected and switch is off
if domoticz.globalData.EVSE_Connected == 'Connected' and EVSwitch_16A.state == 'Off' then
local currentTime = os.time()
if newLevel ~= domoticz.data.desiredLevel then
domoticz.data.desiredLevel = newLevel
domoticz.data.lastChangeTime = currentTime
elseif currentTime - domoticz.data.lastChangeTime >= 120 then -- 2 minutes buffer
if availableEnergy < 900 then
newLevel = 'Off'
end
domoticz.devices('Charging Level').switchSelector(newLevel)
domoticz.log('## Set to: ' .. newLevel, domoticz.LOG_DEBUG)
-- Notification for starting or stopping charging
local current_time = os.date('%Y-%m-%d %H:%M:%S')
local subject, message
if newLevel == 'Off' then
subject = 'We stopped charging on PV power!'
message = current_time .. '\nCharging level is set to: Off'
else
subject = 'We are charging on PV power!'
message = current_time .. '\nCharging level is set to: ' .. newLevel
end
domoticz.notify(subject, message, domoticz.PRIORITY_NORMAL)
domoticz.log('#8 Notification sent: ' .. subject, domoticz.LOG_DEBUG)
end
domoticz.log('#10 Car charging on PV Power END of test loop', domoticz.LOG_DEBUG)
end
end
}
There is more to come.
I'll show you how to switch the SmartEVSE using mosquitto, how to set the charging level for the car using the Python Plugin: Volvo EV and a script that shows how much charging time and percentage is used for each charhing level.
Before publishing the scripts I'll consult CoPilot to enhance the scripts I wrote.