MQTT Sensor - JSON Template

Hi All,

I am having a bit of trouble getting a value_template to work correctly. I have a JSON coming through an MQTT with a list of sensors (13 total) and need to parse the value of each to individual sensors. I can parse the value in the template tool using {{ value_json.System.Slot[0].Module.Sensor.Value }}, but cant get it to work in a sensor.

Here is the full json;

{
  "System" : {
    "Slot" : [ {
      "ID" : "1",
      "Module" : {
        "ID" : "05",
        "Name" : "SL1",
        "CustomName" : { },
        "Sensor" : {
          "Name" : "Temp",
          "CustomName" : { },
          "Value" : "76.9",
          "Units" : "F"
        }
      }
    }, {
      "ID" : "2",
      "Module" : {
        "ID" : "05",
        "Name" : "SL1",
        "CustomName" : { },
        "Sensor" : {
          "Name" : "pH",
          "CustomName" : { },
          "Value" : "8.10",
          "Units" : { }
        }
      }
    }, {
      "ID" : "3",
      "Module" : {
        "ID" : "05",
        "Name" : "SL1",
        "CustomName" : { },
        "Sensor" : {
          "Name" : "ORP",
          "CustomName" : { },
          "Value" : "442",
          "Units" : "mV"
        }
      }
    }, {
      "ID" : "4",
      "Module" : {
        "ID" : "03",
        "Name" : "SL2",
        "CustomName" : { },
        "Sensor" : {
          "Name" : "Temp",
          "CustomName" : { },
          "Value" : "77.4",
          "Units" : "F"
        }
      }
    }, {
      "ID" : "5",
      "Module" : {
        "ID" : "03",
        "Name" : "SL2",
        "CustomName" : { },
        "Sensor" : {
          "Name" : "pH",
          "CustomName" : { },
          "Value" : "8.31",
          "Units" : { }
        }
      }
    }, {
      "ID" : "6",
      "Module" : {
        "ID" : "03",
        "Name" : "SL2",
        "CustomName" : { },
        "Sensor" : {
          "Name" : "Salinity",
          "CustomName" : { },
          "Value" : "Error",
          "Units" : "ppt"
        }
      }
    }, {
      "ID" : "7",
      "Module" : {
        "ID" : "07",
        "Name" : "PC4",
        "CustomName" : "Sump-D",
        "Sensor" : {
          "Name" : "CH1",
          "CustomName" : "MainPump",
          "Value" : "ON",
          "Units" : { }
        }
      }
    }, {
      "ID" : "8",
      "Module" : {
        "ID" : "01",
        "Name" : "PC4",
        "CustomName" : "Sump-B",
        "Sensor" : {
          "Name" : "CH2",
          "CustomName" : "Skimmer1",
          "Value" : "ON",
          "Units" : { }
        }
      }
    }, {
      "ID" : "9",
      "Module" : {
        "ID" : "01",
        "Name" : "PC4",
        "CustomName" : "Sump-B",
        "Sensor" : {
          "Name" : "CH3",
          "CustomName" : "Skimmer2",
          "Value" : "ON",
          "Units" : { }
        }
      }
    }, {
      "ID" : "10",
      "Module" : {
        "ID" : "01",
        "Name" : "PC4",
        "CustomName" : "Sump-B",
        "Sensor" : {
          "Name" : "CH1",
          "CustomName" : "WM-B",
          "Value" : "ON",
          "Units" : { }
        }
      }
    }, {
      "ID" : "11",
      "Module" : {
        "ID" : "07",
        "Name" : "PC4",
        "CustomName" : "Sump-D",
        "Sensor" : {
          "Name" : "CH2",
          "CustomName" : "WM-C",
          "Value" : "ON",
          "Units" : { }
        }
      }
    }, {
      "ID" : "12",
      "Module" : {
        "ID" : "06",
        "Name" : "PC4",
        "CustomName" : "LED 2",
        "Sensor" : {
          "Name" : "Amps",
          "CustomName" : { },
          "Value" : "1.0",
          "Units" : "A"
        }
      }
    }, {
      "ID" : "13",
      "Module" : {
        "ID" : "02",
        "Name" : "PC4",
        "CustomName" : "LED 1",
        "Sensor" : {
          "Name" : "Amps",
          "CustomName" : { },
          "Value" : "2.0",
          "Units" : "A"
        }
      }
    } ]
  }
}

Appreciate the help
Cody-

What do you mean you can’t get it to work in a sensor?
Can you share your sensor code?

I guess that would of been helpful huh, forgot that part. And what I mean by it doesn’t work in a sensor, is the above template (and other variation I tried) doesn’t return values, just “unknown”. Here is the sensor:

  - platform: mqtt
    name: fish_tank_SL1_temp
    state_topic: "home/sensors/fish tank/reefkeeper/all"
    value_template: '{{ value_json.System.Slot[0].Module.Sensor.Value }}' #Works in Template Tool
    #value_template: '{{ value_json.System.Slot[0].Module[0].Sensor[0].Value }}'
    #value_template: '{{ value_json["System"]["Slot"]["Module"]["Sensor"]["Value"] }}'

The above 3 value templates are what I have tried so far, none work

Nevermind! I got it working:

value_template: ‘{{ value_json[“System”][“Slot”][0][“Module”][“Sensor”][“Value”] }}’

1 Like