Hello
First of all, thank you for your help in advance. I did not find a solution for a sensor I just created.
My sensor is sending a message with mqtt and the message content is as follows.
The content is as stated below.
09:09:12 CMD: {“speed”: “0.00”, “direction”: “292”}
I want to select the speed and direction parts from the json part in this message. I could not succeed in any way, the part I could do is as follows.
you ask:
platform: mqtt
name: “Meterology_Wind_Yon”
state_topic: “stat / Meterology_98A39E / LOGGING”
unit_of_measurement: “°”
value_template: “{{value.split (’’) [2]}}”
platform: mqtt
name: “Meterology_Wind_Speed”
state_topic: “stat / Meterology_98A39E / LOGGING”
unit_of_measurement: “m / sn”
value_template: “{{value.split (’’) [2]}}”
How should I make a change in the code.
Best regards,
The problem is that it’s not a true json string so you can’t use the json notation to retrieve the values.
So you’ll have to use different methods for splitting the strings up to get to what you want.
the following will work with the data exactly as it is in your post:
{{ value.split(': ')[2].split(', ')[0] }}
{{ value.split(': ')[3].split(', ')[0][:-1] }}
And it should work as long as the data stays in that format.
those templates will return string values (“0.00”, “292”) so you may have to do other formatting to strip the " from them if needed.
also from now on you should get used to posting your code blocks properly formatted by using three backticks (```) on the line before and after the code block
petro
(Petro)
May 10, 2021, 4:21pm
3
split your string on the cmd and then use from_json
- platform: mqtt
name: "Meterology_Wind_Yon"
state_topic: "stat / Meterology_98A39E / LOGGING"
unit_of_measurement: "°"
value_template: >
{% set items = value.split('CMD: ')[-1] %}
{% set items = items | from_json %}
{{ items.direction }}
- platform: mqtt
name: "Meterology_Wind_Speed"
state_topic: "stat / Meterology_98A39E / LOGGING"
unit_of_measurement: "m / sn"
value_template: >
{% set items = value.split('CMD: ')[-1] %}
{% set items = items | from_json %}
{{ items.speed }}
1 Like
Thank you.
This is perfect.
platform: mqtt
name: “Meterology_Wind_Yon”
state_topic: “stat / Meterology_98A39E / LOGGING”
unit_of_measurement: “°”
value_template: >
{% set items = value.split('CMD: ')[-1] %}
{% set items = items | from_json %}
{{ items.direction }}
platform: mqtt
name: “Meterology_Wind_Speed”
state_topic: “stat / Meterology_98A39E / LOGGING”
unit_of_measurement: “m / sn”
value_template: >
{% set items = value.split('CMD: ')[-1] %}
{% set items = items | from_json %}
{{ items.speed }}
123
(Taras)
May 10, 2021, 9:05pm
5
If you are interested, you can use the recently introduced concept of Trigger-based Template Sensors . Both of your sensors are subscribed to the same topic so it’s easily done with an MQTT Trigger.
template:
- trigger:
- platform: mqtt
topic: stat/Meterology_98A39E/LOGGING
sensor:
- name: Meterology_Wind_Yon
unit_of_measurement: "°"
state: "{{ (trigger.payload.split('CMD: ')[1] | from_json).direction }}"
- name: Meterology_Wind_Speed
unit_of_measurement: "m/sn"
state: "{{ (trigger.payload.split('CMD: ')[1] | from_json).speed }}"