waltervl wrote: Friday 01 April 2022 15:16
Add a timer trigger to the script ( every 10 minutes)
And check in the main script what the trigger is, device change or timer
When device change: update history
When timer trigger: update custom device
To check use something like
Code: Select all
return
{
on =
{
timer = { 'every 10 minutes', },
devices = {'Test Total solar radiation'},
},
execute = function(domoticz, item)
if item.isTimer then
--- do Device update
else
-- do History update
end
end
}
There is no need to clear the history as it will be a running average.
Unfortunately an earlier error came back.
This is how the script looks right now:
Code: Select all
return
{
on = {
timer = { 'every 10 minutes', },
devices = {'Test Total solar radiation'},
},
logging =
{
level = domoticz.LOG_DEBUG,
marker = 'SEM228T 10 min',
},
data =
{
SEM228THistory = { history = true, maxMinutes = 10},
},
execute = function(domoticz, item)
if item.isTimer then
-- do Device update
RoundAverage = domoticz.utils.round( (RadAverage),0)
domoticz.devices('SEM228T 10 min Av.').updateCustomSensor(RoundAverage)
domoticz.devices('SEM228T 10 min Min.').updateCustomSensor(RadMin)
domoticz.devices('SEM228T 10 min Max.').updateCustomSensor(RadMax)
else
-- add new data
domoticz.data.SEM228THistory.add(sensor.radiation)
-- average
local RadAverage = domoticz.data.SEM228THistory.avg()
domoticz.log('average radiation ' .. RadAverage, domoticz.LOG_INFO)
-- maximum value in the past 10 minutes:
local RadMax = domoticz.data.SEM228THistory.maxSince('00:10:00')
domoticz.log('max radiation ' .. RadMax, domoticz.LOG_INFO)
-- minimum value in the past 10 minutes:
local RadMin = domoticz.data.SEM228THistory.minSince('00:10:00')
domoticz.log('min radiation ' .. RadMin, domoticz.LOG_INFO)
domoticz.log('SEM228THistory entries = ' .. domoticz.data.SEM228THistory.size, domoticz.LOG_DEBUG)
--if (domoticz.data.SEM228THistory.size > 59) then
--RoundAverage = domoticz.utils.round( (RadAverage),0)
--domoticz.devices('SEM228T 10 min Av.').updateCustomSensor(RoundAverage)
--domoticz.devices('SEM228T 10 min Min.').updateCustomSensor(RadMin)
--domoticz.devices('SEM228T 10 min Max.').updateCustomSensor(RadMax)
--domoticz.log('Not enough data points yet to produce something useful', domoticz.LOG_DEBUG)
--domoticz.log(domoticz.data.SEM228THistory.size .. ' entries for ' .. SEM228THistory .. ' in History', domoticz.LOG_DEBUG)
--reset()
end
end
}
This is the error I'm getting now:
Code: Select all
2022-04-01 17:09:59.314 Status: dzVents: Info: SEM228T 10 min: ------ Start external script: SEM228T10M.lua: Device: "Test Total solar radiation (Test Total solar radiation)", Index: 610
2022-04-01 17:09:59.316 Status: dzVents: Info: SEM228T 10 min: ------ Finished SEM228T10M.lua
2022-04-01 17:09:59.316 Error: dzVents: Error: (3.1.7) SEM228T 10 min: An error occurred when calling event handler SEM228T10M
2022-04-01 17:09:59.316 Error: dzVents: Error: (3.1.7) SEM228T 10 min: /home/pi/domoticz/scripts/dzVents/scripts/SEM228T10M.lua:34: attempt to index a nil value (global 'sensor')
2022-04-01 17:10:00.173 Status: dzVents: Info: SEM228T 10 min: ------ Start external script: SEM228T10M.lua:, trigger: "every 10 minutes"
2022-04-01 17:10:00.177 Status: dzVents: Debug: SEM228T 10 min: Processing device-adapter for SEM228T 10 min Av.: Custom sensor device adapter
2022-04-01 17:10:00.178 Status: dzVents: Debug: SEM228T 10 min: Processing device-adapter for SEM228T 10 min Min.: Custom sensor device adapter
2022-04-01 17:10:00.179 Status: dzVents: Debug: SEM228T 10 min: Processing device-adapter for SEM228T 10 min Max.: Custom sensor device adapter
2022-04-01 17:10:00.180 Status: dzVents: Info: SEM228T 10 min: ------ Finished SEM228T10M.lua
2022-04-01 17:10:00.175 Error: dzVents: Error: (3.1.7) SEM228T 10 min: nil is not a number!
line 34 is this one:
Code: Select all
domoticz.data.SEM228THistory.add(sensor.radiation)
When I then replace the script with the original working one, it looks like it is runing perfectly for one run:
Code: Select all
2022-04-01 16:24:29.482 Status: dzVents: Info: SEM228T 10 min: ------ Start external script: SEM228T10M.lua: Device: "Test Total solar radiation (Test Total solar radiation)", Index: 610
2022-04-01 16:24:29.501 Status: dzVents: Info: SEM228T 10 min: average radiation 576.41071428571
2022-04-01 16:24:29.501 Status: dzVents: Info: SEM228T 10 min: max radiation 700
2022-04-01 16:24:29.501 Status: dzVents: Info: SEM228T 10 min: min radiation 264
2022-04-01 16:24:29.501 Status: dzVents: Debug: SEM228T 10 min: SEM228THistory entries = 56
2022-04-01 16:24:29.505 Status: dzVents: Info: SEM228T 10 min: ------ Finished SEM228T10M.lua
2022-04-01 16:24:39.462 Status: dzVents: Info: SEM228T 10 min: ------ Start external script: SEM228T10M.lua: Device: "Test Total solar radiation (Test Total solar radiation)", Index: 610
2022-04-01 16:24:39.481 Status: dzVents: Info: SEM228T 10 min: average radiation 578.10526315789
2022-04-01 16:24:39.481 Status: dzVents: Info: SEM228T 10 min: max radiation 700
2022-04-01 16:24:39.481 Status: dzVents: Info: SEM228T 10 min: min radiation 264
2022-04-01 16:24:39.481 Status: dzVents: Debug: SEM228T 10 min: SEM228THistory entries = 57
2022-04-01 16:24:39.486 Status: dzVents: Info: SEM228T 10 min: ------ Finished SEM228T10M.lua
2022-04-01 16:24:49.339 Status: dzVents: Info: SEM228T 10 min: ------ Start external script: SEM228T10M.lua: Device: "Test Total solar radiation (Test Total solar radiation)", Index: 610
2022-04-01 16:24:49.359 Status: dzVents: Info: SEM228T 10 min: average radiation 580.25862068966
2022-04-01 16:24:49.359 Status: dzVents: Info: SEM228T 10 min: max radiation 703
2022-04-01 16:24:49.359 Status: dzVents: Info: SEM228T 10 min: min radiation 264
2022-04-01 16:24:49.359 Status: dzVents: Debug: SEM228T 10 min: SEM228THistory entries = 58
2022-04-01 16:24:49.365 Status: dzVents: Info: SEM228T 10 min: ------ Finished SEM228T10M.lua
2022-04-01 16:24:59.587 Status: dzVents: Info: SEM228T 10 min: ------ Start external script: SEM228T10M.lua: Device: "Test Total solar radiation (Test Total solar radiation)", Index: 610
2022-04-01 16:24:59.606 Status: dzVents: Info: SEM228T 10 min: average radiation 582.47457627119
2022-04-01 16:24:59.607 Status: dzVents: Info: SEM228T 10 min: max radiation 711
2022-04-01 16:24:59.607 Status: dzVents: Info: SEM228T 10 min: min radiation 264
2022-04-01 16:24:59.607 Status: dzVents: Debug: SEM228T 10 min: SEM228THistory entries = 59
2022-04-01 16:24:59.788 Status: dzVents: Info: SEM228T 10 min: ------ Finished SEM228T10M.lua
2022-04-01 16:25:09.728 Status: dzVents: Info: SEM228T 10 min: ------ Start external script: SEM228T10M.lua: Device: "Test Total solar radiation (Test Total solar radiation)", Index: 610
2022-04-01 16:25:09.752 Status: dzVents: Info: SEM228T 10 min: average radiation 584.66666666667
2022-04-01 16:25:09.752 Status: dzVents: Info: SEM228T 10 min: max radiation 714
2022-04-01 16:25:09.753 Status: dzVents: Info: SEM228T 10 min: min radiation 264
2022-04-01 16:25:09.753 Status: dzVents: Debug: SEM228T 10 min: SEM228THistory entries = 60
2022-04-01 16:25:09.755 Status: dzVents: Debug: SEM228T 10 min: Processing device-adapter for SEM228T 10 min Av.: Custom sensor device adapter
2022-04-01 16:25:09.759 Status: dzVents: Debug: SEM228T 10 min: Processing device-adapter for SEM228T 10 min Min.: Custom sensor device adapter
2022-04-01 16:25:09.760 Status: dzVents: Debug: SEM228T 10 min: Processing device-adapter for SEM228T 10 min Max.: Custom sensor device adapter
2022-04-01 16:25:09.760 Status: dzVents: Info: SEM228T 10 min: ------ Finished SEM228T10M.lua
2022-04-01 16:25:09.760 Error: dzVents: Error: (3.1.7) SEM228T 10 min: An error occurred when calling event handler SEM228T10M
2022-04-01 16:25:09.760 Error: dzVents: Error: (3.1.7) SEM228T 10 min: /home/pi/domoticz/scripts/dzVents/scripts/SEM228T10M.lua:48: attempt to concatenate a nil value (global 'SEM228THistory')
And after that SEM228THistory entries freezes at 59, somewhat later 66
Code: Select all
2022-04-01 16:30:19.573 Status: dzVents: Info: SEM228T 10 min: ------ Start external script: SEM228T10M.lua: Device: "Test Total solar radiation (Test Total solar radiation)", Index: 610
2022-04-01 16:30:19.592 Status: dzVents: Info: SEM228T 10 min: average radiation 538.20338983051
2022-04-01 16:30:19.592 Status: dzVents: Info: SEM228T 10 min: max radiation 711
2022-04-01 16:30:19.592 Status: dzVents: Info: SEM228T 10 min: min radiation 255
2022-04-01 16:30:19.592 Status: dzVents: Debug: SEM228T 10 min: SEM228THistory entries = 59
2022-04-01 16:30:19.597 Status: dzVents: Info: SEM228T 10 min: ------ Finished SEM228T10M.lua
2022-04-01 16:30:29.485 Status: dzVents: Info: SEM228T 10 min: ------ Start external script: SEM228T10M.lua: Device: "Test Total solar radiation (Test Total solar radiation)", Index: 610
2022-04-01 16:30:29.504 Status: dzVents: Info: SEM228T 10 min: average radiation 536.15254237288
2022-04-01 16:30:29.505 Status: dzVents: Info: SEM228T 10 min: max radiation 711
2022-04-01 16:30:29.505 Status: dzVents: Info: SEM228T 10 min: min radiation 255
2022-04-01 16:30:29.505 Status: dzVents: Debug: SEM228T 10 min: SEM228THistory entries = 59
2022-04-01 16:30:29.509 Status: dzVents: Info: SEM228T 10 min: ------ Finished SEM228T10M.lua
2022-04-01 17:49:29.484 Status: dzVents: Info: SEM228T 10 min: ------ Start external script: SEM228T10M.lua: Device: "Test Total solar radiation (Test Total solar radiation)", Index: 610
2022-04-01 17:49:29.505 Status: dzVents: Info: SEM228T 10 min: average radiation 371.34848484848
2022-04-01 17:49:29.506 Status: dzVents: Info: SEM228T 10 min: max radiation 397
2022-04-01 17:49:29.506 Status: dzVents: Info: SEM228T 10 min: min radiation 283
2022-04-01 17:49:29.506 Status: dzVents: Debug: SEM228T 10 min: SEM228THistory entries = 66
2022-04-01 17:49:29.507 Status: dzVents: Debug: SEM228T 10 min: Processing device-adapter for SEM228T 10 min Av.: Custom sensor device adapter
2022-04-01 17:49:29.508 Status: dzVents: Debug: SEM228T 10 min: Processing device-adapter for SEM228T 10 min Min.: Custom sensor device adapter
2022-04-01 17:49:29.509 Status: dzVents: Debug: SEM228T 10 min: Processing device-adapter for SEM228T 10 min Max.: Custom sensor device adapter
2022-04-01 17:49:29.514 Status: dzVents: Info: SEM228T 10 min: ------ Finished SEM228T10M.lua