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: