Getting json data from API

Hi,

Am I a little new to this rest sensor stuff, so forgive me for my “stupid” questions.

How can I get this json data properly into a sensor that lists each serial number as a seperate attribute, or even as a separate sensor?

[
  {
    "serialNumber": "122022009125",
    "lastReportDate": 1605626962,
    "devType": 1,
    "lastReportWatts": 0,
    "maxReportWatts": 198
  },
  {
    "serialNumber": "122022007165",
    "lastReportDate": 1605626903,
    "devType": 1,
    "lastReportWatts": 0,
    "maxReportWatts": 215
  },
  {
    "serialNumber": "122022010765",
    "lastReportDate": 1605626959,
    "devType": 1,
    "lastReportWatts": -1,
    "maxReportWatts": 200
  },
  {
    "serialNumber": "122022007141",
    "lastReportDate": 1605626838,
    "devType": 1,
    "lastReportWatts": 0,
    "maxReportWatts": 216
  },
  {
    "serialNumber": "122022003320",
    "lastReportDate": 1605627351,
    "devType": 1,
    "lastReportWatts": -2,
    "maxReportWatts": 160
  },
  {
    "serialNumber": "122022009897",
    "lastReportDate": 1605626917,
    "devType": 1,
    "lastReportWatts": 0,
    "maxReportWatts": 130
  },
  {
    "serialNumber": "122022008144",
    "lastReportDate": 1605626947,
    "devType": 1,
    "lastReportWatts": 0,
    "maxReportWatts": 197
  },
  {
    "serialNumber": "122022009149",
    "lastReportDate": 1605626941,
    "devType": 1,
    "lastReportWatts": 0,
    "maxReportWatts": 204
  },
  {
    "serialNumber": "122022009197",
    "lastReportDate": 1605626948,
    "devType": 1,
    "lastReportWatts": 0,
    "maxReportWatts": 197
  },
  {
    "serialNumber": "122022009871",
    "lastReportDate": 1605626856,
    "devType": 1,
    "lastReportWatts": -1,
    "maxReportWatts": 124
  },
  {
    "serialNumber": "122022009122",
    "lastReportDate": 1605626913,
    "devType": 1,
    "lastReportWatts": 0,
    "maxReportWatts": 201
  },
  {
    "serialNumber": "122022009124",
    "lastReportDate": 1605626959,
    "devType": 1,
    "lastReportWatts": -1,
    "maxReportWatts": 199
  },
  {
    "serialNumber": "122022009643",
    "lastReportDate": 1605627346,
    "devType": 1,
    "lastReportWatts": -2,
    "maxReportWatts": 121
  },
  {
    "serialNumber": "122022009395",
    "lastReportDate": 1605626907,
    "devType": 1,
    "lastReportWatts": 0,
    "maxReportWatts": 122
  },
  {
    "serialNumber": "122022009244",
    "lastReportDate": 1605626963,
    "devType": 1,
    "lastReportWatts": -1,
    "maxReportWatts": 197
  },
  {
    "serialNumber": "122022009799",
    "lastReportDate": 1605626935,
    "devType": 1,
    "lastReportWatts": 0,
    "maxReportWatts": 159
  },
  {
    "serialNumber": "122022009268",
    "lastReportDate": 1605626878,
    "devType": 1,
    "lastReportWatts": 0,
    "maxReportWatts": 214
  }
]

And how to do more or less the same for this:

{
   "production":[
      {
         "type":"inverters",
         "activeCount":17,
         "readingTime":1605637864,
         "wNow":0,
         "whLifetime":36408
      },
      {
         "type":"eim",
         "activeCount":1,
         "measurementType":"production",
         "readingTime":1605637885,
         "wNow":15.833,
         "whLifetime":566.661,
         "varhLeadLifetime":0.0,
         "varhLagLifetime":1241.524,
         "vahLifetime":1740.122,
         "rmsCurrent":1.167,
         "rmsVoltage":667.566,
         "reactPwr":231.073,
         "apprntPwr":259.947,
         "pwrFactor":0.06,
         "whToday":521.661,
         "whLastSevenDays":521.661,
         "vahToday":1675.122,
         "varhLeadToday":0.0,
         "varhLagToday":1221.524
      }
   ],
   "consumption":[
      {
         "type":"eim",
         "activeCount":1,
         "measurementType":"total-consumption",
         "readingTime":1605637885,
         "wNow":481.115,
         "whLifetime":5040.974,
         "varhLeadLifetime":2810.136,
         "varhLagLifetime":1246.201,
         "vahLifetime":6564.788,
         "rmsCurrent":4.557,
         "rmsVoltage":667.464,
         "reactPwr":-675.698,
         "apprntPwr":3041.9,
         "pwrFactor":0.16,
         "whToday":4890.974,
         "whLastSevenDays":4890.974,
         "vahToday":6025.788,
         "varhLeadToday":2498.136,
         "varhLagToday":1225.201
      },
      {
         "type":"eim",
         "activeCount":1,
         "measurementType":"net-consumption",
         "readingTime":1605637885,
         "wNow":465.283,
         "whLifetime":4558.574,
         "varhLeadLifetime":2810.136,
         "varhLagLifetime":4.677,
         "vahLifetime":6564.788,
         "rmsCurrent":3.391,
         "rmsVoltage":667.362,
         "reactPwr":-444.624,
         "apprntPwr":756.11,
         "pwrFactor":0.63,
         "whToday":0,
         "whLastSevenDays":0,
         "vahToday":0,
         "varhLeadToday":0,
         "varhLagToday":0
      }
   ],
   "storage":[
      {
         "type":"acb",
         "activeCount":0,
         "readingTime":0,
         "wNow":0,
         "whNow":0,
         "state":"idle"
      }
   ]
}

Hopefully I will be able to fiddle the rest together myself :slight_smile:

Thanks,

Michel

You can create REST sensor in your configuration.yaml

Take a look at this: https://www.home-assistant.io/integrations/rest/

Let me know if you need anything after looking at that.

I have something similar I would like to tackle. Please post results when you get it working.

Okay since there’s interest, here’s what I’ve done. I have a web service that grabs NEST thermostat info using the new API. MY web service returns this JSON:

{"living_room": {"temperature": 72.4, "humidity": 47, "mode": "OFF"}, "upstairs": {"temperature": 71.8, "humidity": 48, "mode": "COOL"}}

In my configuration.yaml , I have the following REST sensor:

sensor: 
    - platform: rest
      name: nest_thermos
      resource: URL GOES HERE
      timeout: 15
      scan_interval: 300
      json_attributes:
        - living_room
        - upstairs
      value_template: 'OK'
      
    -  platform: template
       sensors:
         living_room_temperature:
           value_template: '{{ states.sensor.nest_thermos.attributes["living_room"]["temperature"] }}'
           device_class: temperature
           unit_of_measurement: '°F'
           friendly_name: "Living Room temperature"
         living_room_humidity:
           value_template: '{{ states.sensor.nest_thermos.attributes["living_room"]["humidity"] }}'
           device_class: humidity
           unit_of_measurement: '%'
           friendly_name: "Living Room humidity"
         upstairs_temperature:
           value_template: '{{ states.sensor.nest_thermos.attributes["upstairs"]["temperature"] }}'
           device_class: temperature
           unit_of_measurement: '°F'
           friendly_name: "Upstairs temperature"
         upstairs_humidity:
           value_template: '{{ states.sensor.nest_thermos.attributes["upstairs"]["humidity"] }}'
           device_class: humidity
           unit_of_measurement: '%'
           friendly_name: "Upstairs humidity"

And here’s what I get in Lovelace:

3 Likes

You might take a look at my post from some years ago when I had a problem of trying to parse out multiple sensor values from one large JSON payload: Need help storing URL for use in generic IP camera (over 255 chars)

This might give you some ideas. I found when I was working with that large JSON payload that I couldn’t just put it as the value of a sensor entity because it was too large for a state value. (The idea was that I was going to use multiple other template sensors on the value of the REST sensor payload for the individual data items…) You can have larger attributes associated with an entity as compared to the actual state…

I was aware of that :wink:

The issue is that I don’t know how to get all the data into separate sensors or even into one sensor with separate attributes for this specific json data.

Each “serialNumber:” needs to be a al least a separate attribute, or most preferably a separate sensor.
The seperate sensor part I know I can to with separate template sensors. There might be a more elegant solution, but I don’t even know how to get these in separate attributes.

[
  {
    "serialNumber": "122022009125",
    "lastReportDate": 1605626962,
    "devType": 1,
    "lastReportWatts": 0,
    "maxReportWatts": 198
  },
  {
    "serialNumber": "122022007165",
    "lastReportDate": 1605626903,
    "devType": 1,
    "lastReportWatts": 0,
    "maxReportWatts": 215
  },

etc…

to parse the json in a template sensor…

for the first one:

value_template: "{{value_json[0].serialNumber}}

for the second one:

value_template: "{{ value_json.production[1].type}}"

then just substitute the index numbers (the number between the []) and the keys after the dots to extract the specific info that you want. the index numbers are 0 based so the first item in the list will be 0, the next will be 1, etc.

I have this json data. How do I get the temperature in C, humidity and battery for id:c289d47408fd?

{"id":"c289d47408fd","address":"c2:89:d4:74:08:fd","rssi":-81,"serviceData":{"model":"T","modelName":"WoSensorTH","temperature":{"c":22.8,"f":73},"fahrenheit":false,"humidity":70,"battery":100}}
{"id":"f4f839cd40fd","address":"f4:f8:39:cd:40:fd","rssi":-43,"serviceData":{"model":"H","modelName":"WoHand","mode":false,"state":false,"battery":95}}
{"id":"e79341ccf8ea","address":"e7:93:41:cc:f8:ea","rssi":-55,"serviceData":{"model":"H","modelName":"WoHand","mode":false,"state":false,"battery":87}}

Does the data always come in that same order?

you’ll have to try to reformat the data to get it be valid json to make the templates easier.

but try putting the following into the template editor and playing around with it:

{% set value_json = [ {"id":"c289d47408fd","address":"c2:89:d4:74:08:fd","rssi":-81,"serviceData":{"model":"T","modelName":"WoSensorTH","temperature":{"c":22.8,"f":73},"fahrenheit":false,"humidity":70,"battery":100}},
{"id":"f4f839cd40fd","address":"f4:f8:39:cd:40:fd","rssi":-43,"serviceData":{"model":"H","modelName":"WoHand","mode":false,"state":false,"battery":95}},
{"id":"e79341ccf8ea","address":"e7:93:41:cc:f8:ea","rssi":-55,"serviceData":{"model":"H","modelName":"WoHand","mode":false,"state":false,"battery":87}} ]
%}

{{ value_json[0].serviceData.humidity }}
{{ value_json[0].serviceData.battery }}
{{ value_json[0].serviceData.temperature.c }}

Actually no. It is in random order and the id is duplicate. It scans for any Bluetooth devices for 2 seconds. So I need to add a , somehow and maybe sort them as unique alphabetical order so the list isn’t random.

{"id":"e79341ccf8ea","address":"e7:93:41:cc:f8:ea","rssi":-54,"serviceData":{"model":"H","modelName":"WoHand","mode":false,"state":false,"battery":87}}
{"id":"e79341ccf8ea","address":"e7:93:41:cc:f8:ea","rssi":-65,"serviceData":{"model":"H","modelName":"WoHand","mode":false,"state":false,"battery":87}}
{"id":"f4f839cd40fd","address":"f4:f8:39:cd:40:fd","rssi":-44,"serviceData":{"model":"H","modelName":"WoHand","mode":false,"state":false,"battery":97}}
{"id":"e79341ccf8ea","address":"e7:93:41:cc:f8:ea","rssi":-64,"serviceData":{"model":"H","modelName":"WoHand","mode":false,"state":false,"battery":87}}
{"id":"c289d47408fd","address":"c2:89:d4:74:08:fd","rssi":-87,"serviceData":{"model":"T","modelName":"WoSensorTH","temperature":{"c":20.4,"f":68.7},"fahrenheit":false,"humidity":59,"battery":100}}
{"id":"f4f839cd40fd","address":"f4:f8:39:cd:40:fd","rssi":-43,"serviceData":{"model":"H","modelName":"WoHand","mode":false,"state":false,"battery":97}}

Update: I’ve made some progress. I’ve added a comma for each object. Tested in developer -> template and it works. I have yet able to sort by unique and it comes in at random.
Is there a way to specifically call it by it’s id?

Can someone check my codes below if it will work? I have cleaned up the url to only display 3 devices with the help from HA community.

Raw url output. Scans any BLE devices. There will be duplicate IDs.

{"id":"e79341ccf8ea","address":"e7:93:41:cc:f8:ea","rssi":-78,"serviceData":{"model":"H","modelName":"WoHand","mode":false,"state":false,"battery":87}},
{"id":"f4f839cd40fd","address":"f4:f8:39:cd:40:fd","rssi":-42,"serviceData":{"model":"H","modelName":"WoHand","mode":false,"state":false,"battery":97}},
{"id":"e79341ccf8ea","address":"e7:93:41:cc:f8:ea","rssi":-62,"serviceData":{"model":"H","modelName":"WoHand","mode":false,"state":false,"battery":87}},
{"id":"f4f839cd40fd","address":"f4:f8:39:cd:40:fd","rssi":-42,"serviceData":{"model":"H","modelName":"WoHand","mode":false,"state":false,"battery":97}},
{"id":"e79341ccf8ea","address":"e7:93:41:cc:f8:ea","rssi":-63,"serviceData":{"model":"H","modelName":"WoHand","mode":false,"state":false,"battery":87}},
{"id":"f4f839cd40fd","address":"f4:f8:39:cd:40:fd","rssi":-42,"serviceData":{"model":"H","modelName":"WoHand","mode":false,"state":false,"battery":97}},
{"id":"e79341ccf8ea","address":"e7:93:41:cc:f8:ea","rssi":-74,"serviceData":{"model":"H","modelName":"WoHand","mode":false,"state":false,"battery":87}},
{"id":"f4f839cd40fd","address":"f4:f8:39:cd:40:fd","rssi":-43,"serviceData":{"model":"H","modelName":"WoHand","mode":false,"state":false,"battery":97}},
{"id":"e79341ccf8ea","address":"e7:93:41:cc:f8:ea","rssi":-75,"serviceData":{"model":"H","modelName":"WoHand","mode":false,"state":false,"battery":87}},
{"id":"e79341ccf8ea","address":"e7:93:41:cc:f8:ea","rssi":-75,"serviceData":{"model":"H","modelName":"WoHand","mode":false,"state":false,"battery":87}},
{"id":"f4f839cd40fd","address":"f4:f8:39:cd:40:fd","rssi":-42,"serviceData":{"model":"H","modelName":"WoHand","mode":false,"state":false,"battery":97}},
{"id":"c289d47408fd","address":"c2:89:d4:74:08:fd","rssi":-81,"serviceData":{"model":"T","modelName":"WoSensorTH","temperature":{"c":20.7,"f":69.3},"fahrenheit":false,"humidity":45,"battery":100}},
{"id":"e79341ccf8ea","address":"e7:93:41:cc:f8:ea","rssi":-73,"serviceData":{"model":"H","modelName":"WoHand","mode":false,"state":false,"battery":87}},
{"id":"f4f839cd40fd","address":"f4:f8:39:cd:40:fd","rssi":-42,"serviceData":{"model":"H","modelName":"WoHand","mode":false,"state":false,"battery":97}},
{"id":"e79341ccf8ea","address":"e7:93:41:cc:f8:ea","rssi":-74,"serviceData":{"model":"H","modelName":"WoHand","mode":false,"state":false,"battery":87}},
{"id":"f4f839cd40fd","address":"f4:f8:39:cd:40:fd","rssi":-42,"serviceData":{"model":"H","modelName":"WoHand","mode":false,"state":false,"battery":97}},

Cleaned up URL:

{'id': 'c289d47408fd', 'address': 'c2:89:d4:74:08:fd', 'rssi': -81, 'serviceData': {'model': 'T', 'modelName': 'WoSensorTH', 'temperature': {'c': 20.7, 'f': 69.3}, 'fahrenheit': False, 'humidity': 45, 'battery': 100}},
{'id': 'e79341ccf8ea', 'address': 'e7:93:41:cc:f8:ea', 'rssi': -78, 'serviceData': {'model': 'H', 'modelName': 'WoHand', 'mode': False, 'state': False, 'battery': 87}},
{'id': 'f4f839cd40fd', 'address': 'f4:f8:39:cd:40:fd', 'rssi': -42, 'serviceData': {'model': 'H', 'modelName': 'WoHand', 'mode': False, 'state': False, 'battery': 97}}

sensor:

sensor: 
  - platform: rest
    name: Switchbot Devices
    resource: 'http://192.168.1.30:5003'
    timeout: 15
    scan_interval: 300
    value_template: '{{ value_json | unique(attribute='id') | sort(attribute='id') | join(', ') }}'
    // The line above should remove duplicate ids, sort in order, and add , for proper json format.

  -  platform: template
     sensors:
       hall_temperature:
         value_template: '{{ states.sensor.switchbot_devices[0].serviceData.temperature.c }}'
         device_class: temperature
         unit_of_measurement: '°C'
         friendly_name: 'Hall Temperature'
       hall_humidity:
         value_template: '{{ states.sensor.switchbot_devices[0].serviceData.humidity }}'
         device_class: humidity
         unit_of_measurement: '%'
         friendly_name: 'Hall Humidity'
       hall_battery:
         value_template: '{{ states.sensor.switchbot_devices[0].serviceData.battery }}'
         device_class: battery
         unit_of_measurement: '%'
         friendly_name: 'Hall Battery'

       floor_heater_battery:
         value_template: '{{ states.sensor.switchbot_devices[1].serviceData.battery }}'
         device_class: battery
         unit_of_measurement: '%'
         friendly_name: 'Floor Heater Battery'             

       jcom_tv_box_battery:
         value_template: '{{ states.sensor.switchbot_devices[2].serviceData.battery }}'
         device_class: battery
         unit_of_measurement: '%'
         friendly_name: 'Jcom Tv Box Battery'             

I have one more. Maybe someone can point me to a online service that would help getting the correct value path as I still don’t understand how this works :wink:

I would like to get the “p” value from production on ph-a into a value_template

{
   "data":{
      "production":{
         "ph-a":{
            "p":76.194,
            "q":84.101,
            "s":138.092,
            "v":225.879,
            "i":0.601,
            "pf":0.54,
            "f":50.0
         },
         "ph-b":{
            "p":75.981,
            "q":85.071,
            "s":139.273,
            "v":224.484,
            "i":0.609,
            "pf":0.5,
            "f":50.0
         },
         "ph-c":{
            "p":64.644,
            "q":69.447,
            "s":131.33,
            "v":223.075,
            "i":0.583,
            "pf":0.52,
            "f":50.0
         }
      },
      "net-consumption":{
         "ph-a":{
            "p":214.28,
            "q":-123.658,
            "s":284.623,
            "v":225.777,
            "i":1.245,
            "pf":0.77,
            "f":50.0
         },
         "ph-b":{
            "p":-29.52,
            "q":-67.313,
            "s":127.347,
            "v":224.373,
            "i":0.557,
            "pf":-0.17,
            "f":50.0
         },
         "ph-c":{
            "p":58.725,
            "q":-224.641,
            "s":258.718,
            "v":223.058,
            "i":1.159,
            "pf":0.21,
            "f":50.0
         }
      },
      "total-consumption":{
         "ph-a":{
            "p":290.474,
            "q":-207.759,
            "s":416.965,
            "v":225.828,
            "i":1.846,
            "pf":0.7,
            "f":50.0
         },
         "ph-b":{
            "p":46.462,
            "q":-152.385,
            "s":11.588,
            "v":224.429,
            "i":0.052,
            "pf":1.0,
            "f":50.0
         },
         "ph-c":{
            "p":123.368,
            "q":-294.088,
            "s":388.673,
            "v":223.067,
            "i":1.742,
            "pf":0.32,
            "f":50.0
         }
      }
   }
}

I would have expected this to do it, but no luck:

value_template: "{{ value_json.data.production.ph-a.p }}"

This link may help you out.
https://jsonpathfinder.com/

1 Like

Thanks, I got it to work.

  - platform: rest
    name: Enphase net-verbruik fase 3
    resource: https://xxxxxxxxxxxxxxxxxxx/local/enphase/stream.json
    scan_interval: 30
    verify_ssl: false
    value_template: "{{ (value_json['data']['net-consumption']['ph-c']['p'] | float / 1000) | round(2) }}"
    unit_of_measurement: 'kWh'
    json_attributes_path: $['data']['net-consumption']['ph-c']
    json_attributes:
      - f
      - i
      - q
      - p
      - s
      - pf
      - v
[
  {
    "serialNumber": "122022009125",
    "lastReportDate": 1605626962,
    "devType": 1,
    "lastReportWatts": 0,
    "maxReportWatts": 198
  },
  {
    "serialNumber": "122022007165",
    "lastReportDate": 1605626903,
    "devType": 1,
    "lastReportWatts": 0,
    "maxReportWatts": 215
  },

Thanks, that works. Would it also be possible to loop this somehow so sensors will be added as more “serialNumbers” are added to the json?

Here is one I shamelessly stole from other examples in the forum and midified to make it work fo you:

{% set ns = namespace(serials = '') %}
{%- for i in range(value_json|length) -%}
  {% set delim = ', ' if ns.serials | length > 0 else '' %}
  {% set ns.serials = ns.serials ~ delim ~ value_json[i].serialNumber %}
{% endfor %}
{{ns.serials}}

it will create a list of serial numbers you can use in further templates.

Hi That works :slight_smile:

What I am looking for though is for these serialNumbers to be put in seperate sensors automatically. Maybe I am asking the impossible :wink:

yeah, I think you are.

I know of no way to create sensors automatically based on a single template or in any other way for that matter outside of some kind of discovery mechanism set up thru a specific integration.