How to 'get' data from incoming 'mqtt' message...?

Scan by ‘mqtt’ is giving 2 results:

Example A:
36.88;ok

Example B:
39.81;64898;ok

I want to get with ‘value_template’:
A
‘36.88’ - this is temperature
‘ok’ - probably can be something else (like ‘not ok’), but still text

B
‘39.81’ - this is temperature
‘ok’ - probably can be something else (like ‘not ok’), but still text

How to get all 4 parameters?

P.S. ‘64898’ - I’m not interested…

results

I’m not sure if what you want to do is possible with this payload.

Be aware, the below works only if the desired number is always the first element and the text the last element of the payload.
This should work to extract the first number

value_template: "{{ value_json.split(";")[0] }}"

And this for the text

value_template: "{{ value_json.split(";")[-1] }}"

However, I think you need to either to create two sensors, one for the text and one for the number or if possible change the payload to a proper JSON payload, where “ok” is an attribute.

1 Like

Thank you. There is some error in syntax:

When I remove the “” from ; - it is also giving error…

Invalid config for [sensor.mqtt]: invalid template (TemplateSyntaxError: unexpected ';') for dictionary value @ data['value_template']. Got '{{ value_json.split(;)[0] }}'. (See ?, line ?). Please check the docs at https://home-assistant.io/integrations/mqtt/

U used too many quotes.

Copy this 1:1 and test

value_template: '{{ value_json.split(";")[0] }}'

O.K. Corrected to:

'{{ value_json.split(';')[0] }}'

No error. But no value appear.
The full ‘.yaml’ settings looks like below.
Should be still something corrected?

- platform: mqtt
    name: "sdtcfas18_returntemp"
    unit_of_measurement: '°C'
    state_topic: "ebusd/bai/ReturnTemp"
    availability_topic: "ebusd/global/signal"
    payload_available: "true"
    payload_not_available: "false"
    qos: 2
    value_template: "{{ value_json.split(';')[0] }}"

Yes, noticed that - but with “x” is also not working. Must be: ‘x’. Then no errors…
Anyway - still not getting results - see further post…

Do you see a value when you cut the .split part and only leave "{{ value_json }}"? I think the issue could be that your sensor doesn’t publish the state in JSON format.

With this code I see only ‘°C’.
Before that - was ‘Unknown’ displayed.
See screen what I mean:

cels

This MQTT “scan” that you did, was this made in home assistant? Your issue is either that the published data is not in JSON format or that home assistant is not connected to the MQTT Broker.

Yes, the data I see in ‘MQTT tool’ in ‘Develper tools’.
Format - as seen on 1st post…

2019-12-17 19_05_01-Window

After some research I used this:

value_template: >
      {{ value.split(';')[0] }}    
    

Then I get what I want:

split 1

Ah I see, didn’t know that you can use value directly. Learned something new :slight_smile:

Well, the data isn’t coming through as JSON formatted data, so it would be value only. JSON requires key:value, and the data showing up on his message bus is not JSON.

1 Like

Anyway I also learned something… Thx…

I don’t have any MQTT sensor in my system and all the examples I saw, used value_json. So I somehow assumed that MQTT payloads need to be in JSON :crazy_face:

Heh. No requirement there. A payload can contain a single value, like on or off, but they are not JSON.