This unfortunately isn’t giving me anything. The sensor shows as unavailable. If I get rid of attributes’ template, I’m able to see the state value.
Any idea what I’m doing wrong? I’m guessing maybe the square brackets (the “rxInfo” section of the payload) might have something to do with this - not sure.
rssi and snr are keys in a dictionary. The dictionary itself is the zeroth item of a list in the rxInfo key. So you need to indicate you want the zeroth item of rxInfo
ie. I’m calling upon just the one attribute here (rssi and not time and snr)
I see the state value and rssi as an attribute. When I add the comma and include other attributes, it doesn’t show.
What I suggested is in the same format as what I have suggested to other users (and tested myself) and it has been confirmed to work.
I agree it’s weird that it works when specifying just one attribute but not for two attributes. It shouldn’t make a difference and the technique has been successfully tested with far more than just two attributes.
As I mentioned earlier, the main problem was that your original template didn’t refer to the zeroth item in the list. Now all that remains is to determine why your MQTT Sensor misbehaves when supplied with a valid template.
BTW, you need to confirm the received payload is always valid JSON otherwise we’re chasing ghosts here.
EDIT
In addition, check the log for warnings and errors related to your MQTT Sensor.
The first sensor works correctly and confirms what I suggested is valid; it reports state and three attributes.
The second sensor reports state but none of the attributes. It also reports the following warning in the Log:
tl;dr
When supplied with a valid JSON payload, the MQTT Sensor configuration I posted above works correctly. The resulting sensor displays state and three attributes (Time, RSSI, and SNR).
Huh, that’s interesting. I wonder if the “erroneous JSON” is related to the Jinja template outputting single-quoted keys despite the doubles in the template?
Feels like a bug that a template that outputs a dict fails, whereas a string with embedded templates that looks like a dict passes…
I suspect my version might be able to be cajoled into working with to_json — might have a go later. That feels even more wrong though.
So whether this way or the way I originally suggested, the resulting MQTT Sensor is able to have three attributes. It’s unclear why the OP fails to get positive results with a proven solution.
EDIT
To be clear, it doesn’t work with the payload the OP posted because it’s invalid JSON (contains three errors).
Thanks for trying it — that means json_attributes_template needs to be a JSON string rather than a Jinja template, although it supports templates in its construction.
It’s an unusual feature, only (I think) found on MQTT. I seem to recall it being proposed and rejected on REST, where it would be useful for processing responses that aren’t dicts at their top level.
When json_attributes_template was first released (it was preceded by a far more limited way to get attributes; like in REST) I recall having to do a fair bit of experimentation to be able to extract values and rename keys. The example in the docs is kind of skimpy.
Anyways, at this point it’s up to the OP to explain why the posted payload is invalid and what are the related errors/warnings (if any) in the Log.
And it’s working fine. It’s showing me the state value + all 3 attributes. I really have no idea why it didn’t work the first time.
This isn’t the first sensor that I’m seeing experience this issue.
I currently have another sensor (with a much smaller payload and without any square brackets) that’s giving me the exact same problem even though the json attributes template I’m using has the exact same format (only different variable names).
I’m thinking it’s probably something else and not to do with the syntax I’m using.
Anyways, thanks a lot for suggesting rxInfo[0] - that solves this for me.