Convert sensor value from bytes to string

Hi,

I have a SNMP sensor, which returns its value in bytes.

How can I encode the value to a string in the configuration.yaml?

- platform: snmp
    name: "TESTSENSOR"
    host: 192.168.178.180
    baseoid: 1.3.6.1.4.1.5127.1.1.1.8.1.7.0
    version: "2c"
    community: "public"

What do you mean?

All state values are strings.

Sorry I meant decode…

The value of my sensor is “0x43656e746967726164653a3536200946616872656e686569743a313332”.
When I put it in a byte decoder, I get a string of this hex string.

I C# or Python there exists a decode function (with given charset).
How can I decode this in YAML?

I’ve found a solution here

- platform: snmp
    name: "TESTSENSOR"
    host: 192.168.178.180
    baseoid: 1.3.6.1.4.1.5127.1.1.1.8.1.7.0
    version: "2c"
    community: "public"
    value_template: >
      {%- set line = value.replace('0x','') %}
      {%- set chars = " !'#$%&\'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~" %}
      {%- set n = 2 %}
      {%- set ns = namespace(value='') %}
      {%- for i in range(0, line | length, n) %}
      {%- set c = chars[line[i:i+n] | int('', 16) - 32 ] %}
      {%- if c is defined %}
      {%- set ns.value = ns.value ~ c %}
      {%- endif %}
      {%- endfor %}
      {{ ns.value }}