Author Topic: How do I count sensor trips to increment a new variable ?  (Read 375 times)

Offline parkerc

  • Sr. Hero Member
  • ******
  • Posts: 2465
  • Karma: +35/-48
  • Life Moves Pretty Fast....
How do I count sensor trips to increment a new variable ?
« on: July 26, 2018, 05:03:52 am »
Hi,

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

Code: [Select]
function sensortripped()
  local tripscountedsofar = luup.variable_get("urn:micasaverde-com:serviceId:SecuritySensor1","TripCount",338)
  local addtriptocount = tripscountedsofar + 1
  luup.variable_set("urn:micasaverde-com:serviceId:SecuritySensor1","TripCount",addtriptocount,338)
end

luup.variable_watch("sensortripped","urn:micasaverde-com:serviceId:SecuritySensor1","Tripped",338)
« Last Edit: July 26, 2018, 04:30:58 pm by parkerc »

Offline jswim788

  • Hero Member
  • *****
  • Posts: 789
  • Karma: +57/-2
Re: How do I count sensor trips to increment a new variable ?
« Reply #1 on: July 26, 2018, 12:40:17 pm »
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?

Offline parkerc

  • Sr. Hero Member
  • ******
  • Posts: 2465
  • Karma: +35/-48
  • Life Moves Pretty Fast....
Re: How do I count sensor trips to increment a new variable ?
« Reply #2 on: July 26, 2018, 02:53:09 pm »
1) make sure that the TripCount variable exists before this starts or add this to your variable read: or "0"

Thanks, yes Ill do that.

2) suggest you use a different SID - you never know what Micasaverde will do in the future - you can make up your own SID

Good call, Ill factor that in as well.

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).

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

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

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 :-)
« Last Edit: July 26, 2018, 04:30:02 pm by parkerc »

Offline parkerc

  • Sr. Hero Member
  • ******
  • Posts: 2465
  • Karma: +35/-48
  • Life Moves Pretty Fast....
Re: How do I count sensor trips to increment a new variable ?
« Reply #3 on: July 26, 2018, 05:04:20 pm »
(Still traveling/away from Vera so can?t test)

Is this heading in the right direction ?

Code: [Select]
function sensortripped()
local istripcountthere = tonumber(luup.variable_get("urn:micasaverde-com:serviceId:SecuritySensor1","TripCount",338) or 0)
if
istripcountthere == nil  then
luup.variable_set("urn:nodecentral-net:serviceId:SecuritySensor1","TripCount",'0',338)
luup.log('TripCount variable was missing so has been created')
print('TripCount variable was missing so has been created')
else
luup.log('TripCount variable already exists')
print('TripCount variable already exists')
  end

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

luup.variable_watch("sensortripped","urn:micasaverde-com:serviceId:SecuritySensor1","Tripped",338)
« Last Edit: July 26, 2018, 08:07:43 pm by parkerc »

Offline RichardTSchaefer

  • Master Member
  • *******
  • Posts: 10091
  • Karma: +763/-142
Re: How do I count sensor trips to increment a new variable ?
« Reply #4 on: July 26, 2018, 05:37:35 pm »
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.

Offline parkerc

  • Sr. Hero Member
  • ******
  • Posts: 2465
  • Karma: +35/-48
  • Life Moves Pretty Fast....
Re: How do I count sensor trips to increment a new variable ?
« Reply #5 on: July 26, 2018, 06:01:17 pm »
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.

Offline parkerc

  • Sr. Hero Member
  • ******
  • Posts: 2465
  • Karma: +35/-48
  • Life Moves Pretty Fast....
Re: How do I count sensor trips to increment a new variable ?
« Reply #6 on: August 04, 2018, 04:52:02 pm »
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: [Select]
-- 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)
   if
      istripcountthere == nil  then
      luup.variable_set("urn:nodecentral-net:serviceId:SecuritySensor1","TripCount",'0',dev)
      print('TripCount variable was missing so has been created, now call function to increment TripCount variable')
      increment_trip_counter(tripped)
   else
      print("Trip counter already exists, current count = " ..istripcountthere)
      print('Call function to increment TripCount variable')
      increment_trip_counter(tripped)
   end
end

function increment_trip_counter(...)
   print(...)
   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)
   if
      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
      luup.variable_set("urn:nodecentral-net:serviceId:SecuritySensor1","TripCount",addtriptocount,dev)
      print("increment_trip_counter " ..addtriptocount)
   else
      luup.log('Sensor was closed, so no count added')
      print('Sensor was closed, so no count added')
   end
end
     

check_if_trip_counter_exists()



--[[luup.variable_watch("check_if_trip_counter_exists","urn:micasaverde-com:serviceId:SecuritySensor1","Tripped",338)]]--

Version 2

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

local function increment_trip_counter(dev)
   print(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
   luup.variable_set("urn:nodecentral-net:serviceId:SecuritySensor1","TripCount",addtriptocount,dev)
   print("now we add 1 to the trip so far counter to make it " ..addtriptocount)
end


local function check_if_trip_counter_variable_exists(dev)
   -- local dev = 338
   print(dev)
   local istripcountthere = luup.variable_get("urn:nodecentral-net:serviceId:SecuritySensor1","TripCount",dev)
   if
      istripcountthere == nil  then
      luup.variable_set("urn:nodecentral-net:serviceId:SecuritySensor1","TripCount",'0',dev)
      print('TripCount variable was missing so has been created, now call function to increment TripCount variable')
      increment_trip_counter(dev)
   else
      print("Trip counter already exists, current count = " ..istripcountthere)
      print('Call function to increment TripCount variable')
      increment_trip_counter(dev)
      return true
   end
end


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)
   if
      tripped == "0" then
      luup.log('Sensor was closed, so no count added')
      print('Sensor was closed, so no count added')
      return true
   else
      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?')
      check_if_trip_counter_variable_exists(dev)
   end
end



check_that_sensor_has_only_tripped_open(345)



--[[luup.variable_watch("check_if_trip_counter_exists","urn:micasaverde-com:serviceId:SecuritySensor1","Tripped",338)]]--


Offline akbooer

  • Master Member
  • *******
  • Posts: 6158
  • Karma: +276/-69
  • "Less is more"
Re: How do I count sensor trips to increment a new variable ?
« Reply #7 on: August 04, 2018, 05:43:12 pm »
Just some quick comments:
  • check_if_trip_counter_exists() is redundant, you can write code which works anyway (use 'or 0')
  • string to number coercion is done by the Lua interpreter anyway
  • it's not clear how or when your increment_trip_counter(...) is called... variable_watch ??
The basics are simply:

Code: [Select]
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)
3x Vera Lite-UI5/Edge-UI7, 25x Fibaro, 23x TKB, 9x MiniMote, 2x NorthQ Power, 2x Netatmo, 1x Foscam FI9831P, 9x Philips Hue,
Razberry, MySensors Arduino, HomeWave, AltUI, AltHue, DataYours, Grafana, openLuup, ZWay, ZeroBrane Studio.

Offline parkerc

  • Sr. Hero Member
  • ******
  • Posts: 2465
  • Karma: +35/-48
  • Life Moves Pretty Fast....
Re: How do I count sensor trips to increment a new variable ?
« Reply #8 on: August 04, 2018, 07:09:51 pm »
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 :)

string to number coercion is done by the Lua interpreter anyway

So, for arithmetic you do not need to take the string and make it a number - that great if not .

Code: [Select]
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)
it's not clear how or when your increment_trip_counter(...) is called... variable_watch ??

My workflow logic for version 2 code above was...

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.
2) Check if the device has had the TripCount variable added already, if not add it , and assign an initial value (0)
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 .

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 ..
« Last Edit: August 04, 2018, 07:14:07 pm by parkerc »