How to send a logbook_entry event via MQTT and auto-discovery

Hey folks, I’ve utilized MQTT auto-discovery on a device to so far expose number and sensor entities. What I’d like to do next is to expose a logbook_entry event so that my device can send periodic logs to the HomeAssistant logbook via MQTT.

I’ve gotten the configuration to the point where HA discovers the logbook event entity, but my subsequent attempts to send logs via the MQTT topic I defined in the discovery config message don’t seem to be properly interpreted by HA.

Here’s what my discovery config topic and payload look like:
topic:

homeassistant/event/UM-70041dad7d30_Bedroom_CO2_root/config

payload:

{
  "obj_id": "Bedroom_CO2_root",
  "evt_typ": [
    "logbook_entry"
  ],
  "name": "root",
  "~": "homeassistant/event/UM-70041dad7d30/root",
  "uniq_id": "UM-70041dad7d30_Bedroom_CO2_root",
  "stat_t": "~/logs",
  "dev": {
    "ids": "70041dad7d30",
    "name": "Bedroom_CO2",
    "mdl": "FeatherS3 with ESP32-S3",
    "sw": "v1.21.0-7-gecd418fb1-dirty on 2023-12-27",
    "mf": "esp32"
  },
  "val_tpl": "{{ value_json.name }}, {{ value_json.message }}, {{ value_json.domain }}, {{ value_json.entity_id }}"
}

And here is my attempt to send a message to the state topic:
topic:

homeassistant/event/UM-70041dad7d30/root/logs

payload:

{
  "name": "Bedroom_CO2_root",
  "domain": null,
  "event_type": "logbook_entry",
  "message": "15031046 app-INFO:Publishing data...",
  "entity_id": null
}

I confirmed that the MQTT broker on HA is receiving these messages, so the problem must be in how I’m configuring and formatting.

Any ideas on what I’m missing? Thanks!

What is the state of event.UM-70041dad7d30_Bedroom_CO2_root?

Refer to the second example in the documentation.

Note how the event entity’s value_template is used to create an attribute named button.

Your event entity’s value_template should be modified so that it creates attributes for the keys in your payload (name, domain, etc).

1 Like

It’s actually more specific than that

Defines a template to extract the value and render it to a valid JSON event payload

Thanks all. I must confess I find the documentation here super confusing.

In the second example you referred to, this looks like it requires that I write a yaml entry in my config file for translating the MQTT payload, which in the example is just:
{"Button1": {"Action": "SINGLE"}}

  1. I’d like auto-discovery to find all the relevant attributes without me needing to write customized yaml in HA, ie I haven’t had to do that with the Number and Sensor entities I’m currently exposing from my device.
  2. The payload seems to be missing some required attributes like event_types, which I guess is why it requires a custom yaml entry?

Blockquote
Your event entity’s value_template should be modified so that it creates attributes for the keys in your payload (name, domain, etc).

Blockquote
Defines a template to extract the value and render it to a valid JSON event payload

The value template I’m using was an attempt to render the event payload defined here. Is that not the correct event definition that I should be trying to match with the value template?

Ok, so I decided to drop the value template since I think I was using it incorrectly. Based on the first example here it seems that I can just include the event_type in the state topic payload along with the other event attribute values. With that I am now seeing logbook events showing up in HA! Here’s what my config and state payloads look like:

auto-discovery config:

{
  "obj_id": "Bedroom_CO2_root",
  "evt_typ": [
    "LOGBOOK_ENTRY"
  ],
  "name": "root",
  "~": "homeassistant/event/UM-70041dad7d30/root",
  "uniq_id": "UM-70041dad7d30_Bedroom_CO2_root",
  "stat_t": "~/logs",
  "dev": {
    "ids": "70041dad7d30",
    "name": "Bedroom_CO2",
    "mdl": "FeatherS3 with ESP32-S3",
    "sw": "v1.21.0-7-gecd418fb1-dirty on 2023-12-27",
    "mf": "esp32"
  }
}

state topic payload:

{
  "message": "1924071 app-INFO:Publishing data...",
  "event_type": "LOGBOOK_ENTRY",
  "name": "Bedroom_CO2_root"
}

The issue I have now is that when I look at the logbook I only see entries for each event detection; I can’t see the log message I sent in the state topic payload:

@koying I think the answer to my issue is related to answer your question. When I look at the ‘state’ of the event I can see that the state is just the date/time stamp, whereas log message I’m sending is considered an “attribute”

Is that just how logbook events are handled/represented? I was really hoping I could just use the logbook page to see all the log messages from my device. Any ideas on how I might be able to do that or at least visually see all of the “message” attributes for these lobook entry events?

No. The example keeps things simple by showing how to do it with an MQTT Sensor configured with YAML. The same principle applies to a sensor configured with MQTT Discovery. The important part of the example is the template, not the method used to create the sensor.

What led you to believe it would do that?

Can you point me to the documentation explaining that by setting an MQTT Event’s event_type to LOGBOOK_ENTRY it will make a part of the event’s payload (i.e. message) appear in Home Assistant’s Logbook?

The only method that I am aware of for doing that is by using the Logbook integration’s logbook.log service call.

Blockquote
What led you to believe it would do that?

Nothing, it was just an assumption. I was looking for a way to send logs to HA via MQTT and of all the entities support by auto-discovery, event seemed like the only one where I could possibly do that. I found this page which was the only place I could find that listed possible event types. Among the ones listed, LOGBOOK_ENTRY seemed the most promising based on name alone.

I couldn’t find any documentation on LOGBOOK_ENTRY other than this page that just lists its attributes which does list a message attribute. I didn’t think it too far-fetched of an assumption to think that an event called LOGBOOK_ENTRY would add an entry to the logbook presumably with the message attribute, but clearly I was mistaken. It seems that I’m trying to do something that is not explicitly supported by HA.

However, I don’t see how the logbook.log service call is any different. It looks to be generating the same logbook_entry event with the same name, message, entity_id, and domain attributes.

The events that are posted to the event bus have nothing to do with the Event integration (which is the basis for the MQTT Event integration). They’re two separate concepts; all they have in common is that use the same word “event”. An event entity isn’t an event on the event bus.

That’s understandable because you believe it’s all one concept except it’s two separate ones with nothing in common except a name.

I see, thank you for clearing that up. The remaining question I have then is what are the possible event_type strings that are a required parameter of MQTT Event integration if not the ones defined for the event bus?

I think that’s where I initially got tripped up since I my search for event_type led me to the event bus events.

It’s whatever string you choose that best describes what the device did. For example, a button would produce events like press, release, hold, etc.


Your original goal was:

my device can send periodic logs to the HomeAssistant logbook via MQTT.

You can do that with an automation that uses an MQTT Trigger to subscribe to a topic and then write the received payload to the log using the aforementioned logbook.log service call. Simple and easy.

Yeah, makes sense, thanks!