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("--------------------------------------------------")

Sorry to re-open this topic, but which are the values to change in this script?
I’ve changed the “IPADRESSOFBROKER” to the IP of my AJAX hub, but what else to change?

Hi, base on python script from @Flop , I am able to get MQTT message to openhab-cli.
message looks like this:

08:33:23.317 [INFO ] [openhab.event.ItemStateChangedEvent ] - **Item 'alarm_state' changed from "ADM-CID"0106L0#8B4F[#8B4F|1401 01 001][Iuser]_07:24:11,03-28-2024 to "ADM-CID"0107L0#8B4F[#8B4F|3401 01 001][Iuser]_07:33:30,03-28-2024**

Unfortunate I do not know how to parse it, to get status of the alarm. eg. Armed or Disarmed etc…
Any help will be appreciated.

I can change @Flop python code in the publish part of the script
from:

ret= client1.publish("alarm/status",ev.full_message)

to:

ret= client1.publish("alarm/status",ev.content)

in order to get another type of mqtt message:

08:44:00.654 [INFO ] [openhab.event.ItemStateChangedEvent  ] - Item 'alarm_state' changed from "ADM-CID"0107L0#8B4F[#8B4F|3401 01 001][Iuser]_07:33:30,03-28-2024 to #8B4F|1401 01 001][Iuser]_07:44:07,03-28-2024

It is shorter mqtt message which hold necessary information:

#8B4F|1401 01 001][Iuser]_07:44:07,03-28-2024

in debug mode of the pysiaalarm, I can see log, that I do not know how to publish to mqtt broker, but looks like this:

DEBUG:pysiaalarm.event:Content matches: {'account': '8B4F', 'event_qualifier': '1', 'event_type': '401', 'partition': '01', 'ri': '001', 'xdata': 'Iuser', 'timestamp': '07:49:59,03-28-2024'}