Example Scripts

Description

A place where scripts developed by users can be shared. Please make sure that all submissions are well commented.

Here is a simple script that demonstrates writing data to Sen.se.  There should be enough comments in the script to get you going, you'll need to insert you API key and feed id.  You will also need a Sen.se account, you can request one on their web site, or I can give you an invite, although I only have 5 at the moment.

 

EDIT: Thanks for the pointer Brett, I've updated the script to remove the use of the depreciated global frame and removed some unnecessary conversion.  The updated script is attached.

I have several temperature/humidity sensors located in my house. The are well-known lacrosse TX3-TH, transmitting data on 433 mhz. I have a simple arduino with 433 mhz receiver that receives and decodes the signals.

lacrosse arduino

Arduino is attached to serial port of HAH and feeds serial port with data in the form

id=505,type=t,value=24 (sensor with id 505 transmitted temperature with value of 24 centigrade)

or

id=900,type=h,value=24 (sensor with id 505 transmitted humidity with value of 24%)

Attached sript creates BSC endpoints and updates them from the serial (arduino) data. Endpoints can be displayed in xapflash

th data in xapflash

I am using a HAH with an Envi Current Cost meter and 2 clamps to measure the amount of electricity being generated, and the amount being used. If there is spare capacity I switch on one or two 500W heaters wirelessly using the HAH.

If I switch the kettle on or the dishwasher heater kicks in, the water heaters immediately turn off and will come on again a minute or so after the load has dropped back to a background level.

The system works brilliantly and I anticipate being able to heat most of my hot water over the next 6 months for free!

If anyone is interested in any other details of this system I am more than happy to share,

James.

My house lighting is controlled by a PLC that is not a xAP device at all. I've managed to write a custom xAP stack for it (in CodeSys ST language), but still - it was not accepting broadcast messages like 192.168.1.255. To solve this I created a simple lua applet that intercepts broadcast messages to the specific target (khome.PLC.41) and sends UDP datagram directly to PLC IP (192.168.1.50)

 

Long Post....

xAP Intranet applications have for many years been at the core of my Home network. xAPIntranet integrates multiple webservers, which can be on multiple hosts into one entity.

These hosts are windows based, and I know that there is a school of thought which says that windows is not suitable for Home Automation, which I understand, but for me xAPIntranet has always been about information first, any 'control' aspect being secondary or non critical

The principal applications in my intranet have been xAPSwitchboard which monitors my landline phone, xAPNews which collects my RSS news feeds into one place and xAPLibrary which collates my mainly pdf documents and manuals.

The xAPIntranet applications have a common header for each webserver which allows you to access the other webservers, and a "Groups" page which gives access to xAPintranet applications not in the "Home" Group as well. In addition there is a task bar based menu system called Intranet Launcher to allow easy access to each application as well

xAPIntranet Applications originate from James at mi4.biz (http://www.mi4.biz/modules.php?name=Content&pa=showpage&pid=39) and are based on the xAPIntranet ocx which I used to create some applications for myself. Some of these apps may no longer work, thinking about  TV in particular, because the data it relied on has changed.

This integration is achieved by each app broadcasting a periodic 'Web.Service" message:-

xap-header
{
v=12
hop=1
uid=FF555600
Class=Web.Service
Source=netcomp.Library.black1
}
Server.Start
{
Name=Library
Desc=xAPIntranet Document Library
Group=Home
PC=BLACK1
Config=yes
ConfigURL=http://192.168.253.100:51002/config.xsp
Icon=http://192.168.253.100:51002/images/icon.gif
URL=http://192.168.253.100:51002/
}

For convenience, I wanted to integrate my Livebox and the xAPFlash GUI hosted on the Livebox into my xAPIntranet setup. This could not be full , two way, integration since this would require modification to the HAH Web server, but access to HAH and xAPFlash from xAPIntranet could be achieved relatively easily with a couple of plugboard scripts and some icons.

In this screendump is the front page of the News application with clickable icons for the different xAPIntranet application that are running in the blue banner on the right. These icons are specified in the web service message.
From left to right these are xAPFlash, xAPSwitchboard, QUasar K3145 connector, Intranet, Library and WDLive. These applications are all in the Home group ( see the home key= above), to access xAPIntranet applications in other groups click on the + sign and you get an icon based menu as shown below

In the System group are the Livebox Web server, WXLink and another version of Intranet called Infrastructure. All full xAPIntarnet apps would have a page like this, but with diffrent background images.

To achieve this a Web.Service message is broadcast by two plugboard scripts once every 5 minutes


xap-header
{
uid=FF00D800
source=dbzoo.livebox.Plugboard
hop=1
class=Web.Service
v=12
}
server.start
{
config=no
name=LiveboxGUI
configurl=
group=hOME
url=http://192.168.253.5/flashgui.kl1
icon=http://192.168.253.5/live/xapflash.gif
desc=xAPFlash on HAH
}

 and

xap-header
{
uid=FF00D800
source=dbzoo.livebox.Plugboard
hop=1
class=Web.Service
v=12
}
server.start
{
config=no
name=Livebox
configurl=
group=System
url=http://192.168.253.5/automation.kl1
icon=http://192.168.253.5/live/hahlogo.gif
desc=Livebox Home Automation Hub
}

I confess to being a little artisticaly challenged, I have filched and modifed the icons from the HAH, and dbzoo websites, you could use these or make up your own

In addition to the xAPIntranet web servers there is also a taskbar resident menu application called IntranetLauincher which recognises the web.service messages and produces a pop up menu for applications in the home group

 

Easier to do than describe...

kevint

 

Here are some lua scripts which pick up xAP display messages and forward them to twitter

The message schema being monitored are Message.Display and xAP-OSD.Display which are used in my setup for caller display messages and other alerts.

I should explain my twitter setup. My house has a separate twitter account, 'kevins house', to my personal account and it is this account which the HAH twitter connector is configured and authorized for. 'kevinshouse' is a protected twitter account so its messages are not publicly available and only people I authorise can see the messages. Obviously, my personal account is authorised.

Although the HAH Twitter connector can action  commands, I currently don't plan to use this feature.

If you don't have twitter on your phone, you can setup your account so  that you get a text message (mobile notification) when a tweet from the 'house' account arrives

My primary usage will be for the Caller ID messages from xAP Switchboard, and it was this script, TweetPhoneApplet.lua, which I made first. It can send messages using a number of schema, I chose to use xAP-OSD.Display, a typical message looks like

xap-header
    {
    v=12
    hop=1
    uid=FF111300
    Class=xAP-OSD.display
    Source=mi4.switchboard.BLACK1
    }
    Display.SLIMP3
    {
    Line1=Incoming  Call : Kevin
    Line2=Mobile Telephone, 07871 700000
    Duration=20
    Brightness=Brightest
}

this results in my account getting a tweet like this

This script specificaly checks for xAP-OSD.display messages from mi4.switchboard.BLACK1 (vendor.application.pcname)

To ensure that two consecutive calls from the same number are recognised by twitter (it won't repeat identical messages), each message is prefixed by the time. I have also added a  suffix message, you have to do the tone singing in your head....

Recognising that not many of you will have xAP Switchboard, the second script, TweetOSDApplet.lua, is more general and will tweet any xAP-OSD.display message, without the suffix.

The third script,TweetMessageApplet.lua, deals with message.display schema messages

Tweets are limited to 140 characters. I have not investigated/implemented how to limit the message length coming from these scripts. If the message exceeds 140 characters then twitter, if not HAH Twitter, will not pass them on

Note that my HAH is configured for an 'Instance' of '1', so you may want to change the references in the scripts of target=dbzoo.livebox.1.Twitter and dbzoo.livebox.1.Plugboard to match your setup

Each of the scripts includes example xAP messages in the header comments.

kevint

 

Based on bretts script "Sending EMAIL (adv)" I have created a script which emails me with phone number and caller id of any calls to my landline phone number.

I should explain that this monitoring of the phone line is not an HAH function but is carried out by the Switchboard(mi4) and xAPtel(UKUSA) on dedicated HA PC on my home network. I have been monitoring my phone line for many years and the system works well.

Briefly, when a phone call is received, xAPtel monitors a meteor caller ID device and sends a xAP message to switchboard, which looks up the number and carries out a number of actions including sending a xAP-OSD.display message like this

xap-header
{
v=12
hop=1
uid=FF111300
Class=xAP-OSD.display
Source=mi4.switchboard.BLACK1
}
Display.SLIMP3
{
Line1=Incoming  Call : Kevin  (Mobile)
Line2=Mobile Telephone, 07871 7946221
Duration=20
Brightness=Brightest
}

The plugboard script, htmlEmailPhoneApplet.lua, sees this xAP message and sends an SMTP email formatted using the message template,phone.tmpl,  to a mail server.

My main email source is gmail which does not accept smtp, so I send it to another email server and gmail collects it from there. As before lte.lua is also required in the plugboard folder

So now when I am away from home, I get an email on my phone telling me when someone has called and who they are.

 

And in case you are wondering, that phone number is not my real phone number, so dont go ringing it expecting to get me!

Configuration

In htmlEmailPhoneApplet.lua you will need to change

3 instances of user@somedomain.co.uk>
- to = "user <user@somedomain.co.uk>",
- from="<user@somedomain.co.uk>",
- rcpt="<user@somedomain.co.uk>",

and

user="user@domain.co.uk",
password="password",
server="smtp.domain.co.uk",
port=587

plus

f:add("xap-header","source","mi4.switchboard.BLACK1")

to the appropriate values for your setup

Thanks to brett for the original script

Note that lte.lua is unchanged from the original script, and it would be shared by other applets

kevint

I have been exploring lua and plugboard and creating some scripts, here is one that calculates the carbon equivilant of the electricity consumed with a 1 minute update.

The source of the data in this script is ch.1 on a currentcost device:-

dbzoo.livebox.1.CurrentCost:ch.1

Note that I have an instance of "1" configured and that if you do not use this configuration you will have to change this.

If you use more than one channel then you need to change to use ch.total and if you are playing with the beta with the eco manager support then this would become ch.H

The factor is a 5 year rolling average factor for total CO2 equivilant is derived from the "August 2011 Guidelines to Defra/DECC’s Greenhouse Gas Conversion Factors for Company Reporting" available from http://archive.defra.gov.uk/environment/business/reporting/pdf/110819-guidelines-ghg-conversion-factors.pdf .

The relevant factors are in table 3c of Annex 3. I am using the data for 2009 Grid Rolling Average, Total Direct GHG, ie 0.52462

As you will see in the table this gives factors for the components, ie CO2, CH4, and N20 as well, but when I tried to add those extra endpoints to the script it failed

The resulting data is a subuid of plugboard and I post this to pachube on feed 1867, datastream 1. (I never could get the AMEE calcs to work properly)

My lack of experience with lua prevents me from adding the component calculations and in addition I get at least one bsc message with ? and at least one with a 0 value on startup. In addition I had problems dividing by 1000 (!) and had to kludge it ,so any help or sugestions with these issues will be most welcome.

 kevint

 

Here's a script (in applet form) to count gas use from a Schlumberger R5 gas meter using a CC dev board attached to the RJ11 socket on the underside of the digital display.

The CC sensor (sensor.1 in my case) alternates between 0 and 500 watts output depending on the state of the reed switch in the meter. This script just counts up and records the pulses on the livebox. The switch opens/closes once per cu ft of gas consumed.

 

 

_______________________________________________________________________________________________________

require ('xap')
require('xap.bsc')


require("string")
local io = require("io")



-- Reader for a Schlumberger R5 gas meter using a CurrentCost digital development board and an HAH

module(...,package.seeall)

info={
version="1.0", description="Gas Counter"
}


Postinterval=5
tempcount=0
gas=0




function init()
local f = xap.Filter()
f:add("xap-header", "source", "dbzoo.livebox.CurrentCost:sensor.1")
f:add("xap-header","class","xAPBSC.event")
f:callback(gaslog)
end


function gaslog(frame)
gas = frame:getValue("input.state","text")
-- When the sensor toggles to 500 the reed switch has incrememted by 1 cu ft
if tonumber(gas) == 500 then
tempcount=tempcount+1
end
end

function gassave(self)
-- save the gas meter count at 5 minute intervals to mitigate data loss from an outage
local file = io.open("/etc/plugboard/gascuft","r")
cumulativegas = file:read("*all")
file:close()
cumulativegas = tonumber(cumulativegas) + tempcount
tempcount = 0
local cmd = string.format("echo '%s'>/etc/plugboard/gascuft", cumulativegas)
os.execute(cmd)
self:reset()
end

xap.Timer(gassave, Postinterval*60):start()
__________________________________________________________________________________________________________

I haven't yet done anything with the data but obviously posting to pachube or similar would be possible with a little work.