Syntax for JSON to capture this attribute

Very new to this, trying to figure out the correct syntax to capture data from a JSON array to create a sensor for a home theater processor (Monolith HTP-1). I want to output the custom label from whatever the current selected input is.

The relevant parts of the JSON array are here:

   "inputs": {
      "h1": {
         "label": "NVIDIA Shield"
      },
      "h2": {
         "label": "Computer"
      },
      "h5": {
         "label": "Nintendo"
      }
    },
   "input": "h5"
 

So I need value_template: " {{ value_json.inputs.value from .input.label }} " The JSON here is chopped up and there are more inputs.

Not sure how to write this.

It would be better to show the whole JSON, but if what you’ve shown is the dictionary x, you want:

{{ x['inputs'][x['input']]['label'] }}

Disclaimer: written on a phone after a nice bottle of rioja.

Sorry, the whole JSON is like 4000 lines long so I cut it down.

Right now, I have sensors that pull info like this:

    sensor:
      - name: "HTP-1 Volume"
        value_template: "{{ value_json.volume }}"
        unit_of_measurement: "dB"
      - name: "HTP-1 Loudness status"
        value_template: "{{ value_json.loudness }}"
      - name: "HTP-1 Loudness level"
        value_template: "{{ value_json.loudnessCal }}"
      - name: "HTP-1 Videoresolution"

I wanted to make a sensor called “HTP-1 Input” that outputs the label name of the input, which can be changed. However, the inputs are the same but they aren’t numbered in the JSON, they are labeled depending on input type (h1, for example, is the HDMI 1 input).

Right, so where is inputs in the structure? Is it value_json.inputs, or deeper than that?

Whatever it is, just write a value_template with the appropriate substitution for my x.

inputs is top level. It would be value_json.inputs, as you say.

So the correct code would be

value_template: {{ value_json['inputs'][value_json['input']]['label'] }}

I just don’t know the syntax when I was using periods in between before. Do I not need that with the brackets?

You need to enclose the template in quotes:

value_template: "{{ value_json['inputs'][value_json['input']]['label'] }}"

Dot and bracket notation are usually equivalent, but bracket notation is safer:

foo.bar == foo['bar']
foo.values != foo['values'] # reserved keyword
foo.3bears != foo['3bears'] # index misinterpretation 

and you need it when including variables like we are here.

That worked perfectly. Thank you! And thank you for the additional tutorial on syntax. That will make things much easier.

I have shell commands that can control the device, so my next task is to make a template for it so I can control everything from a card.

1 Like