The Fibaro Motion Sensor can be configured to detect motion with an accuracy of one second between movements. With two motion sensors working together on either side of a door, it is possible to count the number of people entering and leaving a room.

SOME THOUGHTS ON THIS

There is a limitation with this approach: because the minimum setting for the motion sensor is 1 second, if two people enter a room within 1 second of each other, there is no way of detecting the two people apart. If you need accuracy like this, perhaps a better option is a universal sensor connected with two light beams in the door frame. This still won’t detect two people walking through the door at the same time sideways, but the chances of this are much less - one would assume! Some things that need to be considered when using two motion sensors:

    • The motion sensors need to be above the door frame so the frame acts as a filter - so the outside sensor doesn’t detect the inside one and visa versa.
    • The sensors need to be rotated inwards so that only a very narrow beam of detection is looking straight down. This stops the movement in the room triggering the device.

In essence you are narrowing the beam to two areas just on either side of the door.

WHAT WILL I NEED?

You will need two Fibaro Motion Sensors - one for each side of the door. Of course, if you have more than one door in the room, you will need two motion sensors for each door.

LET’S GO!

Making the sensors more sensitive

Change these parameters in both motion sensors: Parameter 1 = 8 Parameter 2 = 0 Parameter 6 = 1s Save this and wake up the device to let it know things have changed.

The global variable

We need a global variable to contain the count of people in the room. We have called ours PeopleCount, but feel free to change that - especially if you’re counting more than one area.

The code

Don’t forget to change the trigger device IDs and the two local variables motionInside and motionOutside to the IDs of the motion sensors you want to monitor!

--[[
%% properties
325 value
330 value
%% globals
--]]
--[[
People counter
This scene has been created by and is copyright (c) 2014, Fibaro UK Ltd
http://www.FibaroUK.co.uk/
You may use this scene for personal or commercial purposes however
please respect our intellectual property and leave this comment block intact
and unmodified.
]]
-- VERSION 1.1
-- change log at bottom of scene
--[[
This scene uses a global variable to monitor the number of people in a room.
You must create the global variable in panels->variables in order to use this
scene. By default the global variable should be called 'PeopleCounter' this
must be changed if the scene is duplicated for use in different rooms.
]]
-- change these and the triggers to match the IDs of the two motion sensors
local motionInside = 325
local motionOutside = 330
-- this scene should only be run from the sensors
local trigger = fibaro:getSourceTrigger()
if (trigger['type'] ~= 'property') then
fibaro:abort()
end
-- Only allow one instance of the current scene to run at a time
if (fibaro:countScenes() > 1) then
fibaro:abort()
end
local debugLevel = 5
function dbug (message, level)
if (debugLevel >= level) then
fibaro:debug(message)
end
end
local firstTrigger = tonumber(trigger['deviceID'])
local lastTrigger = 0
local both = false
local motionOnInside = false
local motionOnOutside = false
repeat
motionOnInside = (tonumber(fibaro:getValue(motionInside, 'value')) > 0)
motionOnOutside = (tonumber(fibaro:getValue(motionOutside, 'value')) > 0)
if ((motionOnInside or motionOnOutside) and (not (motionOnInside and motionOnOutside))) then
if both then
dbug('motionOnInside: ' .. tostring(motionOnInside), 20)
dbug('motionOnOutside: ' .. tostring(motionOnOutside), 20)
if (motionOnInside) then
lastTrigger = motionInside
dbug('So! Last Trigger: ' .. lastTrigger, 20)
else
lastTrigger = motionOutside
dbug('Now! Last Trigger: ' .. lastTrigger, 20)
end
end
else
both = true
end
fibaro:sleep(10) -- let's be a little bit nice to the processor!
until not (motionOnInside or motionOnOutside)
local count = tonumber(fibaro:getGlobalValue('PeopleCount'))
if (lastTrigger ~= 0) then
dbug('First Trigger: ' .. firstTrigger, 15)
dbug('Last Trigger: ' .. lastTrigger, 15)
dbug('motionInside: ' .. motionInside, 15)
dbug('motionOutside: ' .. motionOutside, 15)
if (firstTrigger ~= lastTrigger) then
if (firstTrigger == motionInside) then
dbug('Going out!', 10)
if (count > 0) then
count = count - 1
end
else
dbug('Coming in!', 10)
count = count + 1
end
else
if (firstTrigger == motionInside) then
dbug('Out and in.', 10)
else
dbug('In and out.', 10)
end
end
fibaro:setGlobal('PeopleCount', count)
dbug('Count: ' .. count, 5)
else
dbug('Ignore', 11)
end
--[[
Change log
1.1
- Changed location of getGlobalValue to make use with multi-door counts more
accurate
1.0
- Initial release
]]

THINGS TO TRY

Adjustments

See how it works. Adjust the height of the sensors if there is interference between inside and outside. Rotate them further in if there is interference from within the room. The local variable debugLevel can be changed to give more debugging detail. Try values of 5, 10, 11 and 20.

Going further

If you have more than one access door in the room, you may want to add sensors to the other doors too. You will need to duplicate the scene so that each door will run independently of the other, but the global variable used for counting will remain the same. If you have a door sensor, it would make sense to ignore movement unless the door is open - but don’t ignore it completely, you may be wanting to go out through the door you just opened… or maybe you could ignore it completely - time to experiment some more 🙂

WHERE NOW?

For more information on programming the Home Center 2 in Lua, see our support page at http://www.fibarouk.co.uk/support/lua/