Please login or register.

Login with username, password and session length
Advanced search  

News:

The New WIFI Module is now available!
                   *****Here's the link******

Author Topic: [Advanced] Monitoring and logging actions  (Read 13779 times)

orcusomega

  • Sr. Member
  • ****
  • Helpful Post Rating: 6
  • Posts: 97
[Advanced] Monitoring and logging actions
« on: January 11, 2011, 02:40:57 PM »

Narrative:
AHP 3.301
Runs from the interface
This series of macros allows a user with SmartMacros to be able to see when DS10A contacts are opened and closed, log the actions that were observed, and trigger alerts (chimes, etc).

I have contacts on several doors, and all reporting into AHP.  When a sensor is opened, it will alert, and when the contacts are closed, it will trigger an alert - BUT THE SENSOR ALSO SENDS A CLOSE SIGNAL EVERY ~2 MINUTES.  So in order to keep this as simple as possible, I have used flags to keep the state of the sensor.  When the sensor opens, Flag 1 is set, and when it closes the flag is cleared.  Any additional sensor status CLOSED messages are not actioned on, because the flag was cleared when the door first closed...

In order to allow for logging, it is necessary to use windows batch scripts, because AHP does not allow you to use command line arguments.  In this example, I will show three - one that is triggered when the contact opens, one that is triggered when the contact closes, and a universal script that does all the heavy lifting.

It is important to note here:  Since you can't tell AHP to do something like "run batch.bat option1 option2" etc, each and every set of commands is going to need its own script to run.  When you program 20 of these, and you decide to change where the file goes, then what?

In my example, I use the initial front_door-OPEN.bat and front_door-CLOSED.bat files to simply do what AHP can't - give options to the file that actually writes out the log.  Here is what it looks like, and a detailed explanation:

--------------
Macro 1 Name:
Front door Opened

Trigger & Conditions:
   DS10A on front door triggered

Actions:
   run batch script front_door-OPEN.bat
   set Flag 1
   
--------------
Macro 2 Name:
Front door Closed

Trigger & Conditions:
   DS10A on front door Closed
   Flag 1 = Set

Actions:
   run batch script front_door-CLOSED.bat
   Clear Flag 1

   

Now when either of these scripts are run, I want it to be written to a spreadsheet (well, a comma seperated file) with the following fields:
  • DATE
  • TIME
  • TYPE
    What I am watching, Door, window, etc
  • LOCATION
    Where it occurred front, back, etc
  • ACTION
    What happened - opened, closed, etc
  • DESCRIPTION
    Human readable info on what has happened - if there are any spaces, must be in ""

And without further ado, here is the front_door-OPEN.bat file:
Code: [Select]
@echo off
set TOOLSDIR=D:\X10_Utils\scripts\tools
call %TOOLSDIR%\logger.bat DOOR FRONT OPEN "The Front door was opened"

And here is what it means:
  • @echo off
    Don't show me what you are doing when you are running
  • set TOOLSDIR=D:\X10_Utils\scripts\tools
    Since you are going to be calling logger.bat (see below) this script needs to know where that file is. replace "D:\X10_Utils\scripts\tools" with the directory where you put logger.bat.
  • call %TOOLSDIR%\logger.bat DOOR FRONT OPEN "The Front door was opened"
    If you recall from above, I wanted 6 fields in the spreadsheet, but there are only 4 options after logger.bat.  Date and Time are generated automatically.  The four options after logger.bat are put into fields 3, 4, 5, and 6 in the log.

So when run, it calls logger.bat and tells logger.bat what info to use.  Here is the logger.bat file, with comments inline as to what it us doing.
   
Code: [Select]
@echo off

:: ###########################################################################
::                 X10 Command Logger
::
:: Author: Bob Perciaccante (orcusomega@gmail.com)
:: Purpose: To give the basic ability for ActiveHome to
::          log events seen in macros, etc to a CSV file
::          that can later be reviewed, edited, audited, etc
::
:: Example Use Case: Log when doors are opened, etc
::
::
:: ###########################################################################

:: ###########################################################################
:: Take the command line options and assign them to the basic variables.
:: Usage: logger.bat [option 1] [option 2] [option 3] [option 4]
::    Option 1: Contents assigned to Field1
::    Option 2: Contents assigned to Field2
::    Option 3: Contents assigned to Field3
::    Option 4: Contents assigned to Field4
:: ###########################################################################

set FIELD1_VAR=%1
set FIELD2_VAR=%2
set FIELD3_VAR=%3
set FIELD4_VAR=%4

:: ###########################################################################
::  Define basic parameters.
::   LOGDIR: The output directory you want the log to be placed in.
::   LOGFILE: The name of the output file
::   DELIMETER: The character you want to seperate fields with in the log.
:: ###########################################################################

set LOGDIR=D:\\X10_Utils\\logs
set LOGFILE=%LOGDIR%\\home.log
set DELIMETER=,

:: ###########################################################################
::  Define how date and time formatting will be handled
:: ###########################################################################

set CURDATE=%date:~10,4%-%date:~4,2%-%date:~7,2%
set CURTIME=%time:~0,8%

:: ###########################################################################
::  Output format is currently 6 columns.
::    COLx_LABEL: This is the heading line of the log file, name the
::                  columns here
::
:: ###########################################################################

set COL1_LABEL=DATE
set COL2_LABEL=TIME
set COL3_LABEL=TYPE
set COL4_LABEL=LOCATION
set COL5_LABEL=ACTION
set COL6_LABEL=DESCRIPTION

:: ###########################################################################
:: See if the log file exists, and if not, create it and populate the headings
:: ###########################################################################

if not exist %LOGFILE% (
echo %COL1_LABEL%%DELIMETER%%COL2_LABEL%%DELIMETER%%COL3_LABEL%%DELIMETER%%COL4_LABEL%%DELIMETER%%COL5_LABEL%%DELIMETER%%COL6_LABEL% > %LOGFILE%
)

:: ###########################################################################
::  Take all the variables assigned above and decide the order you want
::    them to be entered in the log
:: ###########################################################################

set COL1=%CURDATE%
set COL2=%CURTIME%
set COL3=%FIELD1_VAR%
set COL4=%FIELD2_VAR%
set COL5=%FIELD3_VAR%
set COL6=%FIELD4_VAR%

:: ###########################################################################
::  Take the information and write it to the log file.
:: ###########################################################################

echo %COL1%%DELIMETER%%COL2%%DELIMETER%%COL3%%DELIMETER%%COL4%%DELIMETER%%COL5%%DELIMETER%%COL6% >> %LOGFILE%

So when this runs, you get a log file that looks like this (obviously I am using more than just a front door):

Code: [Select]
DATE,TIME,TYPE,LOCATION,ACTION,DESCRIPTION
2011-01-09,14:19:02,DOOR,BACK,OPEN,"The back door was opened"
2011-01-09,14:19:08,DOOR,BACK,CLOSE,"The back door was closed"
2011-01-09,14:20:02,DOOR,BACK,OPEN,"The back door was opened"
2011-01-09,14:20:55,DOOR,BACK,OPEN,"The back door was opened"
2011-01-09,14:21:01,DOOR,BACK,CLOSE,"The back door was closed"
2011-01-09,14:23:47,DOOR,FRONT,OPEN,"The Front door was opened"
2011-01-09,14:24:05,DOOR,FRONT,CLOSE,"The Front door was closed"
2011-01-09,14:24:15,DOOR,LAUNDRY,OPEN,"The door from the laundry room to the garage was opened"
2011-01-09,14:24:23,DOOR,LAUNDRY,CLOSE,"The door from the laundry room to the garage was closed."
2011-01-09,14:27:42,DOOR,BACK,OPEN,"The back door was opened"
2011-01-09,14:28:01,DOOR,BACK,CLOSE,"The back door was closed"
2011-01-09,14:28:21,DOOR,LAUNDRY,OPEN,"The door from the laundry room to the garage was opened"
2011-01-09,14:39:37,DOOR-GARAGE,AMY,OPEN,"Amy's garage door was opened"
2011-01-09,14:53:00,DOOR,LAUNDRY,OPEN,"The door from the laundry room to the garage was opened"
2011-01-09,14:53:03,DOOR,LAUNDRY,CLOSE,"The door from the laundry room to the garage was closed."
2011-01-09,15:12:17,DOOR,BACK,OPEN,"The back door was opened"
2011-01-09,15:12:23,DOOR,BACK,CLOSE,"The back door was closed"
2011-01-09,15:12:36,DOOR,BACK,OPEN,"The back door was opened"

So yes, this is LONG winded, but I hope that someone can benefit from it.  I am using this information to start a MySQL logging application to tie into a web-app, that allows me to see what the state of the sensor, etc is (as far as AHP knows).

I have attached the files in a ZIP file to this post (just remove the .txt extension - the forum doesnt allow me to attach a straight ZIP file)

Comments welcome!

Bob
Logged

lviper

  • PI Expert
  • Hero Member
  • ******
  • Helpful Post Rating: 3
  • Posts: 257
Re: [Advanced] Monitoring and logging actions
« Reply #1 on: January 11, 2011, 03:15:19 PM »

Very interesting. I like this and I can see a lot of potential uses with it.  #:)
Logged

orcusomega

  • Sr. Member
  • ****
  • Helpful Post Rating: 6
  • Posts: 97
Re: [Advanced] Monitoring and logging actions
« Reply #2 on: January 11, 2011, 03:18:14 PM »

Believe it or not, it was harder to make it so other people could understand what I was doing - the first version was VERY short :)

I am working right now with a few similar scripts, and VBS email scripting, to be able to do all sorts of things.  I saw in another post somewhere where someone was using scripting to control PC's - I am working on something similar, but I need more housecodes and unit codes! :)

Bob
Logged

-Bill- (of wgjohns.com)

  • Advanced Member
  • Hero Member
  • ******
  • Helpful Post Rating: 81
  • Posts: 1428
  • He's just this guy. You know?
    • wgjohns.com
Re: [Advanced] Monitoring and logging actions
« Reply #3 on: January 11, 2011, 09:47:35 PM »

I am working right now with a few similar scripts, and VBS email scripting, to be able to do all sorts of things.  I saw in another post somewhere where someone was using scripting to control PC's - I am working on something similar, but I need more housecodes and unit codes! :)

Actually my first version of BVC called BXVC was written entirely in JScript with Hypertext Application code for the display!   :)%
 >!
Logged
-Bill- (of wgjohns.com)
bill@wgjohns.com

In the real world, the only constant is change.

When I'm online you can find me in the Home Automation Chat Room!

Noam

  • Community Organizer
  • Hero Member
  • ***
  • Helpful Post Rating: 47
  • Posts: 2721
Re: [Advanced] Monitoring and logging actions
« Reply #4 on: January 12, 2011, 09:43:25 AM »

If the developers can figure out how to give us the ability to pass command line parameters, that will certainly make your job a lot easier.
Logged

orcusomega

  • Sr. Member
  • ****
  • Helpful Post Rating: 6
  • Posts: 97
Re: [Advanced] Monitoring and logging actions
« Reply #5 on: January 12, 2011, 11:26:08 AM »

Ya know, I feel like a bit of a tool - I tagged this Advanced since I figured it was more than most people would bite off, but forgot how technical the audience here is :). I should have named it "Advanced for Some" :)

Bob
Logged

-Bill- (of wgjohns.com)

  • Advanced Member
  • Hero Member
  • ******
  • Helpful Post Rating: 81
  • Posts: 1428
  • He's just this guy. You know?
    • wgjohns.com
Re: [Advanced] Monitoring and logging actions
« Reply #6 on: January 12, 2011, 09:33:14 PM »

Ya know, I feel like a bit of a tool - I tagged this Advanced since I figured it was more than most people would bite off, but forgot how technical the audience here is :). I should have named it "Advanced for Some" :)

Bob

Actually, you were quite right.

I would venture to say that most of the users here have no idea how to write batch files or script.

It's nice to see someone else using the more advanced batch file stuff for a change too! :o
 >!
Logged
-Bill- (of wgjohns.com)
bill@wgjohns.com

In the real world, the only constant is change.

When I'm online you can find me in the Home Automation Chat Room!

thejackal

  • Full Member
  • ***
  • Helpful Post Rating: 0
  • Posts: 33
Re: [Advanced] Monitoring and logging actions
« Reply #7 on: January 16, 2011, 02:32:40 PM »

Thanks for the write up. Not sure what I'll use it for... YET!  -:)  I'm always looking for new things to make the house do.  :)+ 
Logged
 

X10.com | About X10 | X10 Security Systems | Cameras| Package Deals
© Copyright 2014-2016 X10.com All rights reserved.