Text Sensor URL Encoding

Hello ESPHome users!

For a few days I’ve been implementing my first things with ESPHome and have managed it quite well so far. Unfortunately I can’t get any further at one point. I have an MQTT text sensor on which the desired text is also received. However, this text is URL coded so that the special characters %xx are hidden in the text. Now I’ve tried to clarify the whole thing with a text filter and “substitute”, which works quite well, but unfortunately not with the “spaces” in text. It’s just not clear to me how I should do it, the %20 (URL encodet) in the text is replaced by a space.

- platform: mqtt_subscribe
    name: AudioTitel
    id: AudioTitel
    topic: lox2mqttms1/504F94A0FD49/1a8ffe02-034a-c73f-ffffbde3a5b0cf24
    filters:
      - substitute:
        - "%20 -> _"
        - "%21 -> !"
        - "%22 -> '"
        - "%23 -> #"
        - "%24 -> $"
        - "%25 -> %"
        - "%26 -> &"
        - "%27 -> '"
        - "%28 -> ("
        - "%29 -> )"
        - "%2A -> *"
        - "%2B -> +"
        - "%2C -> ,"
        - "%2D -> -"
        - "%2E -> ."
        - "%2F -> /"
        - "%3A -> :"
        - "%3B -> ;"
        - "%3C -> <"
        - "%3D -> ="
        - "%3E -> >"
        - "%3F -> ?"
        - "%40 -> @"
        - "%5B -> ["
        - "%5D -> ]"
        - "%7B -> {"
        - "%7C -> |"
        - "%7D -> }"

Thank you for your ideas!

You probably have to add a lambda.

Untested:

- platform: mqtt_subscribe
    name: AudioTitel
    id: AudioTitel
    topic: lox2mqttms1/504F94A0FD49/1a8ffe02-034a-c73f-ffffbde3a5b0cf24
    filters:
      - substitute:
        - "%21 -> !"
        - "%22 -> '"
        - "%23 -> #"
        - "%24 -> $"
        - "%25 -> %"
        - "%26 -> &"
        - "%27 -> '"
        - "%28 -> ("
        - "%29 -> )"
        - "%2A -> *"
        - "%2B -> +"
        - "%2C -> ,"
        - "%2D -> -"
        - "%2E -> ."
        - "%2F -> /"
        - "%3A -> :"
        - "%3B -> ;"
        - "%3C -> <"
        - "%3D -> ="
        - "%3E -> >"
        - "%3F -> ?"
        - "%40 -> @"
        - "%5B -> ["
        - "%5D -> ]"
        - "%7B -> {"
        - "%7C -> |"
        - "%7D -> }"
      - lambda: |-
          size_t index = x.find("%20", 0);
          while (index != std::string::npos) {
            x.replace(index, 3, " ");
            index = x.find("%20", index+3);
          }
          return x;
1 Like

Hello ChrisB,

I already thought that I was missing a llama :llama: somewhere :rofl:
Installed it earlier and after a little adjustment it works great. I changed “index+3” to “index+1”, because otherwise there always had to be 3 characters before the next space was replaced.

Thank you !!!