Live Motion Report

This is a tutorial for creating a live motion report display on a WebRemote page. This is a very useful display for seeing whats going on in your system right now live. It's a combination of 3 parts that you need to create. First you need to create a “group” in XTension and place the units you wish to have monitored and displayed into it. In the ON script of the group add the following script. This will be called each time that a unit in the group changes state and can re-update the display data. In my system all the motion sensors start their name with the word “MOTION” and all the perimeter units start with “PERIMETER” and it's not necessary to waste web site space showing that so this routine also has a method to filter those out. You can add or change the words that will be removed from the list below. At this point you will also need to create another “pseudo” unit into which the data will be stored as the description for display in the web remote. I called mine “activity holder” you'll need to set that name at the beginning of the script. I've inserted many comments describing what the applescript code is doing but basically you're creating a small snippet of HTML code that is being inserted into the webpage and it's updated live whenever this script runs to display new data.

 
 --Latest Motion Activity script James Sentman MacHomeAutomation.com
 --this script keeps a local variable that has the necessary data in it
 --to build a recent motion type display in X2Web.
 
 -- list format { {sensorname, count}, {sensor name, count} }

 --change this to hold the name of the pseudo unit you created to hold the resultant display data
 property DisplayHolderUnit: "Activity Holder"
 
 --change this to set the number of lines of data to display, max of 30
 property DisplayLineCount: 15
 
 --this is a "list of lists" that hold the data between calls
property DataList : {}


 --this handler will remove prefix words that you may have in your database
 --but that may not make sense to display in the live motion activity control.
on FilterUnitName(TheName)

--add or remove items from this list to filter your unit names so
--that you dont display whats not necessary and it fits properly in the 
--page or view display	

set WordsToFilter to {"MOTION", "PERIMETER"}

set FirstWord to the 1st word of TheName
if FirstWord is in WordsToFilter then
	set AllWords to words 2 through (count of words in TheName) of TheName
else
	return TheName
end if
set AppleScript's text item delimiters to " "
set FilteredName to AllWords as text
set AppleScript's text item delimiters to ""

return FilteredName


end FilterUnitName




  --this is the handler that is called automatically every time one of the units in the group changes state.
on GroupMemberChanged(TheUnit, TheValue)
set IgnoreOff to true


--if we want to ignore offs and this is an off, then ignore it
      --if you're doing motion sensors then leave IgnoreOff to true or you'll
      --get another entry for them when they send an off too which will be confusing

if (IgnoreOff = true) and (TheValue = 0) then
	return
end if

try
	
	set FirstEntry to first item of DataList
	
	if item 1 of FirstEntry is TheUnit then
		--increment the counter
		set newcount to (second item of FirstEntry) + 1
		-- put the new number in the second element of the entry  
		set second item of FirstEntry to newcount
		set the third item of FirstEntry to (time string of (current date))
		set the first item of DataList to FirstEntry
	else
		copy {TheUnit, 1, time string of (current date)} to the beginning of DataList
	end if
	
on error
	--unable to get the first element of the list so just add it
	
	set DataList to {{TheUnit, 1, (time string of (current date))}}
	
end try

--limit to 30 items
if (count of DataList) is greater than 30 then
	set DataList to items 1 through 30 of DataList
	
end if

set description of DisplayHolderUnit to "<div style=\"font-family:verdana; font-size:11px\">" & GetHTMLDataList(DisplayLineCount) & "</div>"
end GroupMemberChanged



on GetHTMLDataList(NumToShow)
--this method is called by the X2Web page to get the NumToShow most 
--recent events from the units you've placed in this list.

set theoutput to ""
set TheCount to count of DataList

if TheCount is greater than NumToShow then
	set TheCount to NumToShow
end if


repeat with i from 1 to TheCount
	
	if i is greater than 1 then
		set theoutput to theoutput & "<br>"
	end if
	
	set ThisEntry to (item i of DataList)
	set theoutput to theoutput & (item 3 of ThisEntry) & ": " & FilterUnitName(item 1 of ThisEntry)
	if (item 2 of ThisEntry) is greater than 1 then
		set theoutput to theoutput & " (" & (item 2 of ThisEntry) & ")"
	end if
end repeat

return theoutput

end GetHTMLDataList

on GetTextDataList(NumToShow)
set theoutput to ""
set TheCount to count of DataList


if TheCount is greater than NumToShow then
	set TheCount to NumToShow
end if


repeat with i from 1 to TheCount
	
	if i is greater than 1 then
		set theoutput to theoutput & return
	end if
	
	set ThisEntry to (item i of DataList)
	set theoutput to theoutput & (item 3 of ThisEntry) & ": " & FilterUnitName(item 1 of ThisEntry)
	if (item 2 of ThisEntry) is greater than 1 then
		set theoutput to theoutput & " (" & (item 2 of ThisEntry) & ")"
	end if
end repeat

return theoutput

end GetTextDataList

Once you have the group with the units added and the script placed in it's ON script and the display data holder pseudo created and the script edited to hold it's name you should be able to trigger a motion sensor and start to see the html data show up in the description of the holder pseudo unit you created. If so then everything is working and you can move on to create the control in the web remote. If web remote is running when you created the display data holder unit then you may need to disable and re-enable it in the preferences to get it to pick up on the new unit you created. Once you've done that you can click the admin menu on the page, (or control click on a holder box to add more controls to it) and select “add control” from the resultant popup select “unit description as HTML snippet” and select the name of the display data holder pseudo you created as the unit. Anything you put into the description of that unit will be piped to this display you've just created, and anything that changes it will result in a live update on the webpage.

webremote/livemotion.txt · Last modified: 2013/07/29 16:53 by James Sentman
CC Attribution-No Derivative Works 3.0 Unported
www.chimeric.de Valid CSS Driven by DokuWiki do yourself a favour and use a real browser - get firefox!! Recent changes RSS feed Valid XHTML 1.0