Skip to content

Replace slashes with underscores in JSON payload keys #209

@Unambiguous

Description

@Unambiguous

Describe the solution you'd like

Hi there,
I would like to suggest to replace or escape slashes in the keys of the JSON payload. There seems to be an issue with Home Assistant integration where the slashes in the JSON key are considered as different topic levels and this creates an invalid topic that can not be processed by Home Assistant a the corresponding entity is not created.

The root of the issue comes from the fact that the name of the battery contains a date in a format of YYYY/MM/DD.

I am not sure whether it makes sense to do this kind of key sanitization at this place, so I am reporting this as a feature request instead of an issue.

I will also submit an issue on Home Assistant's ticketing system, but I was thinking maybe this would be a quicker way to resolve the issue.

Additional context

Here's a practical example of what is happening on my laptop.
Used broker: Mosquitto broker

lnxlink publishes the following payload under topic lnxlink/laptop-01/monitor_controls/battery:

{
  "Hewlett-Packard Primary 01234 2024/10/22": {
    "percent": 100.0,
    "attributes": {
      "vendor": "Hewlett-Packard",
      "model": "Primary",
      "serial": "01234 2024/10/22",
      "native_path": "BAT0",
      "rechargeable": true,
      "status": "fully charged",
      "time_to_empty": 0,
      "time_to_full": 0
    }
  },
  "Wireless Mouse": {
    "percent": 100.0,
    "attributes": {
      "vendor": "",
      "model": "Wireless Mouse",
      "serial": "11-22-33-44",
      "native_path": "hidpp_battery_2",
      "rechargeable": true,
      "status": "fully charged",
      "time_to_empty": 0,
      "time_to_full": 0
    }
  },
  "AC": {
    "percent": 0.0,
    "attributes": {
      "vendor": "",
      "model": "",
      "serial": "",
      "native_path": "AC",
      "rechargeable": false,
      "status": "unknown",
      "time_to_empty": 0,
      "time_to_full": 0
    }
  }
}

When reviewing the topic in MQTT Explorer, everything seems to be OK.

This topic is then adopted by Home Assistant. The following topics are created:

  • homeassistant/sensor/lnxlink/laptop-01_battery_ac/config
  • homeassistant/sensor/lnxlink/laptop-01_wireless_mouse/config
  • homeassistant/sensor/lnxlink/laptop-01_battery_hewlett-packard_primary_01234_2024/10/22/config

The topic related to the main battery (third item in the list above) has two more levels, and Home Assistant fails to create a corresponding entity. The other two battery entities are created ok.

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions