Background
A friend asked if it was possible to collect device states and capture them into a database. I said "I don't know, let me check." and then a few days later this small project was born.
Project Scope
-
Via the Maker API, collect events emitted from Hubitat and store into a PostgreSQL Database.
-
Allow application to run in Docker for ease of setup.
GitHub Repository
https://github.com/entmike/hubitat-datacollector
Small NodeJS project to listen for Hubitat Maker API POST events and capture them to a PostgreSQL database.
Try it Now with Docker
If you don't care about local development and just want to run it, see the example below.
Pre-requisites:
- Docker Installed
- PostgreSQL Installed somewhere (physical host, VM, Docker, whatever) with a new DB created (i.e.
hubitat
) -
events
table created in PostgreSQL DB. Create statement for your convenience:
CREATE TABLE IF NOT EXISTS events (
name varchar( 255 ) NOT NULL, value varchar( 255 ) NOT NULL, displayName varchar( 255 ) NOT NULL,
deviceId varchar ( 255 ) NOT NULL, descriptionText varchar( 255 ), unit varchar( 255 ), type varchar( 255 ),
data varchar( 255 ), timestamp TIMESTAMP DEFAULT NOW());
Example:
docker run --rm -ti \
-e PGHOST=yourpostgreshost -e PGUSER=postgres -e PGPASSWORD=YourPassword \
-e PGDATABASE=hubitat -e PGPORT=5432 -p 3000:3000 entmike/hubitat-datacollector
- In Hubitat, add a Maker API App (if you haven't already.)
- In your Maker API App settings, set the 'URL to send device events to by POST' property to the URL of your Docker Container (i.e.
http://mydockerhost:3000
orhttp://192.168.1.123:3000
, etc.) - Trigger or wait for an event from one of your devices enabled in your Maker API to fire.
- In
psql
or whatever PostgreSQL client you use, connect to the database and look at theevents
table for updates.
Local Development
Read this section if you are a developer wanting to tinker with the code in this repository locally on your machine. Prerequisties for this example are to have NodeJS and Docker installed.
Set Up a Local Postgres DB Container
Local DB Setup
The following steps demonstrate how to start up a new PostgreSQL DB in a Docker container:
- Spin up a PostgreSQL Database using Docker:
docker run -d --name=hubitat-postgres \
-v /mnt/user/appdata/hubitat-postgres:/var/lib/postgresql/data \
-p 5432:5432 -e POSTGRES_PASSWORD=YourPassword postgres
- Create a
hubitat
DB in the container:
docker exec --user postgres hubitat-postgres psql -c "CREATE DATABASE hubitat;"
- Create
events
Table
docker exec --user postgres hubitat-postgres psql hubitat -c "CREATE TABLE IF NOT EXISTS events (name varchar( 255 ) NOT NULL, value varchar( 255 ) NOT NULL, displayName varchar( 255 ) NOT NULL, deviceId varchar ( 255 ) NOT NULL, descriptionText varchar( 255 ), unit varchar( 255 ), type varchar( 255 ), data varchar( 255 ), timestamp TIMESTAMP DEFAULT NOW());"
Initial Setup
- Clone this Git Repository
git clone https://github.com/entmike/hubitat-datacollector.git
- Install required npm modules
npm i
- Maintain your
.env
file
In the root of your cloned repository, create a filed called .env
with the following values
PGUSER=postgres
PGHOST=yourdbhost
PGPASSWORD=YourPassword
PGDATABASE=hubitat
PGPORT=5432
Run Local
npm run debug
Building your own Docker Image:
docker build --build-arg CACHE_DATE="$(date)" -t your/dockertag .
Running your Docker Image as Container:
docker run --rm -ti \
-e PGHOST=yourpostgreshost -e PGUSER=postgres -e PGPASSWORD=YourPassword \
-e PGDATABASE=hubitat -e PGPORT=5432 -p 3000:3000 your/dockertag