Json statement

Hello,

I have been using homeassistant for many years and haven’t upgraded my installation for quite a while. I am still using the MQTT definitions in the configuration file. I recently reset an outdoor switch unit on accident and rather than re-programming it I have found the MQTT data from the monitor and have utilized that to restate my switch in the config file. The switch can be tuned on and off, but the switch status doesn’t updated.

The problem that I am having is figuring out how to get the status from the status message properly. It has a very old version of Espurna on it and the status message is:

{“button/0”:“2”,“relay/0”:“0”,“time”:“2023-05-22 21:09:57”,“host”:“ESPURNA_054547”,“ip”:“192.168.1.248”}

I am trying to get the relay status with the following MQTT config, this is under a “Switch” heading as if have many switches defined.

  • platform: mqtt
    name: “Outdoor_2”
    state_topic: “ESPURNA_054547/data”
    value_template: “{{value_json.relay\/0}}”
    command_topic: “ESPURNA_054547/relay/0/set”
    qos: 1
    payload_on: “1”
    payload_off: “0”
    state_on: “1”
    state_off: “0”
    optimistic: false

It seems that home assistant isn’t picking up the /0 as part of the variable it is looking for in the message even though is shows it as the string it is looking for. Below is the error message that I get when it evaluates the status message.

  • Error parsing value: ‘dict object’ has no attribute ‘relay’ (value: {“button/0”:“2”,“relay/0”:“0”,“time”:“2023-05-22 21:09:57”,“host”:“ESPURNA_054547”,“ip”:“192.168.1.248”}, template: {{value_json.relay/0}})

How do I format the Json statement so it looks for the proper “relay/0” variable. I finally figured out that the preceeding “\” was required but the system is still only looking for “relay” instead of “relay/0”

Any help would be greatly appreciated.

TM Harris

  1. format your code properly next time with the three back-ticks ```
  2. you can try this out in your devtools > templates
{% set my_test_json = {"button/0":"2","relay/0":"0","time":"2023-05-22 21:09:57","host":"ESPURNA_054547","ip":"192.168.1.248"} %}

{{ my_test_json["relay/0"] }}

Thank you for your response. After giving things a break I ask the question to Chat GBT and actually got the correct answer. I was stunned…

Again thanks for taking the time to respond. It was the brackets that I was missing.

TMHarris

Chatgpt is more than often wrong and this is already a topic stating to be careful.
So if it helped this time … fine of course … but donot expect support on chatgpt suggested stuff :slight_smile:

Just for my info too…how did you ask chatgpt for the solution?

I was pretty amazed that I got a correct answer with the information that I put in…

Thanks and… nicely supported with text too …should one be getting afraid for loosing their jobs :thinking: