How do I count sensor trips to increment a new variable ?


I want to create a new variable that counts the number of times a motion sensor trips to see how active a room is . I?ve been playing with some code but not sure I have the logic correct.

My logic suggests I can do the following (which is currently not tested as im traveling - just looking for advice for now on the best way to do it )

  1. create a new variable on a motion sensor called TripCount
  2. Then do a luup_variablewatch for when the sensor trips to run a global function that adds +1 to that new TripCount variable
function sensortripped()
  local tripscountedsofar = luup.variable_get("urn:micasaverde-com:serviceId:SecuritySensor1","TripCount",338)
  local addtriptocount = tripscountedsofar + 1


a few small suggestions:

  1. make sure that the TripCount variable exists before this starts or add this to your variable read: or “0”
  2. suggest you use a different SID - you never know what Micasaverde will do in the future - you can make up your own SID
  3. this will count every time the value is changed - so you’ll get both tripped and untripped. You could just live with this, or you could add a test for Tripped equal to “1” (or whatever it is for your sensor).

Maybe you want to plot this over time? In that case do you want to reset it periodically and plot those values?

[font=verdana]Thanks, yes Ill do that.

Good call, Ill factor that in as well.

Good spot, thanks - as I only what to capture the trips, Ill see how that can be done

Yes, that was my thinking too, I was just not sure what to set that level/time period to, also I had a idea that I could consider creating other variables to store the motion activity during set periods, writing the incremental total at a specific time to another variable and then reset the TripCount counter. e.g maybe do it in 4 hour slots 8am-12pm, 12pm -4pm, 4pm - 8pm, 8pm - 12pm etc.

Just a thought :slight_smile:

(Still traveling/away from Vera so can?t test)

Is this heading in the right direction ?

function sensortripped()
local istripcountthere = tonumber(luup.variable_get(“urn:micasaverde-com:serviceId:SecuritySensor1”,“TripCount”,338) or 0)
istripcountthere == nil then
luup.log(‘TripCount variable was missing so has been created’)
print(‘TripCount variable was missing so has been created’)
luup.log(‘TripCount variable already exists’)
print(‘TripCount variable already exists’)

local tripscountedsofar = luup.variable_get("urn:micasaverde-com:serviceId:SecuritySensor1","TripCount",338) or 0
local addtriptocount = tripscountedsofar + 1



Or just use the “Counter” input in PLEG

Than as an action for some logic you an increment it.
When it reaches a certain value … you can run another action.

1 Like

Thanks RTS

Your PLEG plugin/app is great, but whereever possible I still prefer to try to do as many things as I can in Lua/luup, if only to help me learn/improve my poor programming skills.

Ive created 2 versions of code to do this, both will only count the number of (1) opening trips and ignore any closure (0) trips.

I would welcome any advice on which one is best, or perhaps if there is another more efficient way.

Version 1

-- code to be called when a security sensor is tripped, to count how many times its been tripped.

function check_if_trip_counter_exists()
   local dev = 338
   local tripped = luup.variable_get("urn:micasaverde-com:serviceId:SecuritySensor1","Tripped",dev)
   print("Has the sensor tripped? 1 = Yes, 2 = No -  " ..tripped)
   local istripcountthere = luup.variable_get("urn:nodecentral-net:serviceId:SecuritySensor1","TripCount",dev)
      istripcountthere == nil  then
      print('TripCount variable was missing so has been created, now call function to increment TripCount variable')
      print("Trip counter already exists, current count = " ..istripcountthere)
      print('Call function to increment TripCount variable')

function increment_trip_counter(...)
   local tripped = ...
   local dev = 338
   local tripscountedsofar = luup.variable_get("urn:nodecentral-net:serviceId:SecuritySensor1","TripCount",dev)
   print("increment_trip_counted string value so far = " ..tripscountedsofar)
   local tripcount = tonumber(tripscountedsofar)
   print("increment_trip_counter value is converted from a string to a number " ..tripcount)
      tripped == "1" then
      print("sensor has been tripped how many times has it been tripped so far? " ..tripscountedsofar)
      luup.log('TripCount variable already exists')
      print('TripCount variable already exists')
      local addtriptocount = tripcount + 1
      print("increment_trip_counter " ..addtriptocount)
      luup.log('Sensor was closed, so no count added')
      print('Sensor was closed, so no count added')



Version 2

[code]-- code to be called when a security sensor is tripped, to count how many times its been tripped.

local function increment_trip_counter(dev)
local tripscountedsofar = luup.variable_get(“urn:nodecentral-net:serviceId:SecuritySensor1”,“TripCount”,dev)
print("how many trips so far = " …tripscountedsofar)
local tripcount = tonumber(tripscountedsofar)
print("convert tripscountedsofar from a string to a number " …tripcount)
local addtriptocount = tripcount + 1
print("now we add 1 to the trip so far counter to make it " …addtriptocount)

local function check_if_trip_counter_variable_exists(dev)
– local dev = 338
local istripcountthere = luup.variable_get(“urn:nodecentral-net:serviceId:SecuritySensor1”,“TripCount”,dev)
istripcountthere == nil then
print(‘TripCount variable was missing so has been created, now call function to increment TripCount variable’)
print("Trip counter already exists, current count = " …istripcountthere)
print(‘Call function to increment TripCount variable’)
return true

local function check_that_sensor_has_only_tripped_open(dev)
– local dev = 338
local tripped = luup.variable_get(“urn:micasaverde-com:serviceId:SecuritySensor1”,“Tripped”,dev)
print("Has the sensor tripped? 1 = Yes, 2 = No - " …tripped)
tripped == “0” then
luup.log(‘Sensor was closed, so no count added’)
print(‘Sensor was closed, so no count added’)
return true
luup.log(‘Sensor was open, so now check if the trip count variable is there?’)
print(‘Sensor was open, so now check if the trip count variable is there?’)



Just some quick comments:

[ul][li][tt]check_if_trip_counter_exists() [/tt]is redundant, you can write code which works anyway (use ‘or 0’)[/li]
[li]string to number coercion is done by the Lua interpreter anyway[/li]
[li]it’s not clear how or when your [tt]increment_trip_counter(…)[/tt] is called… variable_watch ??[/li][/ul]

The basics are simply:

local var = "TripCount"
local sid = "urn:nodecentral-net:serviceId:SecuritySensor1"
local n = luup.variable_get(sid, var, dev) or 0
luup.variable_set(sid, var, n+1, dev)

Thanks as always @akbooer

Im, not sure I follow all your points, but its all good stuff to help me - as this continues to be just one big learning curve for me. I?m proud I got it to work :slight_smile:

[font=verdana]So, for arithmetic you do not need to take the string and make it a number - that great if not .[/font]

local var = "TripCount"local sid = "urn:nodecentral-net:serviceId:SecuritySensor1"local n = luup.variable_get(sid, var, dev) or 0luup.variable_set(sid, var, n+1, dev)

[font=verdana]My workflow logic for version 2 code above was…[/font]

[font=verdana]1) Check if the door/motions sensor has tripped on/opened (value = 1) - if not, stop there do nothing more - if it is open, then go to step 2.[/font]
[font=verdana]2) Check if the device has had the TripCount variable added already, if not add it , and assign an initial value (0)[/font]
[font=verdana]3) Now you have the variable present, take the value that is already or the (0) if its just been created/added, and then just add/increment (+) 1 on to that value - so you can see a running total of how many times that sensors has tripped open in total .[/font]

As for the variable watch, that is excluded for now for this post - but I would look to enable to allow it to watch for the sensor to be triggered and the function called for next steps …

Do you full working version of your code yet and could you share it

If you’re just looking to trigger an event when a sensor trips a certain number of times in a certain period, that’s something Reactor does easily.

Is it done the same as PLEG and you start a Counter?

No, Reactor just counts events anyway and you just tell it what count to trigger on.

Well so far I have day of week and Hallway Motion tripped but don’t know what else to set or action to take

On the motion sensor condition, there’s a downward-pointing chevron (arrow) on the right side… click that. It will open up the Condition Options panel. In the “Restrictions” section, you’ll find “Condition must repeat” with a count and timing options.

More info here: Condition Options (Reactor documentation)

No my motion sensors are MySensor Modules so no options just wireless sensors that send information to gateway that is connected to MySensor app thru IP address. So I guess Reactor will not work.
But maybe idea for addition to Reactor.

Do your motion sensors have appearances in the dashboard/device list as Vera devices?

1 Like

Yes they do but have no options to choose from and configuration is accomplished in MySensos code.

The option I’m talking about is in Reactor. Go the to the link I posted earlier: Condition Options

It gives you instructions for what I’m talking about. If that’s not clear, post a screen shot (just this one time) of your Reactor conditions and I’ll point it out to you.

Got it now I will try this out
thanks much
Tried the setting under restrictions and I placed 2 times in at least 3600 seconds.
and I still do not know what to do with Actives
Do you still want screen shot?

1 Like

Best Home Automation shopping experience. Shop at getvera!

© 2021 Ezlo Innovation, All Rights Reserved. Terms of Use | Privacy Policy | Forum Rules