Hello guys,
I know that there are many topics on templating subjects and believe me I went through a lot of them!
I have in my configuration.yaml an MQTT sensor:
My goal would be to extract âHumidityâ as entity and the rest of the values as attributes of this entity.
At the moment the only thing that I got with the code in the configuration.yaml is: "unkwnown"
Since I was not even able to extract one value from the json string I stop there start searching for solutions and as I said couldnât find anythng meaningful.
Just as a check, an MQTT topic is usually of the form xxx/yyy/zzz. It doesnât have to be, but just wanted to double-check first to make sure that your topic was not the issue.
Is this definitely what is posted on the MQTT topic, and you havenât made any mistakes? Because the % at the end of this string of text, makes this invalid JSON which Home Assistant wonât be able to parse.
feedbacks clear⌠this is exactly the content of the message that I receive from the MQTT sensor. From my understanding there is a â%â simbol that is not in line with the JSON format as well as the unquoted strings. Thanks for your feedbacks, Iâll come back when I will have the JSON formt corrected.
Now I am back to my original questions: how to extract from the MQTT payload âHumidityâ as entity and the rest of the values as attributes of this entity.
This should identical. I.e. if it can be converted from json into value_json, then itâs already a json string, so thereâs no reason to double convert
Interestingly, the example in the documentation uses to_json even when the sample payload contains an obvious JSON dictionary. Its use in this situation seems redundant to me as well but I deferred to the example.
Observations originally reported in this post cannot be duplicated so I have collapsed it (content retained for the curious).
Original post
Actually, you should not have to include json_attributes_topic. Or at least you did not have to include it in the past. If it was left out, the sensor would simply default to using the state_topic as the source for attributes.
However, its new behavior differs from the past and may due to a bug. Hereâs what I mean:
I can confirm your observation that when the MQTT Sensor is first created, if you leave out json_attributes_topic it fails to create attributes from the payload received via the state_topic. Afterwards, if you addjson_attributes_topic it will then create attributes.
However, I then removedjson_attributes_topic (reloaded âManually created MQTT entitiesâ, as always, to load the new configuration) and the sensor continued to create attributes and report their correct values.
Without json_attributes_topic
Attributes are still created and updated with the new published values.
Just to confirm I wasnât imagining things, I restarted Home Assistant, published a new payload and the sensor reported attributes ⌠all without a json_attributes_topic. Thatâs the correct behavior it always had ⌠until now when it fails to do this when the sensor is initially created.
I repeated the test to confirm my observations.
I removed the MQTT Sensorâs configuration, reloaded, and it disappeared from the system.
I added the MQTT Sensorâs configuration (without json_attributes_topic), reloaded, and it appeared (with no value or attributes).
I published a payload and the sensor reported a value but no attributes.
I added json_attributes_topic to the sensorâs configuration and reloaded.
I published a new payload and the sensor reported a value and attributes.
I removed json_attributes_topic from the sensorâs configuration and reloaded.
I published a new payload and the sensor reported a value and attributes (i.e. without having a json_attributes_topic).
This new behavior suggests itâs a bug that occurs exclusively when the MQTT Sensor is initially created.
If you have a moment to spare, see if you can duplicate my results.
Confirm your existing sensor is currently reporting attribute values.
Remove the following line and then reload âManually created MQTT entitiesâ
json_attributes_topic: "sms_wf1_out_tx-o-txsoil1"
Publish a new payload to sms_wf1_out_tx-o-txsoil1.
Confirm the sensor continues to report attribute values.
Let me know your results. If they are identical to mine, I will report this as a bug in the GitHub Core repository. In addition, let me know which version of Home Assistant you are using.
UPDATE
Well, oddly enough, I can no longer replicate my own results.
Iâll have to chalk it up to a mistake in my testing procedure (where exactly, I donât know) because I can no longer get it to work without including json_attributes_topic. Very strange because I swear it used to work without it in previous versions. Oh well.
I have been trying for hours without success to somehow integrate a json including attributes as a sensor, but so far without any success. I have tried all the different configurations that I found here in the forum, but it still doesnât work. I can retrieve the json as text, but not as individual values.
The json looks something like this: