Skip to main content

Parser for Zigbee2Mqtt logs to python object for further processing

Project description

zigbee2mqtt log parser

A simple module for parsing of zigbee2mqtt logs into Python object for further processing.

How to install:

pip install z2m-log-parser

How to use:

from z2m_log_parser import *

***

parser = Z2mLogParser()

Return type:

class LogEntry(object):
    def __init__(self):
        self.type: str
        self.date: datetime
        self.data: LogEntryData

class LogEntryData(object):
    def __init__(self):
        self.is_mqtt_publish: bool
        self.message: str
        self.mqtt_message: MqttMessage

class MqttMessage(object):
    def __init__(self):
        self.topic: str
        self.payload: json

Methods:

parse_logs(<log_path>) -> list[LogEntry]

path = './log.txt'
logs = parser.parse_logs(path)

This method will read the content of the target log file and return it's content as a list of python object representing each log entry.

parse_latest_logs(<log_path>) -> list[LogEntry]

path = './log.txt'
logs = parser.parse_latest_logs('path')

This method will do the same as the one above, but it will generate a pointer file in the execution directory, containing the timestamp of the latest log on the time of the execution. Upon following executions, the method will use the pointer file as reference in order to determine and return only the latest entries, logged after the previous execution thus allowing the module to be used on a schedule base. The pointer file will be update after each execution.

Examples:

Input:

info  2024-05-28 10:42:38: MQTT publish: topic 'zigbee2mqtt/0x84fd27fffe9f718e', payload '{"linkquality":54,"power_on_behavior":"previous","state":"OFF","update":{"installed_version":537019939,"latest_version":587765297,"state":"available"}}'

Output:

'type': 'info',
'date': datetime.datetime(2024, 5, 28, 10, 42, 38),
'data': {
         'is_mqtt_publish': True,
         'message': 'MQTT publish: topic \'zigbee2mqtt/0x84fd27fffe9f718e\', payload \'{"linkquality":54,"power_on_behavior":"previous","state":"OFF","update";{"installed_version":537019939,"latest_version":587765297,"state":"available"}}\'\n',
         'mqtt_message': {
                          'topic': 'zigbee2mqtt/0x84fd27fffe9f718e',
                          'payload': {
                                      'linkquality'=54,
                                      'power_on_behavior'='previous',
                                      'state'='OFF',
                                      'update': {
                                                 'installed_version'=537019939,
                                                 'latest_version'=587765297,
                                                 'state=''available'
                                                }
                                     }
                         }
        }

Input:

info  2024-05-25 22:53:36: Starting Zigbee2MQTT version 1.35.1 (commit #9eaaa0f)

Output:

{
'type': 'info',
'date': 'datetime.datetime(2024, 5, 25, 22, 53, 36)',
'data': {
          'is_mqtt_publish': False,
          'message': "Starting Zigbee2MQTT version 1.35.1 (commit #9eaaa0f)",
          'mqtt_message': {
                           'topic': None,
                           'payload': None
                          }
        }
}

Input:

error 2024-05-25 22:53:58: Error: Failed to connect to the adapter (Error: SRSP - SYS - ping after 6000ms)
    at ZStackAdapter.start (/app/node_modules/zigbee-herdsman/src/adapter/z-stack/adapter/zStackAdapter.ts:103:27)
    at Controller.start (/app/node_modules/zigbee-herdsman/src/controller/controller.ts:132:29)
    at Zigbee.start (/app/lib/zigbee.ts:60:27)
    at Controller.start (/app/lib/controller.ts:98:27)
    at start (/app/index.js:107:5)

Output:

{
'type': 'error',
'date': datetime.datetime(2024, 5, 25, 22, 53, 58),
'data': {
         'is_mqtt_publish': False,
         'message': 'Error: Failed to connect to the adapter (Error: SRSP - SYS - ping after 6000ms)\n
          at ZStackAdapter.start (/app/node_modules/zigbee-herdsman/src/adapter/z-stack/adapter/zStackAdapter.ts:103:27)\n
          at Controller.start (/app/node_modules/zigbee-herdsman/src/controller/controller.ts:132:29)\n
          at Zigbee.start (/app/lib/zigbee.ts:60:27)\n    at Controller.start (/app/lib/controller.ts:98:27)\n
          at start (/app/index.js:107:5)',
         'mqtt_message': {
                          'topic': None,
                          'payload': None
                         }
        }
}

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

z2m_log_parser-0.1.1.tar.gz (4.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

z2m_log_parser-0.1.1-py3-none-any.whl (4.7 kB view details)

Uploaded Python 3

File details

Details for the file z2m_log_parser-0.1.1.tar.gz.

File metadata

  • Download URL: z2m_log_parser-0.1.1.tar.gz
  • Upload date:
  • Size: 4.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/5.0.0 CPython/3.12.3

File hashes

Hashes for z2m_log_parser-0.1.1.tar.gz
Algorithm Hash digest
SHA256 ac5e5eda9ced1ed3cf4ed6827ecd82b6dc4b50a02b6dcfeec2ebfe8287a88779
MD5 dce2699cf69371a8e9614f896a831611
BLAKE2b-256 ae78f9c6f7d5bacca529ebf42d9c5e5e1b2a194f8b794541a494eb8a2d212594

See more details on using hashes here.

File details

Details for the file z2m_log_parser-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: z2m_log_parser-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 4.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/5.0.0 CPython/3.12.3

File hashes

Hashes for z2m_log_parser-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 ccc5784b1f1e950733c3f80f20b5ebed50ee597a49f6689acdb9358f45a61de9
MD5 2f28963e4fe13a518d70e4f129298bfb
BLAKE2b-256 3cfe76862f6a765672fa3b5dc62bd5931c97dbbdddcb19bc66a46444997ad0d0

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page