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)
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]-- 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)]]â
[/code]