The first part of the script seems to work okay. From the moment I'm commenting the parts it is trial and error and returning error codes.
It is my first time to use persistent data with history and I think the error is caused because I'm storing a string instead of numeric values. The devices (Sun Power as well as Production) are both custom sensors. Though as you can see I've already changed the data tonumber() before adding to the database.
Does somebody know whether I'm filling the database properly and how I can retrieve these values?
Code: Select all
return
{
active = true,
on = {
devices = {
6, -- IDX van SunPower
24 -- IDX van Productie zonnepanelen in W
}
},
logging =
{
level = domoticz.LOG_DEBUG, -- change to LOG_ERROR when OK - was LOG_DEBUG
marker = 'Zonnepanelen_defect'
},
data = { solar = { history = true, maxHours = 25, maxItems = 1000 }},
execute = function(dz, item)
local round = dz.utils.round
local Solar_theoretical = dz.devices(6) -- IDX van SunPower
local Solar_production = dz.devices(24) -- IDX van Productie zonnepanelen in W
local Alert = dz.devices(46) -- Alert device
local lastTheoreticalWatt = round(tonumber(Solar_theoretical.state),1)
local lastProductionWatt = round(tonumber(Solar_production.rawData[1]),1)
dz.log('lastTheoreticalWatt: ' .. lastTheoreticalWatt .. ' W',dz.LOG_DEBUG)
dz.log('lastProductionWatt: ' .. lastProductionWatt .. ' W',dz.LOG_DEBUG)
dz.data.solar.add -- Store current values in persistent historical data
(
{
['theoretical'] = lastTheoreticalWatt,
['production'] = lastProductionWatt
}
)
-- dz.log(dz.data.solar.get(2),dz.LOG_DEBUG)
local sum_2hr = dz.data.solar.sumSince('00:01:00')
dz.log('test ' .. sum_2hr .. ' test',dz.LOG_DEBUG)
--[[
if (lastTheoreticalWatt <= 15 and lastProductionWatt == 0) then
Alert.updateAlertSensor(ALERTLEVEL_GREY, 'Er is geen zon')
dz.log('Sun power (' .. lastTheoreticalWatt .. ') < 15 W and production = 0',dz.LOG_DEBUG)
elseif (theoretical.sumSince('02:00:00') > 0 and production.sumSince('02:00:00') == 0) then
Alert.updateAlertSensor(ALERTLEVEL_RED, 'Zonnepanelen lijken niet meer te werken')
dz.log('Zonnepanelen lijken afgelopen 2 uur niet te werken (zonkracht was in totaal ' .. theoretical.sumSince('02:00:00') .. ', maar productie nog steeds 0)',dz.LOG_DEBUG)
elseif (production.sumSince('24:00:00') / theoretical.sumSince('24:00:00')) > 2 then
Alert.updateAlertSensor(ALERTLEVEL_GREEN, 'Zonnepanelen werken naar behoren')
dz.log('Productie per theroretisch is groter dan 2 (' .. (production.sumSince('02:00:00') / theoretical.sumSince('24:00:00')) .. ')',dz.LOG_DEBUG)
elseif (production.sumSince('24:00:00') / theoretical.sumSince('24:00:00')) > 1 then
Alert.updateAlertSensor(ALERTLEVEL_ORANGE, 'Zonnepanelen lijken slechter te werken')
dz.log('Productie per theroretisch is tussen 1 en 2 (' .. (production.sumSince('02:00:00') / theoretical.sumSince('24:00:00')) .. ')',dz.LOG_DEBUG)
elseif (production.sumSince('24:00:00') / theoretical.sumSince('24:00:00')) <= 1 then
Alert.updateAlertSensor(ALERTLEVEL_RED, 'Zonnepanelen lijken niet meer te werken')
dz.log('Productie per theroretisch is lager dan 1 (' .. (production.sumSince('02:00:00') / theoretical.sumSince('24:00:00')) .. ')',dz.LOG_DEBUG)
end
]]--
end
}
Code: Select all
2020-09-10 20:51:51.999 Error: dzVents: Error: (3.0.2) Zonnepanelen_defect: Item data is not a number type. Type is nil
2020-09-10 20:51:51.999 Error: dzVents: Error: (3.0.2) Zonnepanelen_defect: An error occurred when calling event handler zonnepanelen_defect_dev
2020-09-10 20:51:51.999 Error: dzVents: Error: (3.0.2) Zonnepanelen_defect: ...ticz/scripts/dzVents/scripts/zonnepanelen_defect_dev.lua:52: attempt to concatenate a nil value (local 'sum_2hr')