HowTo: openHAB2 RSS Feed + CometVisu

Hi all,

I wanted to see all important events from openhab in a CometVisu view, that’s why I found a RSS feed quite usefull. Maybe you’ll find it usefull, as well?
As there is no binding or action available to feed a RSS from openhab, I decided to do it my way. It’s just a little perl script, which produces the RSS feed. The feed themself will be served by the openhab jetty (no extra webserver necessary). You can style your feed by a rss.css.

OK, let’s start!
(assumed you installed openhab by apt package manager,otherwise the paths my be different)

Logger
First of all, you need to enhance the openhab logger, to create a new log file. Copy this at the end of /var/lib/openhab2/etc/org.ops4j.pax.logging.cfg

# Logger - RSS.log
log4j.logger.org.eclipse.smarthome.model.script.RSS = INFO, RSS
# File appender - RSS.log
log4j.appender.RSS=org.apache.log4j.RollingFileAppender
log4j.appender.RSS.layout=org.apache.log4j.PatternLayout
log4j.appender.RSS.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss.SSS} [%-5.5p] [%-36.36c] - %m%n
log4j.appender.RSS.file=${openhab.logdir}/RSS.log
log4j.appender.RSS.append=true
log4j.appender.RSS.maxFileSize=20KB
log4j.appender.RSS.maxBackupIndex=5

This will produce the log file /var/log/openhab2/RSS.log and takes care it is rotated, when the file exeeds 20KB (you can change this if needed). The log file shouldn’t be too long, as old messages are barely wanted.

Fill your rules with instructions like this

logInfo ("RSS", "this is your message")

and take care that the subpackage is called “RSS”, otherwise your /var/log/openhab2/RSS.log remains empty.
The next step shouldn’t be necessary, but in case you play with the log level in karaf, take care your loglevel isn’t above INFO (as we use logInfo here, but you might change this as well):

ssh openhab@localhost -p8101
# password: habopen
log:set INFO org.eclipse.smarthome.model.script.RSS loglevel 

In the next step you need to generate three files:
1. /etc/openhab2/scripts/rss_generator.pl
2. /etc/openhab2/html/RSS/rss.css
3. /etc/cron.d/openhab

Take care, that all files in /etc/openhab2 directory, which will be created are readable by openhab.

rss_generator.pl:

#!/usr/bin/perl
 
# Usage:
# ./rss_generator.pl
#   inputfile:  $logbase/$filename.log
#   outputfile: $ohbase/$filename.rss
 
# dependecies:
# install: apt-get install libxml-rss-simplegen-perl
 
 
use strict;
use XML::RSS::SimpleGen;
use utf8;
use open ':utf8'; # opens file in utf-8 for german umlauts
 
my $url      = 'http://myopenhab.org/';
my $email    = 'michael.pophal@web.de';
my $ohbase   = '/etc/openhab2/html/RSS';
my $logbase   = '/var/log/openhab2';
my $filename = 'RSS';
 
rss_new( $url, "openHAB News", "Meldungen und Alarme von openHAB" );
rss_allow_duplicates(1);
rss_language( 'de' );
rss_webmaster( $email );
rss_history_file( "$ohbase/${filename}.dat" );
# This sets the parameter, which suggests how long in minutes
# an RSS reader should wait after it polls a feed again.
rss_ttl(5);
rss_updateBase('2000-01-01T00:00-00:00');
rss_updateFrequency(12);     # update 12 times an hour (every 5minutes)
rss_updatePeriod('hourly');  # 2*hourly means "twice an hour"
rss_item_limit(0);           # set the number of items unlimited
 
# read logfile and sort order revers, so that
# last logline is on top
open(IN, "$logbase/$filename.log") or die "Fehler open()  $!";
my @zeilen_rueckwaerts = reverse <IN>;
close(IN) or die "Fehler close() $!";
 
foreach (@zeilen_rueckwaerts) {
  $_ =~ s/\[INFO.*RSS\]//;
  rss_item($url, $_);
}
 
die "No items in this content?! {{\n$_\n}}\nAborting"
 unless rss_item_count();
 
rss_save( "$ohbase/$filename.rss", 45 );
exit;

rss.css (please adapt it at your needs):

    * {
  display: block;
  border-top:  0px groove #000060;
  border-left: 0px groove #000060;
  padding: 2px 2px 2px 15px;
}
 
link:before            { content: "\00a0\00a0 URL:\00a0\00a0\00a0\00a0 " }
webMaster:before       { content: "\00a0\00a0 Feed admin: " }
/* lastBuildDate:before   { content: "\00a0\00a0 Last feed update: " } */
updateFrequency:before { content: "\00a0\00a0 Update Frequency: " }
updateFrequency:after  { content: " times" }
updatePeriod:before    { content: "\00a0\00a0 Update Period: " }
updateBase:before      { content: "\00a0\00a0 Update Base: " }
language:before        { content: "\00a0\00a0 Language: " }
docs:before            { content: "\00a0\00a0 Read me: " }
description:before     { content: "\21D2  "}
 
docs {  color: #fcc; }
 
image, guid, ttl, skipHours, skipDays, updateBase, link, description, docs, webMaster, updateBase, updateFrequency, updatePeriod, language, :root:before { display: none }
 
channel description { font-size: 150%; }
item    description { font-size: 110%; }
 
:root:before {
 content: "\2022\00a0\00a0\00a0 Don't panic.  This data file is meant to be read in an RSS reader.\00a0\00a0  See http://interglacial.com/rss/about.html  \00a0\00a0\00a0\2022";
}
 
:root {
  border: 0;
  padding: 0;
  padding-top: .7em;
  margin: 2px;
  font-size: 9pt;
  display: block;
  background-color: #303331;
  color: white;
}
 
channel {
  border:  0;
  padding: 0;
  margin-top: .0em;
}
 
channel > title {
  font-size:  110%;
  font-style: italic;
}
 
item {
  padding: 0;
  border: 0;
  border-left: 5px solid #80d0d0;
  margin: 2px 0 2px 0;
}
 
item > * { border: 0; }
 
item > title {
  font-size: 150%;
}
 
item > link {
  font-size: 80%;
  margin-top:  6px;
  margin-bottom: 6px;
}

cron file openhab:

# openhab2 jobs
#
 
# generate RSS from RSS.log, cd in directory, then execute ./rss_generator.pl
# otherwise the rss.css wouldn't be found
*/5 * * * *     openhab     cd /etc/openhab2/scripts ; ./rss_generator.pl

To make the perl script run, you need XML::RSS::SimpleGen. Exeuting the following commands, should fo the trick:

# install dependency
apt-get update
apt-get install libxml-rss-simplegen-perl

# copy rss_generator.pl to right place
cp rss_generator.pl /etc/openhab2/scripts
chmod 750 /etc/openhab2/scripts/rss_generator.pl
chown openhab:openhab /etc/openhab2/scripts/rss_generator.pl

# create the webdiectory, where the RSS feed will be hosted
mkdir -p /etc/openhab2/html/RSS

# copy rss.css to right place
cp rss.css /etc/openhab2/html/RSS
chown -R openhab:openhab /etc/openhab2/html/RSS
# this symbolic link is needed! (see XML::RSS::SimpleGen)
cd /etc/openhab2/scripts
ln -s /etc/openhab2/html/RSS/rss.css

# copy the cronjob to the right place
cp openhab /etc/cron.d

As long openhab creates entries (use logInfo in rules) in the RSS.log the perl script should create the RSS feed, which is located under http://your_ip:8080/static/RSS/RSS.rss

CometVisu
The last step is, to include this RSS in your CometVisu (if you have). Just insert in your /etc/openhab2/html/cometvisu/config/visu_config.xml

<group name="openHAB RSS Feed" align="center">
    <layout colspan="6"/>
    <web src="http://your_ip:8080/static/RSS/RSS.rss" frameborder="no" scrolling="yes" refresh="60" height="500px"/>
</group>

at the right place and you should see something like this:

Troubleshooting:

  1. take care that every file is readable by your runtime user, which should be openhab
  2. execute this: cd /etc/openhab2/scripts ; ./rss_generate.pl.
    In case of errors: is XML::RSS::SimpleGen installed? Is the perl script executable? Did you use other paths?
    3.if your RSS feed doesn’t look like the screenshot, is your rss.css at the right place? Did you create the symlink?
  3. does your cronjob work? Execute grep 'rss_generator.pl' /var/log/cron.log and see, if the scipt is executed every 5 minutes.

Regards,
Michael

4 Likes

Thanks for this wonderful tutorial.

Hi staehler,

is it somehow possible to use again the cometvisu rsslog plugin? This was looking quite nice and an rss.css would be also not required anymore.

best regards René

Hi René,
that’s because I made my own RSS feed, I didn’t find a way to use rsslog. Please ask in the KNX Forum for further assistance in CometVisu.
When you get it running, would be nice to leave a little tutorial :wink:
In /etc/openhab2/html/cometvisu/plugins/rsslog.php you’ll find:

/*****************************************************************************/
/* rsslog.php - A simple log message reciever and sender via RSS             */
/*                                                                           */
/* (c) 2011 by Christian Mayer                                               */
/* Licenced under the GPLv3                                                  */
/*****************************************************************************/

// There are diffentent modes of operation
// 1. Creating a new log line:
//    URL parameter "c":   the content of the log
//    URL parameter "t[]": a tag for later filtering. Multiple might be given,
//                          separated by COMMA, usage &t[]=tag1,tag2
//    URL parameter "h":   a header(title) for the entry; maybe empty
//    URL parameter "state": (optional) state
//    URL parameter "mapping": (optional) mapping - used for displaying
// 2. Receive the log as RSS:
//    URL parameter "f":   The (optional) filter, only log lines with a tag
//                         that fit this string are sent
//    URL parameter "state": get only rows with state=value
//    URL parameter "limit": only get the latest "limit" entries
// 3. Dump all the content in a HTML page:
//    URL parameter "dump" - no value needed
// 4. Remove old content:
//    URL parameter "r":   the timestamp (seconds since 1970) of the oldest log
//                         line to keep
//    URL parameter "f":   The (optional) filter, only log lines with a tag
//                         that fit this string are deleted
// 4a. Remove single line:
//    URL parameter "d":   id of row to delete
// 5. Get content as JSON:
//    URL parameter "j"
// 6. Update state:
//    URL parameter "u" id of row
//    URL parameter "state": new state

Kind regards,
Michael

Hi Michael,
thanks for the tutorial. Worked almost out of the box. I guess due to OH2

I had to adapt the logger config…

# RSS Logger
log4j2.logger.RSS.name = org.eclipse.smarthome.model.script.RSS
log4j2.logger.RSS.level = INFO
log4j2.logger.RSS.additivity = false
log4j2.logger.RSS.appenderRefs = rss
log4j2.logger.RSS.appenderRef.rss.ref = RSS

# File appender - RSS.log
log4j2.appender.RSS.type = RollingRandomAccessFile
log4j2.appender.RSS.name = RSS
log4j2.appender.RSS.layout.type = PatternLayout
log4j2.appender.RSS.layout.pattern = %d{yyyy-MM-dd HH:mm:ss.SSS} [%-5.5p] [%-36.36c] - %m%n
log4j2.appender.RSS.policies.type = Policies
log4j2.appender.RSS.policies.size.type = SizeBasedTriggeringPolicy
log4j2.appender.RSS.policies.size.size = 40KB
log4j2.appender.RSS.fileName = ${openhab.logdir}/RSS.log
log4j2.appender.RSS.filePattern = ${openhab.logdir}/RSS.log.%i
log4j2.appender.RSS.immediateFlush = true
log4j2.appender.RSS.append=true

And if you wish, you can use a relative path if you run cometvisu on the same host as OH.

<group name="openHAB RSS Feed" align="center" nowidget="true">
  <layout colspan="6"/>
  <web src="/static/RSS/RSS.rss" frameborder="no" scrolling="yes" refresh="60" height="500px"/>
</group>

BR,
Alex