Decode Base64 data

Hi, I have a sensor

- platform: template
  sensors:
    trasmettitore_rc_potenza:
      friendly_name: "Potenza Trasmettitore RC"
      unit_of_measurement: "W"
      value_template: >
        {{ state_attr('sensor.trasmettitore_rc', 'PARAM')[5] }}

The value is Base64. Is there a way to decode it directly on the value_template parameter?

E.g.

IDEwMDA= -> 1000

Thanks

Use base64_decode.

Example:

Screenshot from 2021-07-23 13-35-37

Specifically for your sample value:

Screenshot from 2021-07-23 13-39-32

2 Likes

Thank you! It works!

2 Likes

I was stumbiling over this solution while trying to decode a datapacket from a Lora-Node.

Unfortunately this today does not work (anymore?)

I get this errormessage:
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xb3 in position 0: invalid start byte

Perhaps the problem might be that the supplied string isn’t encoded correctly.

Because decoding works with an encoded string.

The problem is that LoraWAN networks send raw byte payloads base64 encoded. So these can not be decoded into utf-8 character strings.

Keep an eye on Base64 decode encoding by paulusbrand · Pull Request #116603 · home-assistant/core · GitHub.

This pull request will enable the option to decode the base64 string into raw bytes, example:

Hi,

I am trying to decode a base64 image received over MQTT.

I ran however the template and got the error

UnicodeDecodeError: 'utf-8' codec can't decode byte 0xff in position 0: invalid start byte

When I insert the same payload into Base64 to Image | Base64 Decode | Base64 Converter | Base64 It resolves the image.

As a side, my mqtt looks like this:

camera:
  name: anpr_camera
  topic: Test01_TrafficMessagePostImage #(This is the image only object in base64)
  json_attributes_topic: Test01_TrafficMessage
  image_encoding: b64
  unique_id: anplrcamera2

However I cannot seem to get the image to display which is possibly down to the error when I try and use the template?

I appreciate this topic is closed, however this does not appear to work for me, maybe due to the image aspect of the base64?

Also, it is not a big image:

File Info

• Resolution: 124×56
• MIME type: image/jpeg
• Extension: jpg
• Size: 3.08 KB
• Channels: 3
• Bit depth: 8

Any help or pointers would be most welcome!

Many thanks