I'm trying to use an SQLite query to calculate when an alarm should become active. I store the start time and the detent (the timeout or delay before the alarm becomes active) in the same table as seconds. Here is the schema:
CREATE TABLE alarm_log (
scope integer NOT NULL, --REFERENCES scopes_inst(inst)
start integer NOT NULL, -- seconds
end integer,
severity text NOT NULL,
value text NOT NULL,
details text,
raised_by integer, -- the scope id that raised the alarm
detent integer, -- detent in seconds
PRIMARY KEY (scope, start)
);
And here is the query that I am trying to use to pull the active alarms using some simple math:
SELECT *,
(start + detent) AS activated, -- the time that the alarm becomes active
STRFTIME("%s","now") AS now -- the current time of the query
FROM alarm_log
WHERE end IS NULL
AND now > activated; -- ensure that the alarm is activated
I have created an alarm with a detent of 30 seconds. The start time is 1378870712 so it should become active at 1378870742 (see the activated column).
This is an example of the above query returning rows 4 seconds before the activated time...
scope start end severity value details raised_by detent activated now
---------- ---------- ---------- ---------- --------------------- ---------------- ---------- ---------- ---------- ----------
4 1378870712 warning out-of-range too-high min 500 max 1500 0 30 1378870742 1378870738
Now the same query returns that row 2 seconds after the alarm was activated.
scope start end severity value details raised_by detent activated now
---------- ---------- ---------- ---------- --------------------- ---------------- ---------- ---------- ---------- ----------
4 1378870712 warning out-of-range too-high min 500 max 1500 0 30 1378870742 1378870744
I would expect reversing the > on the last line to a < it should show alarms that are not active... but this returns nothing, before or after a detent time passes...
Is this normal behaviour? The query looks fine to me. Thanks for reading :)
EDIT:
I should have mentioned that I also tried it with the calculations straight in the WHERE clause, but it does the same thing :(
SELECT *,
(start + detent) AS activated, -- the time that the alarm becomes active
STRFTIME("%s","now") AS now -- the current time of the query
FROM alarm_log
WHERE end IS NULL
AND STRFTIME("%s","now") > (start + detent);
EDIT2:
Here's some test data:
SQLite styles:
scope|start|end|severity|value|details|raised_by|detent
4|1378935271|1378935842|warning|out-of-range too-high|min 500 max 1500|0|600
4|1378935854|1378935876|warning|out-of-range too-high|min 500 max 1500|0|600
4|1378935884||warning|out-of-range too-high|min 500 max 1500|0|600
CSV styles
scope,start,end,severity,value,details,raised_by,detent
4,1378935271,1378935842,warning,out-of-range too-high,min 500 max 1500,0,600
4,1378935854,1378935876,warning,out-of-range too-high,min 500 max 1500,0,600
4,1378935884,,warning,out-of-range too-high,min 500 max 1500,0,600