Get json.attribute for template

Hi,

my wifi bbq thermometer publishes a json with all the values that I use for the Rest integration. This is what the json looks like:

{
  "temp_unit": "celsius",
  "pit": {
    "enabled": false
  },
  "pit2": {
    "enabled": false
  },
  "cpu_load": 61.671836714703,
  "cpu_temp": 57.8,
  "channel": {
    "0": {
      "temp": 29.92,
      "color": "green",
      "state": "ok",
      "temp_min": 0,
      "temp_max": 200,
      "name": "Garraum",
      "alert": false,
      "show": true
    },
    "1": {
   [...]

I am trying to set the template to the temp attribute for the entity state the same way I did it with the values in the json root (cpu_temp, cpu_load …) but I am not able to get the value. Tried different syntax and approaches (see below and way more) but nothing works.

    sensor:
      - name: "WlanThermo CPU-Temp"
        device_class: temperature
        unit_of_measurement: "°C"
        value_template: "{{ value_json.cpu_temp | round(1) }}"
      - name: "WlanThermo CPU-Last"
        unit_of_measurement: "%"
        value_template: "{{ value_json.cpu_load | round(0) }}"
      - name: "WlanThermo Zeitstempel"
        device_class: timestamp
        value_template: "{{ value_json.timestamp }}"
      - name: "Kanal 0"
        # value_template: "{{ value_json['channel']['0']['temp'] | round(1) }}"
        json_attributes_path: "$.channel[0]"
        # value_template: "{{ value_json | map(attribute='temp') | first | default }}"
        # device_class: temperature
        # unit_of_measurement: "°C"
        # value_template: "OK"
        json_attributes:
          - "temp"
          - "color"
          - "state"
          - "temp_min"
          - "temp_max"
          - "name"
          - "alert"
          - "show"

How do I properly set the value_templates for the specific channels?

So your three WlanThermo sensors work, but you can’t get the state to work for Kanal 0, correct? The first value_template works here:

but I think your problem is your json_attributes_path which should include ['0'] not [0]. My guess is that this is causing the whole sensor to fail.

This tool helps. Yours:

Then fixed with ['0'] (key name rather than index):

1 Like

Thank you. Weirdly the [0] in json_attributes_path seems to work for the attributes:

I just tried again with that template and now got it to work. No idea what I did wrong yesterday though as I was sure I tried this.

      - name: "Kanal 0"
        device_class: temperature
        unit_of_measurement: "°C"
        value_template: "{{ value_json['channel']['0']['temp'] | round(1) }}"
        json_attributes_path: "$.channel[0]"
        json_attributes:
          - "temp"
          - "color"
          - "state"
          - "temp_min"
          - "temp_max"
          - "name"
          - "alert"
          - "show"

Follow up question: Is it possible to put a “if … then” statement into templates? E.g. if attribute “state” is “unknown” show “Offline” else show the current temp?

Huh, perhaps because it’s the first key and HA is less strict than the JSONPath tool?

You can build whatever logic you want into the template. You can’t have non-numeric values with device_class and unit_of_measurement though — use the availability feature instead (docs):

availability: "{{ value_json is defined and value_json['channel']['0']['state'] != 'unknown' }}"
1 Like