mqtt2ha
    Preparing search index...

    Function cleanString

    • Returns a string that is safe to use as an MQTT topic and as a Home Assistant discovery node_id/object_id by escaping every character that is not alphanumeric or an underscore. This is a reversible, collision-free encoding: unsupported characters (including a literal hyphen) are percent-style escaped as -XX, where XX is the uppercase hex value of each UTF-8 byte. A hyphen is used as the escape sigil rather than % because Home Assistant only accepts [A-Za-z0-9_-] in discovery ids.

      Because the encoding is injective, distinct inputs always produce distinct outputs, so entities whose names differ only in punctuation no longer collide onto the same discovery topic.

      Parameters

      • raw: string

        The string to be cleaned

      Returns string

      A cleaned string containing only alphanumeric characters, underscores, and hyphens

      cleanString('Living Room/Temp'); // returns "Living-20Room-2FTemp"
      cleanString('Sensor#1'); // returns "Sensor-231"
      cleanString('a/b'); // returns "a-2Fb"
      cleanString('a b'); // returns "a-20b" (no longer collides with 'a/b')