A while after joining the Logentries team, and becoming familiar with the LEQL query language, I figured recording the state changes of HomeAssistant entities (switches, sensors, etc) to my Logentries account (free option available) is a handy way of analysing the data, by running calculations against the data points - who doesn't like pretty graphs!

The plugin itself is a very simple Python script, forwarding any state changes to a target log in my account:

import json
import logging
import requests

import voluptuous as vol

import homeassistant.helpers.config_validation as cv
from homeassistant.const import (CONF_TOKEN, EVENT_STATE_CHANGED)
from homeassistant.helpers import state as state_helper

_LOGGER = logging.getLogger(__name__)

DOMAIN = 'logentries'

DEFAULT_HOST = 'https://webhook.logentries.com/noformat/logs/'

CONFIG_SCHEMA = vol.Schema({
    DOMAIN: vol.Schema({
        vol.Required(CONF_TOKEN): cv.string,
}, extra=vol.ALLOW_EXTRA)

def setup(hass, config):
    """Set up the Logentries component."""
    conf = config[DOMAIN]
    token = conf.get(CONF_TOKEN)
    le_wh = '{}{}'.format(DEFAULT_HOST, token)

    def logentries_event_listener(event):
        """Listen for new messages on the bus and sends them to Logentries."""
        state = event.data.get('new_state')
        if state is None:
            _state = state_helper.state_as_number(state)
        except ValueError:
            _state = state.state
        json_body = [
                'domain': state.domain,
                'entity_id': state.object_id,
                'attributes': dict(state.attributes),
                'time': str(event.time_fired),
                'value': _state,
            payload = {
                "host": le_wh,
                "event": json_body
            requests.post(le_wh, data=json.dumps(payload), timeout=10)
        except requests.exceptions.RequestException as error:
            _LOGGER.exception("Error sending to Logentries: %s", error)

    hass.bus.listen(EVENT_STATE_CHANGED, logentries_event_listener)

    return True

Best part is that if you'd like to use it, it's already part of HomeAssistant. All you have to do, is enabling it in your HomeAssistant configuration:

# Example configuration.yaml entry
comments powered by Disqus