Rest Sensor nested json

Hi All :slight_smile:

Please help me out, i spent more then half of the weekend trying to get some sensors into HA :sweat:

The json is

{
   "Body" : {
      "Data" : {
         "Current_AC_Phase_1" : 5.1689999999999996,
         "Current_AC_Phase_2" : 1.54,
         "Current_AC_Phase_3" : 1.091,
         "Enable" : 1,
         "EnergyReactive_VArAC_Sum_Consumed" : 14350,
         "EnergyReactive_VArAC_Sum_Produced" : 261980,
         "EnergyReal_WAC_Minus_Absolute" : 42,
         "EnergyReal_WAC_Plus_Absolute" : 55303,
         "EnergyReal_WAC_Sum_Consumed" : 55303,
         "EnergyReal_WAC_Sum_Produced" : 42,
         "Frequency_Phase_Average" : 49.899999999999999,
         "Meter_Location_Current" : 0,
         "PowerApparent_S_Phase_1" : 1218.8501999999999,
         "PowerApparent_S_Phase_2" : 371.91000000000003,
         "PowerApparent_S_Phase_3" : 259.76709999999997,
         "PowerApparent_S_Sum" : 1624,
         "PowerFactor_Phase_1" : 0.96999999999999997,
         "PowerFactor_Phase_2" : 0.67000000000000004,
         "PowerFactor_Phase_3" : 0.81000000000000005,
         "PowerFactor_Sum" : 0.92000000000000004,
         "PowerReactive_Q_Phase_1" : -233.18000000000001,
         "PowerReactive_Q_Phase_2" : -243.66,
         "PowerReactive_Q_Phase_3" : -135.13999999999999,
         "PowerReactive_Q_Sum" : -611.98000000000002,
         "PowerReal_P_Phase_1" : 1093.6700000000001,
         "PowerReal_P_Phase_2" : 223.46000000000001,
         "PowerReal_P_Phase_3" : 188.55000000000001,
         "PowerReal_P_Sum" : 1505.6800000000001,
         "TimeStamp" : 1604853646,
         "Visible" : 1,
         "Voltage_AC_PhaseToPhase_12" : 413.39999999999998,
         "Voltage_AC_PhaseToPhase_23" : 415.30000000000001,
         "Voltage_AC_PhaseToPhase_31" : 410.39999999999998,
         "Voltage_AC_Phase_1" : 235.80000000000001,
         "Voltage_AC_Phase_2" : 241.5,
         "Voltage_AC_Phase_3" : 238.09999999999999
      }
   },
   "Head" : {
      "RequestArguments" : {
         "DeviceClass" : "Meter",
         "DeviceId" : "0",
         "Scope" : "Device"
      },
      "Status" : {
         "Code" : 0,
         "Reason" : "",
         "UserMessage" : ""
      },
      "Timestamp" : "2020-11-08T18:40:46+02:00"
   }
}

I know how to get the info by doing separate api calls, but the amount of calls is way too much, so i am trying to get all the sensors from Body.Data in one API call.

Please help if you can, i am not so new at HA but this i just can not figure it out :cry:

Thank you !

json_attributes_path: '$.Body'
json_attributes:
  - Data

Then use template sensors to extract the attributes to their own sensors if needed.

- platform: rest
  name: FroniusMeter
  resource: http://192.168.1.5/solar_api/v1/GetMeterRealtimeData.cgi?Scope=Device&DeviceID=0
  value_template: 'OK' 
  json_attributes_path: '$.Body'
  json_attributes:
    - Data
  scan_interval: 60
  
- platform: template
  sensors: 
    PowerReal_P_Phase_1:
      value_template: "{{state.sensor.FroniusMeter.Data.PowerReal_P_Phase_1}}"

Doesnt work. For sure i am messing it up ;(

Which sensor isn’t working ?
Any errors ?

I have the fronius meter & inverter integrated with a rest sensor also.

On my phone which won’t let me copy / paste.
When I get to a PC I’ll post examples :+1:

All sensors are working IF i make a separate API call for each. What i am trying to do is to make one API call and get all the sensors. But i need lots of guidance for this :slight_smile:

Sensor entity ids are all lower case, no capital letters

I tried all ways…

#      value_template: "{{states.sensor.FroniusMeter.Data.PowerReal_P_Phase_1}}"
#       value_template: "{{state.sensor.froniusmeter.data.powerreal_p_phase_1}}"
      value_template: "{{states.sensor.froniusmeter.Data.PowerReal_P_Phase_1}}"

error still there

Invalid config for [sensor.template]: invalid slug PowerReal_P_Phase_1 (try powerreal_p_phase_1) for dictionary value @ data[‘sensors’]. Got OrderedDict([(‘PowerReal_P_Phase_1’, OrderedDict([(‘value_template’, ‘{{states.sensor.froniusmeter.Data.PowerReal_P_Phase_1}}’)]))]). (See ?, line ?).

Sorry to hijack this thread but I have a similar problem…I think.

I have created a REST switch for a Shelly 1PM that is outside of my LAN and controlled by the Shelly cloud. The switch does turn the light on but doesn’t report the state correctly. The issue (i’m pretty sure) is with my formatting of the JSON to extract the "ison": true

This is what I currently have in my REST switch config

 is_on_template: '{{ value_json.data.device_status.relays.ison == true}}'

This is the JSON response from the GET request. Note the json is nested inside data/device_status/relays

{
    "isok": true,
    "data": {
        "online": true,
        "device_status": {
            "fs_size": 233681,
            "unixtime": 1604855953,
            "ext_sensors": [],
            "fs_free": 148090,
            "serial": 372,
            "tmp": {
                "tC": 40.68,
                "tF": 105.23,
                "is_valid": true
            },
            "time": "17:19",
            "ext_humidity": [],
            "mqtt": {
                "connected": false
            },
            "ext_temperature": [],
            "has_update": false,
            "_updated": "2020-11-08 06:19:13",
            "wifi_sta": {
                "connected": true,
                "ssid": "pedantic_house",
                "ip": "10.0.0.12",
                "rssi": -47
            },
            "actions_stats": {
                "skipped": 0
            },
            "cfg_changed_cnt": 0,
            "mac": "D8BFC01A15E6",
            "uptime": 18669,
            "meters": [
                {
                    "power": 0,
                    "overpower": 0,
                    "is_valid": true,
                    "timestamp": 1604855953,
                    "counters": [
                        0,
                        9.128,
                        10.963
                    ],
                    "total": 1310
                }
            ],
            "inputs": [
                {
                    "input": 0,
                    "event": "S",
                    "event_cnt": 2
                }
            ],
            "ram_total": 50712,
            "relays": [
                {
                    "ison": true,
                    "has_timer": false,
                    "timer_started": 0,
                    "timer_duration": 0,
                    "timer_remaining": 0,
                    "overpower": false,
                    "source": "http"
                }
            ],
            "getinfo": {
                "fw_info": {
                    "device": "shelly1pm-D8BFC01A15E6",
                    "fw": "20200827-070450/v1.8.3@4a8bc427"
                }
            },
            "update": {
                "status": "idle",
                "has_update": false,
                "new_version": "20200827-070450/v1.8.3@4a8bc427",
                "old_version": "20200827-070450/v1.8.3@4a8bc427"
            },
            "temperature": 40.68,
            "ram_free": 39196,
            "cloud": {
                "enabled": true,
                "connected": true
            },
            "overtemperature": false
        }
    }
}

Could someone please help me with the correct formatting? Thanks!

 - platform: rest
   name: meter
   resource: http://192.168.1.49/solar_api/v1/GetMeterRealtimeData.cgi?Scope=System
   json_attributes:
     - Body
   value_template: '{{ value_json.value }}'
   headers:
     Content-Type: application/json
   scan_interval: 60
 - platform: template
   sensors:
     voltage_a:
       friendly_name: 'A Phase'
       value_template: "{{state_attr('sensor.meter','Body')['Data']['0']['Voltage_AC_Phase_1'] | round(0)}}"
       unit_of_measurement: 'V'

Keep in mind the structure of my json is a little different to yours, but you get the general idea

1 Like

{{value_json.data.device_status.relays[0].ison == True}}

1 Like

Cheers:) Finally works ! The difference between yours and mine is the “0” as you interrogate the system while i interrogate the meter and tha gives a shorter json :crazy_face:

1 Like

Great, thankyou!

1 Like

Could you tell me why there needs to be [0] after relays? Sorry I have a poor understanding of json

The way your data is structured has a list for your ‘relay’ data as indicated by the square brackets.
The data you are after is the first ( and only ) item in the list and is referenced by [0]

Thanks! so suppose I wanted to pull the overpower from the json the template would look like this? Overpower being the 5th item in the list

{{value_json.data.device_status.relays[5].overpower == True}

No.

Everything within the curly brackets { } is the first list item.
You want

{{value_json.data.device_status.relays[0].overpower == false}}

A list looks like this

a_list = [ item1, item2, item3,....]

The list from your json contains more json

your_list = [{key1 : "value1", "key2":"value2"}, item2, item3 ]

Apologies if this doesnt make sense; I’m not a programmer :thinking: