appending values to an array everytime a sensor trips

Hey guys,

So what I’m trying to do is simple but I’m not sure how it would work in the Lua engine. Basically I have a motion sensor,
that is set to “Always Fire” mode (I followed their manual and connected it straight to a power adapter). What I want to
do is to log the rate at which the motion sensor trips.

Therefore, in the Scene, every time the armed sensor trips, I want to store the current os.time() in an array. And I want this
array to populate until it has like 10 values and then I want to write its contents to a log that I can see via ssh.

I am confused though because I don’t see how I can have an array that doesn’t “reset” everytime the trigger happens.
Can someone make some suggestions for this?

Thanks!!

You haven’t said if you are writing a full plugin or just snippets of Lua in a scene’s Luup tab. I will assume the latter.

You need to make the array’s current values persist somewhere in between trips. Any device will do, but I would probably go for a Variable Container. When the sensor trips, pull the current array contents from the variable on the device, append the current timestamp, then write it back.

This is complicated by the fact that all Luup variables are strings, so you need to serialize the array to a string when you write it, and deserialize it back from a string into an array when you read it. It’s customary to use a character like “;” for the separator. Lua has some pattern-matching functions that make this a bit easier. A bit.

You may decide that it’s simpler to skip the array entirely, and just use ten variables on the device instead (TimeStamp2, Timestamp3, …), plus two to represent the head and tail of your queue. Essentially you would be implementing a queue data structure from first principles.

What kind of fire rate are you expecting? There may be a much simpler way to measure the rate than use an array if it lies within certain bounds. You’re not actually interested in the actual firing times, just the rate - correct?

The Program Logic Event Genererator plugin keeps a small stack (I think it’s 5) of the last time triggers happened, device properties changed, and conditions were evaluated to true.

This allows the the multi-event semantic expression … i.e. so many events in less than and/or more than the specified interval.
[hr]
The following Lua code self initializes, limits the stack to size 10, removes the oldest data:

if (not MyDataArray) then
MyDataArray = {} (Potentially load from Persistent data store)
end
if (#MyDataArray == 10) then
table.remove(MyDataArray)
end
table.insert(MyDataArray, 1, SomeNewData)
(Potentially write to Persistent data store)

Thanks for the quick responses, yes I am trying to write this as part of a scene.

To Richard, where could I put that Lua code though? Could I attach it to a Luup event that is
attached to a trigger?

I’m just worried that every time the trigger trips, it won’t remember that it initialized the array last time.

Thanks for helping me understand!!

[quote=“RichardTSchaefer, post:4, topic:174157”]The Program Logic Event Genererator plugin keeps a small stack (I think it’s 5) of the last time triggers happened, device properties changed, and conditions were evaluated to true.

This allows the the multi-event semantic expression … i.e. so many events in less than and/or more than the specified interval.
[hr]
The following Lua code self initializes, limits the stack to size 10, removes the oldest data:

if (not MyDataArray) then
MyDataArray = {} (Potentially load from Persistent data store)
end
if (#MyDataArray == 10) then
table.remove(MyDataArray)
end
table.insert(MyDataArray, 1, SomeNewData)
(Potentially write to Persistent data store)[/quote]

You can definitely share state between the LUUP code in a trigger and the LUUP code at the scene level. I do not know about from one scene activation to the next … I have never looked into that.
Simple enough to try … use my example and see if the stack ever get’s full.

If it does not you will be forced to add the persistence logic.