Electric car battery charging automation

Easy to use, 100% Lua-based event scripting framework.

Moderator: leecollings

Post Reply
HvdW
Posts: 615
Joined: Sunday 01 November 2015 22:45
Target OS: Raspberry Pi / ODroid
Domoticz version: 2023.2
Location: Twente
Contact:

Electric car battery charging automation

Post by HvdW »

I'm using the Volvo python plugin created by @akamming
This opens new oportunities.

Let me explain.
The car allows to set a maximum battery charging level.
Problem is that it can only be set from inside the car.

This script allows to set the charging level in the car at 78% from within Domoticz and let Domoticz limit the max charging level.

Code: Select all

-- Volvo XC40 charging limiter

local MINIMUM_BATTERY_LEVEL = 20
local CHARGING_SPEED        = 3.5       -- kWh
local BATTERY_CAPACITY      = 80        -- Kw

return {
	on = {
		timer = {'every 12 minutes'},
	},
	data = {},
	    logging = {
        level = domoticz.LOG_ERROR,
        -- change LOG_DEBUG to LOG_ERROR to stop logging in the log
        marker = 'XC40 charging limiter',
    },
	execute = function(domoticz, triggeredItem)
	    -- domoticz.log(domoticz.devices('XC40 battery %').dump(),domoticz.LOG_DEBUG)
	    -- domoticz.log(domoticz.devices('XC40-ChargeLevel').dump(),domoticz.LOG_DEBUG)
	    local current_time =  os.date("%c")
	    local MINIMUM_BATTERY_LEVEL = domoticz.devices('XC40 battery %').levelVal
	    local chargeLevel = domoticz.devices('XC40-ChargeLevel').percentage
	    local range = domoticz.devices('XC40-fullRange').state
        domoticz.log('Volvo calculated full range is       : ' .. range, domoticz.LOG_DEBUG)
        domoticz.log('Volvo current battery level is        : ' .. chargeLevel, domoticz.LOG_DEBUG)
        if chargeLevel >= MINIMUM_BATTERY_LEVEL then
            domoticz.devices('Auto laden').switchOff()
            local subject = 'The Volvo battery has been charged. '
                    local message = 
                    ' ' .. current_time .. '\n'.. 
                    'Volvo current battery level is        : ' .. chargeLevel .. '\n' 
                    domoticz.notify(subject, message, domoticz.PRIORITY_NORMAL)
        end 
        if chargeLevel < MINIMUM_BATTERY_LEVEL then
            domoticz.devices('Auto laden').switchOn()
            local subject = 'The Volvo battery has started charging. '
                    local message = 
                    ' ' .. current_time .. '\n'.. 
                    'Volvo current battery level is        : ' .. chargeLevel .. '\n' 
                    domoticz.notify(subject, message, domoticz.PRIORITY_NORMAL)
        end 
        
    end
}
It's a realy bare-bone script.

- First addition will be a dimmer switch to set the charging level on the Domoticz Switch page. ✔️


If BMW, Dacia, Hyundai, Kia, Renault, Tesla,Toyota, Volvo drivers and others have code to share, please do.
Last edited by HvdW on Sunday 21 January 2024 23:50, edited 4 times in total.
Bugs bug me.
willemd
Posts: 650
Joined: Saturday 21 September 2019 17:55
Target OS: Raspberry Pi / ODroid
Domoticz version: 2024.1
Location: The Netherlands
Contact:

Re: Volvo battery charging automation

Post by willemd »

For KIA this can be set using the KIA Connect app provided by KIA.
I am also using the KIA plugin for Domoticz, but am only using that to read values, not to write values.
Here is the code for the KIA domoticz plugin https://github.com/CreasolTech/domoticz-hyundai-kia
and here is the code for the communication layer to KIA cloud https://github.com/Hyundai-Kia-Connect/ ... onnect_api
Not so simple ....
HvdW
Posts: 615
Joined: Sunday 01 November 2015 22:45
Target OS: Raspberry Pi / ODroid
Domoticz version: 2023.2
Location: Twente
Contact:

Re: Electric car battery charging automation

Post by HvdW »

I'm satisfied with the script for now.
- There's a check whether the car is connected to the charging station @home
- When battery too low the car will be charged automatically, after having checked that the car is plugged in.
- When charging has started manually the battery will be charged to the limit that has been preset.
- Module to be added is for automatic charging when there's a surplus of PVpanels power

Code: Select all

-- Volvo XC40 charging limiter
-- Useable for all types of electric cars
-- Auto laden is Dutch for car charging

return {
	on = {
		devices = {'Volvo-aangesloten'},
	},
	data = {},
	    logging = {
        level = domoticz.LOG_ERROR,
        -- change LOG_DEBUG to LOG_ERROR to stop logging in the log
        marker = 'XC40 charging limiter',
    },
	execute = function(domoticz, triggeredItem)
	    -- domoticz.log(domoticz.devices('XC40 battery charge set').dump(),domoticz.LOG_DEBUG)
	    -- domoticz.log(domoticz.devices('XC40-ChargeLevel').dump(),domoticz.LOG_DEBUG)
	    -- domoticz.log(domoticz.devices('Volvo-aangesloten').dump(),domoticz.LOG_DEBUG)
	    -- domoticz.log(domoticz.devices('XC40-ChargeLevel'),domoticz.LOG_DEBUG)
	    local current_time =  os.date("%c")
	    local MAXIMUM_BATTERY_LEVEL = domoticz.devices('XC40 battery charge set').levelVal
	    local chargeLevel = domoticz.devices('XC40-ChargeLevel').percentage
	    local range = domoticz.devices('XC40-fullRange').state
	    local isConnected = domoticz.devices('Volvo-aangesloten').state
        domoticz.log('Volvo calculated full range is       : ' .. range, domoticz.LOG_DEBUG)
        domoticz.log('Volvo current battery level is        : ' .. chargeLevel, domoticz.LOG_DEBUG)
        domoticz.log(domoticz.devices('Volvo-aangesloten').state,domoticz.LOG_DEBUG)
        domoticz.log('Is the charger cable plugged in the car ?   :'.. isConnected ,domoticz.LOG_DEBUG)
        
        if (chargeLevel > MAXIMUM_BATTERY_LEVEL and isConnected ~= 'Disconnected') then
            domoticz.devices('Auto laden').switchOff()
            local subject = 'The Volvo battery has been charged. '
                    local message = 
                    ' ' .. current_time .. '\n'.. 
                    'Volvo current battery level is        : ' .. chargeLevel .. '\n' 
                    domoticz.notify(subject, message, domoticz.PRIORITY_NORMAL)
        end 
        if (chargeLevel <= MINIMUM_BATTERY_LEVEL and isConnected ~= 'Disconnected') then -- nvalue != 0
            domoticz.devices('XC40 battery charge set').setLevel(82)  -- 82 to minimize charging cycles
            domoticz.devices('Auto laden').switchOn()
            domoticz.log('Indeed, the car is plugged in !   :'.. isConnected ,domoticz.LOG_DEBUG)
            local subject = 'The Volvo battery charging has started. '
                    local message = 
                    ' ' .. current_time .. '\n'.. 
                    'Volvo current battery level is        : ' .. chargeLevel .. '\n' 
                    domoticz.notify(subject, message, domoticz.PRIORITY_NORMAL)
        end 
    end
}
Bugs bug me.
HvdW
Posts: 615
Joined: Sunday 01 November 2015 22:45
Target OS: Raspberry Pi / ODroid
Domoticz version: 2023.2
Location: Twente
Contact:

Car battery charging with solar power and/or @night

Post by HvdW »

I added another script to switch battery charging on when there's sufficient solar power.

Code: Select all

-- Volvo XC40 solar power charging
-- Auto laden is Dutch for car charging
-- Volvo aangesloten is Dutch for Volvo is plugged in

-- er moet nog iets komen om met de hand inschakelen niet uit te schakelen

local CHARGING_SPEED        = 3.5       -- kWh  not relevant for this script

return {
	on = {
		timer = {'every 6 minutes at daytime'},
		devices = {'Volvo-aangesloten'},
	},
	data = {
	    ChargeOnSunPower  = {initial = 0}
	},
	    logging = {
        level = domoticz.LOG_DEBUG,
        -- change LOG_DEBUG to LOG_ERROR to stop logging in the log
        marker = 'XC40 charging on sun power',
    },
	execute = function(domoticz, triggeredItem)
	    local current_time =  os.date("%c")
	    local chargeLevel = domoticz.devices('XC40-ChargeLevel').percentage
	    local DistanceFromHome = domoticz.devices('Volvo-Distance2Home').sensorValue
	    local isConnected = domoticz.devices('Volvo-aangesloten').state
	    local SunnyPower = tonumber(domoticz.devices('Power').rawData[6]) 	   -- opbrengst zonnepanelen
	    local PowerConsumption = tonumber(domoticz.devices('Power').rawData[5])  -- power consumption @home
	    local VolvoOpladen = domoticz.devices('Volvo-Opladen').state
        
        -- DistanceFromHome is used to prevent switching when the car is connected to a charger elsewhere
        if DistanceFromHome < 0.35 then
            if (SunnyPower >= 2600 and isConnected ~= 'Disconnected' and domoticz.data.ChargeOnSunPower == 0 and domoticz.devices('Auto laden').state == 'Off' ) then -- nvalue != 0

                domoticz.devices('Auto laden').switchOn()
                domoticz.data.ChargeOnSunPower = 1
            end
            if (PowerConsumption >= 1000 and domoticz.data.ChargeOnSunPower == 1) then -- nvalue != 0
                domoticz.devices('Auto laden').switchOff()
                domoticz.data.ChargeOnSunPower = 0
            end  
        end
    end
}
Adapted the charging limiter code to make the car battery being charged at nigt time.

Code: Select all

-- Volvo XC40 charging limiter
-- Auto laden is Dutch for car charging
-- Volvo aangesloted is Dutch for Volvo is plugged in
-- measuring excess power to decide to charge the car

local MINIMUM_BATTERY_LEVEL = 20        -- trigger level to force-start charging
local CHARGING_SPEED        = 3.5       -- kWh  not relevant for this script
local BATTERY_CAPACITY      = 80        -- Kw   not relevant for this script

return {
	on = {
		timer = {'every 15 minutes'},
		devices = {'Volvo-aangesloten'},
	},
	data = {},
	    logging = {
        level = domoticz.LOG_ERROR,
        -- change LOG_DEBUG to LOG_ERROR to stop logging in the log
        marker = 'XC40 charging limiter',
    },
	execute = function(domoticz, triggeredItem)
	    -- domoticz.log(domoticz.devices('XC40 battery charge set').dump(),domoticz.LOG_DEBUG)
	    -- domoticz.log(domoticz.devices('XC40-ChargeLevel').dump(),domoticz.LOG_DEBUG)
	    -- domoticz.log(domoticz.devices('Volvo-aangesloten').dump(),domoticz.LOG_DEBUG)
	    -- domoticz.log(domoticz.devices('Volvo-Distance2Home').dump,domoticz.LOG_DEBUG)
	    -- domoticz.devices('Volvo-Distance2Home').dump()
	    -- domoticz.devices('Power').dump()
	    -- domoticz.devices('Auto laden').dump()
	    local current_time =  os.date("%c")
	    local MAXIMUM_BATTERY_LEVEL = domoticz.devices('XC40 battery charge set').levelVal
	    local chargeLevel = domoticz.devices('XC40-ChargeLevel').percentage
	    local range = domoticz.devices('XC40-fullRange').state
	    local homedistance = domoticz.devices('Volvo-Distance2Home').sensorValue
	    local isConnected = domoticz.devices('Volvo-aangesloten').state
	    local SunnyPower = domoticz.devices('Power').rawData[5]
        domoticz.log('Volvo calculated full range is  : ' .. range, domoticz.LOG_DEBUG)
        domoticz.log('Volvo current battery level is  : ' .. chargeLevel, domoticz.LOG_DEBUG)
        domoticz.log('Volvo charging cable connected  :' .. domoticz.devices('Volvo-aangesloten').state,domoticz.LOG_DEBUG)
        domoticz.log('Volvo distance from home is     :'.. homedistance ,domoticz.LOG_DEBUG)
        
        if domoticz.devices('Volvo-Distance2Home').sensorValue < 0.35 then
            if (chargeLevel > MAXIMUM_BATTERY_LEVEL and isConnected ~= 'Disconnected') then
                domoticz.devices('Auto laden').switchOff()
                local subject = 'The Volvo battery has been charged. '
                    local message = 
                    ' ' .. current_time .. '\n'.. 
                    'Volvo current battery level is        : ' .. chargeLevel .. '\n' 
                    domoticz.notify(subject, message, domoticz.PRIORITY_NORMAL)
            end 
            if (chargeLevel <= MINIMUM_BATTERY_LEVEL and isConnected ~= 'Disconnected') then -- nvalue != 0
                domoticz.devices('XC40 battery charge set').setLevel(82)  -- 82 to minimize charging cycles
                -- domoticz.devices('Auto laden').switchOn()
                -- we laden op nachtstroom
                domoticz.devices('Auto laden').switchOn().at('23:29')
                domoticz.devices('Auto laden').switchOff().at('06:29')
                domoticz.log('Indeed, the car is plugged in !   :'.. isConnected ,domoticz.LOG_DEBUG)
                local subject = 'The Volvo battery charging has started. '
                    local message = 
                    ' ' .. current_time .. '\n'.. 
                    'Volvo current battery level is        : ' .. chargeLevel .. '\n' 
                    domoticz.notify(subject, message, domoticz.PRIORITY_NORMAL)
            end
        end
    end
}
Bugs bug me.
HvdW
Posts: 615
Joined: Sunday 01 November 2015 22:45
Target OS: Raspberry Pi / ODroid
Domoticz version: 2023.2
Location: Twente
Contact:

Re: Electric car battery charging automation

Post by HvdW »

I tweaked the code a bit.

Code: Select all

-- Volvo XC40 charging limiter
-- Auto laden is Dutch for car charging
-- 'Volvo aangesloted' is Dutch for Volvo is plugged in

-- in principle charging is done @night up to the 'XC40 battery charge set' level.
-- the car must be ready for take off  @all times.
-- manual charging can always be done together with setting a desired level.

local STATIC_MAXIMUM_BATTERY_LEVEL      = 96        -- 96% @home always switch off to protect the battery
local MINIMUM_BATTERY_LEVEL             = 69        -- trigger level to force-start charging

return {
	on = {
		timer = {'every 2 minutes'},
		devices = {'Volvo-aangesloten'},
	},
	data = {
        notified = { initial = {'false'} },
    },
	logging = {
        level = domoticz.LOG_ERROR,
        -- change LOG_DEBUG to LOG_ERROR to stop logging in the log
        marker = 'XC40 charging limiter',
    },
	execute = function(domoticz, triggeredItem)
	    -- domoticz.log(domoticz.devices('Auto laden').dump(),domoticz.LOG_DEBUG)
	    -- domoticz.log(domoticz.devices('XC40 battery charge set').dump(),domoticz.LOG_DEBUG)
	    -- domoticz.log(domoticz.devices('XC40-ChargeLevel').dump(),domoticz.LOG_DEBUG)
	    -- domoticz.log(domoticz.devices('Volvo-aangesloten').dump(),domoticz.LOG_DEBUG)
	    -- domoticz.log(domoticz.devices('Volvo-Distance2Home').dump,domoticz.LOG_DEBUG)
	    -- domoticz.devices('Volvo-Distance2Home').dump()
	    -- domoticz.devices('Power').dump()
	    -- domoticz.devices('Auto laden').dump()
	    local current_time = os.date("%c")
	    local current_hour = os.date("%H:%M")
	    local HOME_BATTERY_SETTING = domoticz.devices('XC40 battery charge set').level
	    local chargeLevel = domoticz.devices('XC40-ChargeLevel').nValue
	    local range = domoticz.devices('XC40-fullRange').state
	    local homedistance = domoticz.devices('Volvo-Distance2Home').sensorValue
	    local isConnected = domoticz.devices('Volvo-aangesloten').state
	    local SunnyPower = domoticz.devices('Power').rawData[6] -- 5 == gebruik, 6 == teruglevering
        domoticz.log(' maximum charging level is : ' .. HOME_BATTERY_SETTING,domoticz.LOG_DEBUG)
        domoticz.log(' calculated full range is  : ' .. range, domoticz.LOG_DEBUG)
        domoticz.log(' current battery level is  : ' .. chargeLevel, domoticz.LOG_DEBUG)
        domoticz.log(' charging cable connected  :' .. domoticz.devices('Volvo-aangesloten').state,domoticz.LOG_DEBUG)
        domoticz.log(' distance from home is     :'.. homedistance ,domoticz.LOG_DEBUG)
        domoticz.log(' zonne energie overschot     :'.. SunnyPower ,domoticz.LOG_DEBUG)
        domoticz.log(' Domoticz data notified situation     :'.. domoticz.data.notified ,domoticz.LOG_DEBUG)
        
        if domoticz.devices('Volvo-Distance2Home').sensorValue < 0.35 and isConnected ~= 'Disconnected' then
            
            if domoticz.devices('Auto laden').state == 'On' then
              domoticz.data.notified = 'false'  
            end
            
            if chargeLevel >= HOME_BATTERY_SETTING then
                domoticz.devices('Auto laden').switchOff()
                if domoticz.data.notified == 'false' then
                    local subject = '#1 Volvo battery has been switched off. '
                    local message = 
                    ' ' .. current_time .. '\n'.. 
                    'Volvo battery level setting        : ' .. HOME_BATTERY_SETTING .. '\n' ..
                    'Volvo battery level state        : ' .. chargeLevel .. '\n' 
                    domoticz.notify(subject, message, domoticz.PRIORITY_NORMAL)
                    domoticz.data.notified = 'true'
                end
            end 

            if (domoticz.devices('Auto laden').state == 'Off' and current_hour == '23:00' and chargeLevel <= MINIMUM_BATTERY_LEVEL) then -- nvalue != 0
                domoticz.data.notified = 'false'
                domoticz.devices('XC40 battery charge set').setLevel(HOME_BATTERY_SETTING)  -- 82 to minimize charging cycles
                -- we laden op nachtstroom
                domoticz.devices('Auto laden').switchOn().at('23:02')
                domoticz.devices('Auto laden').switchOff().at('06:58 on mon,tue,wed,thu,fri')
                domoticz.log('Indeed, the car is plugged in !   :'.. isConnected ,domoticz.LOG_DEBUG)
                local subject = '#2 Volvo nightly battery charging has been initiated. '
                    local message = 
                    ' ' .. current_time .. '\n'.. 
                    'Volvo auto laden state is : ' .. domoticz.devices('Auto laden').state ..'\n'..
                    'Volvo current time is     : ' .. current_hour .. '\n'..
                    'Volvo battery level is    : ' .. chargeLevel .. '\n' ..
                    'Trigger battery level is  : ' .. MINIMUM_BATTERY_LEVEL .. '\n' ..
                    'Volvo connected state is  : ' .. isConnected .. '\n'
                    domoticz.notify(subject, message, domoticz.PRIORITY_NORMAL)
                    domoticz.data.notified = 'true'
            end
            
            -- switchoff when preset charging level @home reached
            if (domoticz.devices('Auto laden').state == 'On' and chargeLevel >= HOME_BATTERY_SETTING) then
                domoticz.devices('Auto laden').switchOff()
                if domoticz.data.notified == 'false' then
                    local subject = '#3 Volvo battery charging has been stopped. '
                    local message = 
                    ' ' .. current_time .. '\n'.. 
                    'Volvo current battery level is        : ' .. chargeLevel .. '\n' ..
                    'Maximum dynamic charging level has been set to : ' .. HOME_BATTERY_SETTING .. '\n'..
                    'Maximum static charging level is : ' .. STATIC_MAXIMUM_BATTERY_LEVEL .. '\n'
                    domoticz.notify(subject, message, domoticz.PRIORITY_NORMAL)
                    domoticz.data.notified = 'true'
                end
            end 
            
            if (domoticz.devices('Auto laden').state == 'On' and chargeLevel >= STATIC_MAXIMUM_BATTERY_LEVEL) then
                domoticz.devices('Auto laden').switchOff()
                if domoticz.data.notified == 'false' then
                    local subject = '#4 Volvo battery forced stop. '
                    local message = 
                    ' ' .. current_time .. '\n'..
                    'Maximun admitted charge level has been reached \n'..
                    'Maximum admitted charging level is : ' .. STATIC_MAXIMUM_BATTERY_LEVEL .. '\n'..
                    'Volvo current battery level is        : ' .. chargeLevel .. '\n' ..
                    domoticz.notify(subject, message, domoticz.PRIORITY_NORMAL)
                    domoticz.data.notified = 'true'
                end
            end 
        end
    
    end
}
Bugs bug me.
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest