How to extract a substring from an MQTT sensor value?

Consider the following MQTT sensor:

  - platform: mqtt
    state_topic: "rfbridge-1/rfin"
    name: rf433
    unique_id: rf433

The payload received on this topic is a string such as XXXXXXAAAAAA where the XXXXXX part is irrelevant and I am interested in the content of AAAAAA, the last six elements of the string.

Is there a way I can template this? I had a look at the templating documentation but I did not see any operations on strings (such as the Python equivalent of payload[-6:])

That exists and can be used to get what you want; the documentation doesn’t cover all that’s possible with python.

  value_template: '{{ value[-6:] }}'
1 Like