|
<< Click to Display Table of Contents >> Navigation: Components > Control, logic & time > Script IDE (LUA / Python) > LUA example |
Example:
If you want to log on-times (e.g. how long was the light on or similar) at the MinMaxAverage component, for example, the LUA component can be connected upstream, which determines the on-times and then forwards them to the MinMaxAverage component once a day.
Channels:
Name |
Meaning |
Direction |
Number of elements |
Write output only on value change |
Use as trigger |
DiffSinceLastTrigger |
Buffer |
Output |
1 |
yes |
|
Input |
The binary signal whose "active time" is to be counted |
Input |
1 |
|
yes |
LastDay |
Buffer |
Output |
1 |
yes |
|
LastTimestamp |
Cache |
Output |
1 |
yes |
|
OnTimeOutSeconds |
current on-time (updated with every trigger) |
Output |
1 |
yes |
|
OnTimeOutSecondsDay |
Day on-time (only triggered at day change - this must be connected to the input of the MinMaxAverage component) |
Output |
1 |
yes |
|
UpdateTrigger |
additional trigger (e.g. every second) to output the current on-time and to detect the day change (can also occur less frequently - but should definitely trigger once at the day change so that the day change can be detected and the on-time can be output at the output) |
Input |
1 |
|
yes |
WasActive |
Intermediate memory |
Output |
1 |
yes |
|
LUA-Code:
function ScriptFunc()
isActive = Input
currentTimestamp = os.time()
currentDay = os.date("%M") -- %M for minute (test purposes) %d = Day
if LastDay == nil then LastDay = currentDay end
if OnTimeOutSeconds == nil then OnTimeOutSeconds = 0 end
-- add seconds since last timestamp if the input was on since last trigger
if WasActive == true then
if(LastTimestamp ~= nil) then
DiffSinceLastTrigger = tonumber(currentTimestamp - LastTimestamp)
OnTimeOutSeconds = tonumber(OnTimeOutSeconds + DiffSinceLastTrigger)
end
end
-- if is active - save timestamp for difference calculation on next trigger
if isActive == true then
LastTimestamp = currentTimestamp
end
-- day changed - trigger on day for last day and reset counter
if LastDay ~= currentDay then
OnTimeOutSecondsDay = tonumber(OnTimeOutSeconds)
OnTimeOutSeconds = 0
end
LastDay = currentDay
WasActive = isActive
end