Date and leap year handling in dzvents
Posted: Sunday 10 August 2025 9:15
How can I make this script below working correctly at leap years? Now it always adds 364 days to calculate the end contract date as I am using addDays. If I could use addYears it would be easier I guess but that attribute doesn't exist. Maybe there's an easier way.
Code: Select all
return {
on = {
timer = { 'every minute'}, -- for testing only, else use 'at 00:00'
},
logging = {
level = domoticz.LOG_DEBUG,
marker = "timer"
},
execute = function(domoticz)
local Time = require('Time')
local contractStartDate = Time(domoticz.variables("Energy_Contract_Start").date.rawDate .. ' 00:00:00')
domoticz.log('Contract Start Date: ' .. contractStartDate.rawDate, domoticz.LOG_INFO) -- e.g. 2025-07-15
local contractEndDate = contractStartDate.addDays(364) -- make a new time object '1 year - 1 day' later. This fails at leap years!
domoticz.log('Contract End Date: ' .. contractEndDate.rawDate, domoticz.LOG_INFO) -- e.g. 2026-07-14
if domoticz.time.rawDate == contractEndDate.rawDate then -- e.g. "2026-07-14"
domoticz.log('Energy contract ends today!', domoticz.LOG_INFO)
else
domoticz.log('Energy contract does not end today', domoticz.LOG_INFO)
end
end
}