Export Device Events?

Given there is currently no way to access device events from a dashboard, is there a way to access those events from an external source or export them in some manner? I heavily rely on the ability to view "Recent Events" in ST when I'm away from home. (e.g. Did someone open the back door and let the dogs out). I could obviously gather some info by notifications but I prefer a "pull" process instead of "push" in some scenarios.

If you're familiar with nodejs you can run this app I just coded for listening to the event stream. Just note that this will only show them as they happen:

Save the code as export-event.js and run it like this:

To watch them live:
nodejs export-event.js YOURHUBIP

To save them to a file:
nodejs export-event.js YOURHUBIP > events.js

If you don't have websocket, I believe you can install it like this
npm install websocket

var WSClient = require('websocket').client;

var client = new WSClient();

var eventMap = {};

client.on('connectFailed', function(err){
        console.log('Connect Error: ' + err.toString());
});

client.on('connect', function(connection){
        //console.log('Client connected');
        connection.on('error', function(err){
        //      console.log('Connection Error: '+ err.toString());
        });
        connection.on('close', function(){
        //      console.log('Connection closed');
        });
        connection.on('message', function(message){
                var event = JSON.parse(message.utf8Data);

                if(typeof event.isStateChange != 'undefined'){
                    if(event.isStateChange){
                        console.log(JSON.stringify(event));
                    }
                    return;
                }

                var name = event.name;
                var value = event.value;
                var source = event.source;

                var id = source == "DEVICE" ? event.deviceId :
                    source == "HUB" ? event.hubId : event.locationId;

                var sourceMap = eventMap[source] = eventMap[source] || {};

                var evt = sourceMap[id] = sourceMap[id] || {};

                if(evt[name] == value){
                    return;
                }
                else {
                    evt[name] = value;
                    event.date = new Date().getTime();
                }

                console.log(JSON.stringify(event));
        });
});

client.connect('ws://' + process.argv[2] + '/eventsocket');

It's also not hard to do this in node-red and connect it up to mongodb or have an endpoint to list the latest events in json format, which is what I do.

4 Likes

Thanks for the write up. I got this going on my raspberry pi just to test the functionality. A few questions.

  1. Is there an easy way to have the export file split once it reaches a certain size or after a certain time period.
  2. If above is possible, can it also be configured so that it automatically purges older files.
  3. What application would you use to view the output.

Essentially I want to keep a few days of logs and have this self manage. I figured I could google some of this but was curious to know how you handle your log output.

  1. You can probably use something like simple node logger or winston-log-rotate
  2. Same libraries probably have purge abilities, but I haven't had experience with them.
  3. I actually use node-red to connect to the socket, parse the logs and stick them in mongo db. I have another http node that will read the last 1000 events and display them in JSON format, but in that intro tutorial they talk about http templates to make data more user friendly.

I don't look at event history too much so I've stuck to viewing them in their raw format. You could also potentially make the event.js have a valid JSON array syntax (brackets [] and a "," between the events) so it could be used on a html page that can parse them in something like a datatable.

2 Likes

Thanks. I'll take a look at all of the above.

1 Like

I know nothing about node.js, but Google is great...:wink:

Now I am thinking about installing node.js on my Windows PC, use the above code to retrieve events, save the data in a SQLite database, then pull maybe the current day or two worth of data and format into an html page using Handlebars (the first templating library I ran across). I think a link to that html page could be added to the dashboard, but would only work locally.

You could also put in some purge logic to remove records from the database after a certain amount of time.

I have used SQLite in my Windows programming, and I like it a lot.

1 Like

The Maker API exports events via a POST URL. See https://community.hubitat.com/t/simple-event-graphing-using-linux-tools-and-gnuplot/94665

This thread is 4 years old, I am fairly certain that this was solved a long time ago.

2 Likes

The search engine finds these old threads. Without a closing comment at the end of the thread saying what the solution was, it looks like the problem remains unsolved.

This thread has 10 posts, including 2 by you and 1 by me.

It took me less than 30 seconds to scroll through it and determine that post #2 was marked as a Solution.

And even if someone missed that, this pop-up in the editor indicates the topic has been solved:

2 Likes

I had additional details, and the posted solution didn't work for me because I didn't want or need to install any of nodejs, websocket, or node-red on my server. The Maker API event export is a clean alternative that doesn't need me to install any of that server-side stuff.