Getting Sensor information from json data value

I have a sensor that I am trying to retrieve a temperature information in HEX value from a large json string for Home assistant to display as a temperature sensor.

Here is what appears in the Tasmota console:

15:53:01 MQT: tele/Kettle/RESULT = {“TuyaReceived”:{“Data”:“55AA0007001C0101000101050200040000004B650100010066040001006704000101BB”,“Cmnd”:7,“CmndData”:“0101000101050200040000004B650100010066040001006704000101”,“DpId”:1,“DpIdType”:1,“DpIdData”:“01”}}

My sensor was looking like below but my regex is wrong when I check config

  • platform: mqtt
    name: Kettle Temp
    state_topic: “tele/Kettle/RESULT”
    value_template: “{{ value_json.TuyaReceived.CmndData | regex_find(’^.{24}([0-9a-fA-F]{2})’) }}”

The value is in both Data and CmndData
I am using CmndData as it is shorter.
The 2 characters in position 24 and 25 for my hex value
010100010105020004000000**4B**650100010066040001006704000101

How can I return these 2 characters as a sensor value or does someone have a better way

If I test the regex https://regex101.com/

^.{24}([0-9a-fA-F]{2})
0101000101050200040000004B650100010066040001006704000101

It seems to work fine

How can I return these 2 characters as a sensor value or does someone have a better way
I know that once i get the values I will need to convert them to decimal hopefully by appending
|int(base=16) to the end

This template returns “True” for me in the template dev tool.

{%- set value_json = '{"TuyaReceived":{"Data":"55AA0007001C0101000101050200040000004B650100010066040001006704000101BB","Cmnd":7,"CmndData":"0101000101050200040000004B650100010066040001006704000101","DpId":1,"DpIdType":1,"DpIdData":"01"}}' | from_json -%}
{{ value_json.TuyaReceived.CmndData | regex_search('^.{24}([0-9a-fA-F]{2})') }}

This does not exist according to the doco.

If the string is always the same length and the temperature value always at the same position, then the following is much simpler:

{{ value_json.TuyaReceived.CmndData[24:26] }}

And you’re right you can then simply turn this into a base-10 integer:

{{ value_json.TuyaReceived.CmndData[24:26] | int(base=16) }}

This should do it (index defaults to 0, so first instance):

{%- set value_json = '{"TuyaReceived":{"Data":"55AA0007001C0101000101050200040000004B650100010066040001006704000101BB","Cmnd":7,"CmndData":"0101000101050200040000004B650100010066040001006704000101","DpId":1,"DpIdType":1,"DpIdData":"01"}}' | from_json -%}
{{ value_json.TuyaReceived.CmndData | regex_findall_index('^.{24}([0-9a-fA-F]{2})') }}

Wow, Thanks all for the quick replies all.

I just test all three

{{ value_json.TuyaReceived.CmndData | regex_search('^.{24}([0-9a-fA-F]{2})') | int(base=16) }}

{{ value_json.TuyaReceived.CmndData[24:26] | int(base=16) }}

{{ value_json.TuyaReceived.CmndData | regex_findall_index('^.{24}([0-9a-fA-F]{2})') | int(base=16) }}

All of these work great.

Thank you all I now have a temperature for my kettle.

1 Like

Late to the party but my vote is for this one:

{{ value_json.TuyaReceived.CmndData[24:26] | int(base=16) }}

Based only on a seat of the pants feeling, I think it’s more efficient. Performing a simple string slice seems like less effort required than searching through a string for a matching pattern. Plus the template is more compact and legible (i.e. [24:26] versus '^.{24}([0-9a-fA-F]{2})').

Hi , this seems to work well …

  • platform: mqtt
    name: “Kettle Temp”
    state_topic: “tele/Kettle/RESULT”
    value_template: “{{ value_json.TuyaReceived.DpType2Id5 | int(base=10) }}”
    unit_of_measurement: °C

image