MQTT YAML Switch Entity state value not working

Hey!
i`m trying to extract a value from a nested json object. The config is via yaml file. The command topic is working. The state topic isn´t working (value of entity is always “unknown”)

here is the yaml config:

- unique_id: sw13
  name: "Switch13"
  state_topic: "tele/tasmota_XXXX/SENSOR"
  command_topic: "cmnd/tasmota_XXXX/clientsend"
  availability_topic: "tele/tasmota_XXXX/LWT"
  payload_available: "Online"
  payload_not_available: "Offline"
  payload_on: "S 13=1"
  payload_off: "S 13=0"
  value_template: "{{ value_json.TasmotaClient.13 }}"
  state_on: 1
  state_off: 0
  optimistic: false
  qos: 0
  retain: false

The json response is the following:

{
  "Time": "2024-06-24T21:46:27",
  "TasmotaClient": {
    "0": 0,
    "1": 0,
    "2": 0,
    "3": 0,
    "4": 0,
    "5": 0,
    "6": 0,
    "7": 0,
    "8": 0,
    "9": 0,
    "10": 0,
    "11": 0,
    "12": 0,
    "13": 0,
    "14": 0,
    "15": 0,
    "16": 0,
    "17": 0,
    "18": 0,
    "19": 0
  }
}

Use ['13'] not .13. Always safer to use bracket notation.

value_template: "{{ value_json['TasmotaClient']['13'] }}"

Your version is parsed as [13] (an index), which isn’t applicable to a dictionary.

@Troon thanks, it works!

Do you also know why the entity id is not sw13 as configured?
With the above config the entity id is generated as modified friendly name: switch.switch13

That’s not “as configured”. The unique_id has nothing to do with the generated entity ID. I use random UUIDs for mine, such as:

unique_id: bf97c786-9fbe-433b-82ac-6d8904b56752

See name and object_id in this section.

ok, now i understood the configuration, thanks!!

here`s a link for other users which have the same issue like me:

1 Like