For the people who use this plugin and are interested in getting more data about the car there is this script which displays the EV consumtion in a text sensor.
To be published soon <Once a month data are written to a file so that you end up with a file in which average consumtion is calculated plus the average temperature.> (there seems to be some kind of relationship between thes two)
I hope that Volvo will add average speed in the API in the near future because there seems to be some kind of relationship between speed and consumtion as well.
Code: Select all
-- local function
local function spaces(count)
return string.rep(" ", count)
end
return {
on = {
devices = {'Charging Level'},
-- timer = {'at 23:21'}
},
logging = {
level = domoticz.LOG_DEBUG,
marker = "EV Verbruik"
},
data = {
previousTimeStamp = { initial = '' },
previousOdometer = { initial = 0 },
previousBatteryLevel = { initial = 0 },
previousdeltaDistance = { initial = 0 },
previouskWhTotal = { initial = 0 },
previouskWhPartial = { initial = 0 },
previouskWh100km = { initial = 0 },
previousEuro = { initial = 0 },
previousavgTemp = { initial = 0 },
previousselectorLevel = { initial = 0 },
tempArray = { initial = {} }
},
execute = function(domoticz, device)
-- Constants
local BATTERY_CAPACITY = 79 -- kWh
local PRICE_PER_KWH = 0.25 -- euro/kWh
local CSV_FILE_PATH = "/home/pi/car_charging.csv"
-- Lees de huidige waarden
local selectorLevel = domoticz.devices('Charging Level').levelVal
local batteryLevel = domoticz.devices('XC40-ChargeLevel').nValue
local currentOdometer = domoticz.devices('Volvo-Odometer').nValue
local currentkWhTotal = domoticz.devices('Laadpaal').WhTotal / 1000
local temperature = tonumber(domoticz.devices('Buitentemperatuur').sValue)
-- Controleer of alle benodigde waarden aanwezig zijn
if not (selectorLevel and batteryLevel and currentOdometer and currentkWhTotal and temperature) then
domoticz.log('Een of meer benodigde devices ontbreken of hebben geen waarde', domoticz.LOG_ERROR)
return
end
local timestamp = os.date('%d-%m-%Y %H:%M')
-- Lees het CSV bestand
local file = io.open(CSV_FILE_PATH, "r")
if file then
local lastLine
for line in file:lines() do
lastLine = line
end
file:close()
if lastLine then
local fields = {}
for field in string.gmatch(lastLine, "([^;]+)") do
table.insert(fields, field)
end
if #fields >= 9 then
domoticz.data.previousTimestamp = fields[1]
domoticz.data.previousOdometer = tonumber(fields[2]) or 0
domoticz.data.previousBatteryLevel = tonumber(fields[3]) or 0
domoticz.data.previousDeltaDistance = tonumber(fields[4]) or 0
domoticz.data.previouskWhTotal = tonumber(fields[5]) or 0
domoticz.data.previouskWhPartial = tonumber(fields[6]) or 0
domoticz.data.previouskWh100km = tonumber(fields[7]) or 0
domoticz.data.previousEuro = tonumber(fields[8]) or 0
domoticz.data.previousavgTemp = tonumber(fields[9]) or 0
end
end
else
domoticz.log('Kan het bestand niet openen: ' .. CSV_FILE_PATH, domoticz.LOG_ERROR)
end
-- Debug logging
domoticz.log('Selector Level : ' .. selectorLevel, domoticz.LOG_DEBUG)
domoticz.log('batteryLevel : ' .. batteryLevel, domoticz.LOG_DEBUG)
domoticz.log('previousOdometer : ' .. domoticz.data.previousOdometer, domoticz.LOG_DEBUG)
domoticz.log('currentOdometer : ' .. currentOdometer, domoticz.LOG_DEBUG)
domoticz.log('currentkWhTotal : ' .. currentkWhTotal, domoticz.LOG_DEBUG)
domoticz.log('Temperature : ' .. temperature, domoticz.LOG_DEBUG)
domoticz.log('Timestamp : ' .. timestamp, domoticz.LOG_DEBUG)
domoticz.log('Battery Capacity : ' .. BATTERY_CAPACITY, domoticz.LOG_DEBUG)
domoticz.log(' ', domoticz.LOG_DEBUG)
-- Temperatuur berekeningen
table.insert(domoticz.data.tempArray, temperature)
local sumTemp = 0
for _, temp in ipairs(domoticz.data.tempArray) do
sumTemp = sumTemp + temp
end
local avgTemp = sumTemp / #domoticz.data.tempArray
-- Controleer laadstatus verandering
if domoticz.data.previousselectorLevel > 0 and selectorLevel == 0 then
local distance = currentOdometer - domoticz.data.previousOdometer
local kWhPartial = currentkWhTotal - domoticz.data.previouskWhTotal
local euro = kWhPartial * PRICE_PER_KWH
-- Verbeterde EV consumptie berekening
local batteryLevelDifference = (batteryLevel - domoticz.data.previousBatteryLevel)
local batteryLevelDifferencekWh = batteryLevelDifference * (BATTERY_CAPACITY/100)
local evConsumption = 0
--local evConsumption = (kWhPartial - batteryLevelDifferencekWh) * (distance/10)
if distance > 0 then -- Voorkom delen door nul
--evConsumption = (kWhPartial - batteryLevelDifferencekWh) * (100/distance)
evConsumption = ((kWhPartial - batteryLevelDifferencekWh)/distance)*100
end
-- Update display met formatting
local displayText = string.format(
"Datum : %s\n" ..
"Current Odometer : %d\n" ..
"Battery Level : %d\n" ..
"Afstand : %d\n" ..
"Partial kWh : %.2f\n" ..
spaces(15).."Euro : %.2f\n" ..
spaces(15).."Gemiddelde Temp : %.1f\n" ..
spaces(15).."EV Consumption : %.2f kWh/100km\n\n",
timestamp,
currentOdometer,
batteryLevel,
distance,
kWhPartial,
euro,
avgTemp,
evConsumption
)
-- Update het display device
local displayDevice = domoticz.devices('Charging Display')
if displayDevice then
displayDevice.updateText(displayText)
else
domoticz.log('Charging Display device niet gevonden', domoticz.LOG_ERROR)
end
-- Extra debug logging
domoticz.log('*********************************** ', domoticz.LOG_DEBUG)
domoticz.log(' ', domoticz.LOG_DEBUG)
domoticz.log('Selector Level : ' .. selectorLevel, domoticz.LOG_DEBUG)
domoticz.log('previousbatteryLevel : ' .. domoticz.data.previousBatteryLevel, domoticz.LOG_DEBUG)
domoticz.log('batteryLevel : ' .. batteryLevel, domoticz.LOG_DEBUG)
domoticz.log('batteryLevelDifference : ' .. batteryLevelDifference, domoticz.LOG_DEBUG)
domoticz.log('batteryLevelDifferencekWh : ' .. batteryLevelDifferencekWh, domoticz.LOG_DEBUG)
domoticz.log('previouskWhPartial : ' .. domoticz.data.previouskWhPartial, domoticz.LOG_DEBUG)
domoticz.log('kWhPartial : ' .. kWhPartial, domoticz.LOG_DEBUG)
domoticz.log('kWhPartial - batteryLevelDifferencekWh : ' .. kWhPartial - batteryLevelDifferencekWh, domoticz.LOG_DEBUG)
domoticz.log('previousOdometer : ' .. domoticz.data.previousOdometer, domoticz.LOG_DEBUG)
domoticz.log('currentOdometer : ' .. currentOdometer, domoticz.LOG_DEBUG)
domoticz.log('previouskWhTotal : ' .. domoticz.data.previouskWhTotal, domoticz.LOG_DEBUG)
domoticz.log('currentkWhTotal : ' .. currentkWhTotal, domoticz.LOG_DEBUG)
domoticz.log('previouskWhPartial : ' .. domoticz.data.previouskWhPartial, domoticz.LOG_DEBUG)
domoticz.log('kWhPartial : ' .. kWhPartial, domoticz.LOG_DEBUG)
domoticz.log('Afstand : ' .. distance, domoticz.LOG_DEBUG)
domoticz.log('evConsumption : ' .. evConsumption, domoticz.LOG_DEBUG)
domoticz.log('Temperature : ' .. temperature, domoticz.LOG_DEBUG)
domoticz.log('Gemiddelde Temp: ' .. avgTemp, domoticz.LOG_DEBUG)
domoticz.log('Timestamp : ' .. timestamp, domoticz.LOG_DEBUG)
domoticz.log(' ', domoticz.LOG_DEBUG)
end
-- Update selector level voor volgende iteratie
domoticz.data.previousselectorLevel = selectorLevel
end
}
It doesn't work from scratch.
In another post I have explained how to set charging limits using this plugin and a SmartEVSE charging post.
I have several other sensors.
- 'Charging Level' a selector switch which switches Off, 6A, 8A, 10A, 13A, 16A depending on PV power production
- 'EVSE Swich 16A' a switch to bypass PV powered car charging.
- The 'Charging Display' which shows several data amongst which kWh / 100km
Bugs bug me.