MichielVanwelsenaere / HomeAutomation.CoDeSys3

Home Automation system build in CoDeSys 3 with MQTT communication to any third party Home Automation software
MIT License
112 stars 37 forks source link

Support for logger #128

Closed meijer3 closed 1 year ago

meijer3 commented 1 year ago

I build myself a mqtt logger. Maybe interesting for others?

An use case is: MqttVariables.MQTT_logger.send('DMX ping Error'); It outputs to Devices/PLC/House/debug

I created a funtion block

FUNCTION_BLOCK X_FB_MQTT_LOG 
VAR_INPUT
END_VAR
VAR
    InitMqttDone            :BOOL;
    MqttPublishTopicPrefix  :POINTER TO STRING;
    pMqttPublishQueue       :POINTER TO FB_MqttPublishQueue; 
    MqttQos                 :MQTT.QoS;
    MqttRetain              :BOOL;
    MqttMessageReceived     :BOOL;

END_VAR 

With the same InitMqtt methods like other FBs here. I added a send method to the functionblock

METHOD send
VAR_INPUT
    payload         :STRING(128);
END_VAR
----
IF InitMqttDone THEN
pMqttPublishQueue^.AddMessage(
    Payload:= payload,
    Topic := MqttPublishTopicPrefix^,
    Qos := MqttQos,
    MqttRetain := MqttRetain
);
END_IF      

And in the MqttVariables

MQTT_logger                         : FB_MQTT_LOG;      
meijer3 commented 1 year ago

I like the MQTT debug log because you can transfer it into home-assistant. The idea is based on the OpenMqttGateway debug topic

MichielVanwelsenaere commented 1 year ago

how do you transfer it exactly in to Home-Assistant?

Obviously logging is usefull but I wonder if it wouldn't be better to have logging functionality in the individual function blocks (where necessary).

For example; it seems that you build some DMX functionality which performs a ping to an external device. Most likely there is some other functionality concerning DMX as well then just the ping?

In theory, you could build a FB around this, for example FB_DMX_CONTROLLER. This FB could emmit a log via 'FB_DMX_CONTROLLER/log'. The added value here is that if you have two instances of that DMX controller (because you have for example two external dmx drivers) you'll have 'FB_DMX_CONTROLLER_House' & 'FB_DMX_CONTROLLER_Garden' each emmiting their logs via 'FB_DMX_CONTROLLER_House/log' & 'FB_DMX_CONTROLLER_Garden/log' so you know which one is having a ping error exactly?

I haven't read the OpenMqttGateway docs yet, will try to find some time.

meijer3 commented 1 year ago

You can use a logger in serveral ways. I would only be interested in errors and not complete log files (yet). Therefore I push only errors to mqtt and a simple mqtt sensor would show the last line/error.

mqtt:
  sensor:
    - name: "plc_debug"
      object_id: "plc_debug"
      unique_id: "plc_debug"
      state_topic: "Devices/PLC/House/debug"
      qos: 0

I very much like your instance-logger. That would be a great add on. Other things. that might be interesting or overkill would be:

meijer3 commented 1 year ago

In the current PR this basic is included. To show logs in HA, that is a bit tricky. It also depends how beautiful you want to show them. I show the last 10 statements in my dashboard, with timestamps. But this requires 2 extra plugings

To create a variable that contains history: https://github.com/Wibias/hass-variables To show the log in the dashboard: entity-attributes-card

image

- id: 'automation_plc_log'
  alias: 'Motion Update'
  trigger:
  - platform: state
    entity_id:
      - sensor.plc_log
  action:
    - service: variable.set_variable
      data:
        variable: plc_log_history
        replace_attributes: true
        attributes:
            r1: "{{now().strftime('%Y-%m-%d %H:%M')}} {{states('sensor.plc_log')}}"
            r2: "{{state_attr('variable.plc_log_history','r1')}}"
            r3: "{{state_attr('variable.plc_log_history','r2')}}"
            r4: "{{state_attr('variable.plc_log_history','r3')}}"
            r5: "{{state_attr('variable.plc_log_history','r4')}}"
            r6: "{{state_attr('variable.plc_log_history','r5')}}"
            r7: "{{state_attr('variable.plc_log_history','r6')}}"
            r8: "{{state_attr('variable.plc_log_history','r7')}}"
            r9: "{{state_attr('variable.plc_log_history','r8')}}"
            r10: "{{state_attr('variable.plc_log_history','r9')}}"
type: custom:entity-attributes-card
entity: sensor.plc_log
heading_name: Name
heading_state: State
filter:
  include:
    - variable.plc_log_history.r1
    - variable.plc_log_history.r2
    - variable.plc_log_history.r3
    - variable.plc_log_history.r4
    - variable.plc_log_history.r5
    - variable.plc_log_history.r6
    - variable.plc_log_history.r7
    - variable.plc_log_history.r8
    - variable.plc_log_history.r9
    - variable.plc_log_history.r10

And you could overule the css:

card_mod:
  style: |
    th,
    td:nth-child(1)  {
      display:none;
    }
    tr{
      background:none !important;
    }
meijer3 commented 1 year ago

https://help.codesys.com/webapp/_cds_obj_task_config_sys_event;product=codesys;version=3.5.13.0 Maybe these events are intersting. Like stop and restarts

meijer3 commented 1 year ago

Oh nevermind that long implementation: Simple card

   type: logbook
   entities:
     - sensor.plc_log