Always send new line of a file to a mqtt broker

hi,

I’m working since days on integrating my AJAX alarm in openHAB.
I tried python example from eavanvalkenburg (Eduard van Valkenburg) · GitHub with no sucess. ( not all sensor are managed and I not even know how to modify the code ).
So I have setup a small homeassistant server to install existing AJAX module, then activated debug and now home-assistant.log is logging raw message from my alarm ( SIA ).
I made a file sensor on home assistant to send last line of this log to my openhab server via mqtt broker.
It works but some lines are lost if there is too message in a short time.

So I would like to simplify.
How can I monitor the home-assistant log ( \\homeassistant.local\config\home-assistant.log ) to get all the new lines added since last call and line by line directly with openhab server ?

I tried the LogReader binding, all is green but no event is generating ( due to the file that are on the network, unc ? ).

Openhab version 3.0.1-M1 on windows server 2016.

Thanks in advance for helping me.

You need to set the QOS on Home Assistant and on openHAB to 1 or 2 so the messages are not dropped.

If log reader doesn’t work, there really is nothing on the openHAB server that is going to work. You’ll have to put something on the Home Assistant server to read the logs and publish them to openHAB, which puts you right back where you started.

So I tried change QOS level with no success.

After some trial, I found that :

  • Logreader binding on openhab seems to take full control on the log file on \192.168.1.78\config\home-assistant.log so it works on start but then homeassistant stop to log until I pause the binding…
  • It seems that this logfile is not writed line by line, it seems to exists a “memory storage” and memory is written to logfile frequently but it is possible that several log lines are written at the same time, and the sensor on home assistant take only care of the last line.

So the both way I tried seems not to work.

I see no solutions for the moment…

Edit : Just wonder if the memory written to logile several lines at one time is a windows problem ???

How are you sharing the file? NFS, samba?

Indeed it is a common approach to cache a bunch of log statements and write them out in bulk all at once in order to improve performance and reduce the number of writes on the media.

Home Assistant is a hub like openHAB. Can’t you just have Home Assistant publish the values over MQTT? It seems overly complicated to make it log out the values only to have something else read the log and publish the messages. Why not have Home Assistant just publish the messages itself?

The share is made by a homeassistant add-on using samba share.

unfortunately, the AJAX add-on on home assistant is not fully compatible with my alarm bundle, there is smoke detector, humidity detector but my doorprotect and motion cam detector are not driven by the add-on, and so I can’t simply resend to mqtt broker and I need the raw message from my alarm.
I tried to explore python code of the add-on but it’s too hard for my knowledge.
I think i will try with the python code ( not homeassistant dependant ) , this will be more simple to add a send to mqtt broker.

When accessing files over samba, the file events that tell a tool like tail or the LogReader add-on don’t happen so they never know that the file has been added to. That’s why you’ll need to access the file on the home assistant machine itself where the file events are occurring.

ok thanks for all.

I’ll explore two solutions :

  • first : modify python code ( the non ha dependant version ) to get the raw SIA code.
  • and secondly if the first is not ok : homemade home assistant log file scanner to mqtt broker

Some news for those who would be interested to have AJAX Alarm linked to Openhab.

Latest beta version of pysiaalarm ( 2.0.9b5 ) : GitHub - eavanvalkenburg/pysiaalarm at dev is working. ( the master one didn’t work with my setup, I don’t know why ).

So I will modify the simple python script ( run.py ) to send the raw message to my mqttbroker.

Here is the python script on my openhab server, it works :

import json
import logging
import time
import paho.mqtt.client as paho

from pysiaalarm import SIAAccount, SIAClient, SIAEvent

broker="IPADRESSOFBROKER"
port=1883

def on_publish(client,userdata,result):             #create function for callback
    print("data published \n")
    pass
client1= paho.Client("control1")                           #create client object
client1.on_publish = on_publish                          #assign function to callback
client1.connect(broker,port)   

#logging.basicConfig(level=logging.DEBUG)

events = []


def func(event: SIAEvent):
    events.append(event)


with open("local_config.json", "r") as f:
    config = json.load(f)
print("Config: ", config)
account = [SIAAccount(config["account_id"], config["key"])]
sleep_time = 30
with SIAClient(config["host"], config["port"], account, function=func) as client:
    #time.sleep(sleep_time)
    #counts = client.counts
    while 1:
        for ev in events:
            print(ev)
            ret= client1.publish("alarme/state",ev.full_message)                   #publish
        events=[]
        time.sleep(1)
#print("--------------------------------------------------")
#print("Number of events: ", len(events))
#print("Counts: ", counts)
#print("--------------------------------------------------")