Rest sensor and value_template - Why this syntax doesn't work?

I have a shelly 1pm whose status include the current power consumption.
The request is very simple: http://IP_shelly/status and the answer is a simple json:

{
  "wifi_sta": {
    "connected": true,
    "ssid": "my_home_ssid",
    "ip": "10.0.0.1",
    "rssi": -80
  },
  "cloud": {
    "enabled": false,
    "connected": false
  },
  "mqtt": {
    "connected": true
  },
  "time": "17:47",
  "unixtime": 1593366439,
  "serial": 1,
  "has_update": false,
  "mac": "A4CF123BA42C",
  "relays": [
    {
      "ison": true,
      "has_timer": false,
      "timer_started": 0,
      "timer_duration": 0,
      "timer_remaining": 0,
      "overpower": false
    }
  ],
  "meters": [
    {
      "power": 22.04,
      "is_valid": true,
      "timestamp": 1593366439,
      "counters": [
        22.112,
        22.121,
        22.096
      ],
      "total": 29434
    }
  ],
  "inputs": [
    {
      "input": 0,
      "event": "",
      "event_cnt": 0
    }
  ],
  "ext_sensors": {},
  "ext_temperature": {},
  "ext_humidity": {},
  "temperature": 41.72,
  "overtemperature": false,
  "tmp": {
    "tC": 41.72,
    "tF": 107.1,
    "is_valid": true
  },
  "update": {
    "status": "idle",
    "has_update": false,
    "new_version": "20200601-123334/v1.7.0@d7961837",
    "old_version": "20200601-123334/v1.7.0@d7961837"
  },
  "ram_total": 49920,
  "ram_free": 38332,
  "ram_lwm": 32656,
  "fs_size": 233681,
  "fs_free": 152357,
  "uptime": 81503
}

I am interested in getting the “power” of the above json.

I have tried this:

value_template: "{{value_json.meters.power}}"

which doesn’t work. I found in another post and the correct answer is:

value_template: "{{value_json['meters'][0]['power']}}"

Then my question is why, the plain and simple parsing of the json object doesn’t work when the complex (yet correct) template works.

What is the reason of this ?

GV

The section you’re referencing is an array (list) as denoted by the square brackets. The array just happens to only have only one item in it (a dictionary containing power, is_valid, etc.). Since it’s the first item in the array, you need to do

{{ value_json.['meters'][0]['power'] }}

Note how cloud and meters differ. cloud just contains a single dictionary, so if you wanted to grab the value for enabled you would do

{{ value_json.cloud.enabled }}
1 Like

This would also work:

value_template: "{{value_json.meters[0].power}}"

The reason your original attempt didn’t work is, value_json.meters is an array, not a dictionary. So you have to use the [] operator to select an element of the array. Then, once you do that, you get a dictionary which has power as a key.

1 Like

@pnbruckner and @Tediore thank you for your answers.
What really confused me is that the syntax value_json.meters.power works when using jsonata.
Most of my automations are using nodered and I am using a lot jsonata.
So, the json parsing in HA is not “compliant” with jsonata… I guess it would be a good addition :slight_smile:

GV