Value_template: how to get last x characters from mqtt json payload?

Hi,

I’m trying to use a rf-bridge to receive rf-messages and do some automations.
I flashed a sonoff rf-bridge with Tasmota and Portisch.
When I press a button, I get

15:45:19.402 MQT: tasmota/tasmota-112/tele/RESULT = {"Time":"2021-11-06T15:45:19","RfReceived":{"Sync":11100,"Low":280,"High":980,"Data":"FF5F71","RfKey":"None"}}`

and with following automation works like a charm:

# -------------------------------------------
  - id: tasmota_rf_bridge_FF5F71_mqtt
    alias: tasmota_rf_bridge_FF5F71_mqtt
    trigger:
      - platform: mqtt
        topic: "tasmota/tasmota-112/tele/RESULT"
        payload: "FF5F71"
        value_template: "{{ value_json.RfReceived.Data }}"
    action:
    - data:
        entity_id: light.group_n00_p00
        transition: 0
      service_template:
        homeassistant.toggle

However, a lot of my RF-devices are not supported (even with Portisch), so I have to switch to “raw”-mode: rfraw 177

The messages I receive then are something like (below is message sent from pressing same button on my remote):

15:48:48.093 MQT: tasmota/tasmota-112/tele/RESULT = {"Time":"2021-11-06T15:48:48","RfRaw":{"Data":"AA B1 04 03FC 017C 010E 2B52 3818181818181818190819081818181819081818190A090819 55"}}
15:48:48.276 MQT: tasmota/tasmota-112/tele/RESULT = {"Time":"2021-11-06T15:48:48","RfRaw":{"Data":"AA B1 04 03FC 0186 0118 2B48 381818181818181819081A08181818181A0818181A0A090819 55"}}
15:48:48.413 MQT: tasmota/tasmota-112/tele/RESULT = {"Time":"2021-11-06T15:48:48","RfRaw":{"Data":"AA B1 04 049C 00E6 019A 2B5C 38182828282828282908290828282828290828282A09090829 55"}}
15:48:48.960 MQT: tasmota/tasmota-112/tele/RESULT = {"Time":"2021-11-06T15:48:48","RfRaw":{"Data":"AA B1 04 03F2 0190 0122 2B3E 38181818181818181A081A08181818181A0818181A0A0A081A 55"}}
15:48:50.238 MQT: tasmota/tasmota-112/tele/RESULT = {"Time":"2021-11-06T15:48:50","RfRaw":{"Data":"AA B1 04 03FC 019A 0118 2B48 38181818181818181A081A08181818181A0818181A0A0A081A 55"}}

as you can see, the messages are different, some “buckets” are in front and I can not use the “Data” as a whole.

What I want to do is to take only the last 53 characters from “Data”, thus:

AA B1 04 03FC 0190 0122 2B7A 38181818181818181A081A08181818181A0818181A0A0A081A 55

will become:

38181818181818181A081A08181818181A0818181A0A0A081A 55

and than create an automation based on this value.

Can this be done? How?

any help is appreciated,
kind regards
Bart

Try this:

value_template: "{{ value_json.RfReceived.Data[-56:-3] }}"

Explanation: https://www.freecodecamp.org/news/how-to-substring-a-string-in-python/

1 Like

@tom_l : SUPER!
works perfect. I was confused and looking in the direction of javascript split… :flushed:

thanx a lot!

1 Like

The last 53 characters would be this: [-53:]

value_template: "{{ value_json.RfReceived.Data[-53:] }}"

How is [-56:-3] giving you the last 53 characters?

mmm, still one question:

the above automation works, with condition

        payload: "FF5F71"
        value_template: "{{ value_json.RfReceived.Data }}"

The following automation does NOT work:

# -------------------------------------------
  - id: tasmota_rf01_01
    alias: tasmota_rf01_01
    trigger:
      - platform: mqtt
        topic: "tasmota/tasmota-112/tele/RESULT"
        payload: "38181818181818181A081A08181818181A0818181A0A0A081A"
        value_template: "{{ value_json.RfRaw.Data[-56:-3] }}"
    action:
    - data:
        entity_id: light.group_n00_p00
        transition: 0
      service_template:
        homeassistant.toggle

I double checked the value and topic, here is a screenshot of the tasmota

What am I doing wrong??

grtz
B

@123 : I think @tom_l analysed my problem, and what he suggested was even better: [-56:-3] gives me the last 53 meaningfull characters, so I suppose you are both right :slight_smile:

Actually, neither of us was right. Look at the value you used in the automation’s payload.

That’s not [-56:-3] or [-53:]. Apparently, you want something completely different from what you originally asked for or even what tom_I guessed you might want.

It appears that what you really want is [-53:-3].

        payload: "38181818181818181A081A08181818181A0818181A0A0A081A"
        value_template: "{{ value_json.RfRaw.Data[-53:-3] }}"

what’s the emoticon of being terribly embarrased? :slight_smile:

thanx! solved my problem!

grtz
B

Not when I did it:

{% set Data = 'tasmota/tasmota-112/tele/RESULT = {"Time":"2021-11-06T15:48:48","RfRaw":{"Data":"AA B1 04 03FC 017C 010E 2B52 3818181818181818190819081818181819081818190A090819 55"}}' %}
{{ Data[-56:-3] }}
{{ Data[-53:-3] }}

And I see why.

The important thing was the slicing. That’s my excuse and I’m sticking to it. :smirk:

I see it too; the last three characters of that string are "}} so that’s why [-56:-3] worked for it.

This dict would’ve worked:

{% set value_json = {"Time":"2021-11-06T15:48:48","RfRaw":{"Data":"AA B1 04 03FC 017C 010E 2B52 3818181818181818190819081818181819081818190A090819 55"}} %}
{{ value_json.RfRaw.Data[-53:-3] }}

FWIW, I recently goofed up showing how to use expand with a Light Group … but only tested it with a Group (expand doesn’t know how to extract the members of a Light Group directly).

This would have worked too:

{{ value_json[-56:-3] }}

If you’re referring to the example I posted, value_json contains a dictionary and you can’t slice that.

No I meant for my data. I used the whole payload.