Parse multiple JSON values from 1 sensor

Hi, I’m trying to set up 1 rest sensor to return a block of json, which I will then parse with multiple template sensors.

I could set this up with multiple rest sensors, and specify the information I want from the json as part of the value_template for each sensor, but I’m trying to do this with 1 sensor instead of 40+ as I don’t want to make loads of unnecessary API calls.

I’ve played around with the dev template tool, if I copy the json into a test block in the editor, then I can parse it with square brackets, but trying this from the sensor output with states.sensor doesn’t work (the json text looks the same when it’s retrieved from a sensor or pasted in, I’m guessing when it’s retrieved from a sensor that it’s treated as a string rather than as json though.)

Wondering if there’s something obvious which I’ve missed, or if anyone else has a workaround for this?

3 Likes

does value_json work with a sensor?

As I understand it, basically you want to do what BRUH’s multisensor is doing, no?
I do not know I you have ever heard of it, give it a look in his video and on the forums (there is a topic with +200 posts I think)

Thanks, but that doesn’t look relevant to what I was asking. It looks like it’s polling multiple sensors via mqtt.

I was asking about having 1 sensor (either restful or command line with curl) retrieve a block of json, and then multiple templates could parse that json.

1 Like

I’ve just run into the same problem.

I’m using a RESTful sensor to get data from my PV inverter API, but since they do put a 300 API call limit a day, i cannot make a single request for each value.

In noobs terms, I’m trying to get this

- platform: rest
  resource: https://monitoringapi.solaredge.com/site/SITE_ID/currentPowerFlow.json?api_key=API_KEY
  value_template: '{{ value_json.siteCurrentPowerFlow.PV.currentPower | round(2) }}'
  name: 'Generating'
  unit_of_measurement: 'kWh'
  scan_interval: 600

to “generate” more value_template from a single call.

You can find relevant discussion where we’re trying to add SolarEdge support in HASS here

Thanks so much

disclaimer : just diving into HA and python, so I’m still a noob, but as far as I understand, the rest sensor doesn’t submit any attributes with the rest of the json data which could be used to create other template sensor from the same result (feature request needed).

Did you check this : [SOLVED] Parsing a json value from an existing entity in a template sensor where someone made a custom component to retrieve the data?

Since others are interested in polling a sensor once and keeping the JSON metadata as well, I will clean-up the code referenced by @nxd4n and will send a Push Request. Though I need to test JSON objects which are not flat (e.g. { obj: [ 'item1', 'item2' ] }

3 Likes

This would be soooooo cool, definetly following your work, mate!

I’ve made the time and submitted the PR: https://github.com/home-assistant/home-assistant/pull/10483
Hopefully it will get in.

2 Likes

How to do it is listed here:

1 Like

Thanks, I didn’t know it has become official.

The example in the dox is about a multi layered json object. How about:

{
Timestamp: "190902210505S",
Energy_Delivered: "461.837",
Energy_Returned: "0.000",
Gas_Delivered: "34.30",
Energy_Delivered_Tariff1: "234.400",
Energy_Delivered_Tariff2: "227.437",
Energy_Returned_Tariff1: "0.000",
Energy_Returned_Tariff2: "0.000",
Power_Delivered: "2.480",
Power_Returned: "0.000",
Voltage_l1: "233.4",
Current_l1: "10",
Voltage_l2: "0.0",
Current_l2: "0",
Voltage_l3: "0.0",
Current_l3: "0",
Power_Delivered_l1: "2480",
Power_Returned_l1: "0",
Power_Delivered_l2: "0",
Power_Returned_l2: "0",
Power_Delivered_l3: "0",
Power_Returned_l3: "0"
}

How do i get a sensor for each value in this json?

1 Like

Late reply, but that’s not a valid json object. The text needs to be in quotes and the number values should be without. Like this:

{
	"Timestamp": "190902210505S",
	"Energy_Delivered": 461.837,
	"Energy_Returned": 0.000,
	"Gas_Delivered": 34.30,
	"Energy_Delivered_Tariff1": 234.400,
	"Energy_Delivered_Tariff2": 227.437,
	"Energy_Returned_Tariff1": 0.000,
	"Energy_Returned_Tariff2": 0.000,
	"Power_Delivered": 2.480,
	"Power_Returned": 0.000,
	"Voltage_l1": 233.4,
	"Current_l1": 10,
	"Voltage_l2": 0.0,
	"Current_l2": 0,
	"Voltage_l3": 0.0,
	"Current_l3": 0,
	"Power_Delivered_l1": 2480,
	"Power_Returned_l1": 0,
	"Power_Delivered_l2": 0,
	"Power_Returned_l2": 0,
	"Power_Delivered_l3": 0,
	"Power_Returned_l3": 0
}

You can then read the value using the following:
value_template: {{ states.sensor.<insert your sensor name here>.attribute["Power_Delivered"] }}

Hi,
Can you help me with this:
<template state sensor.dijnet_fogaz=32790.0; unpaidInvoices=[{‘provider’: ‘NKM Energia - földgáz’, ‘issuerId’: ‘FőGáz’, ‘invoiceNo’: ‘101903866191’, ‘issuanceDate’: ‘2020.05.25’, ‘invoiceAmount’: 32790.0, ‘deadline’: ‘2020.06.11’, ‘amount’: 32790.0}], unit_of_measurement=Ft, friendly_name=Dijnet (FőGáz), icon=mdi:currency-usd @ 2020-05-31T20:29:09.137437+02:00>
How can I parse for example the deadline?

Thank you

I think this line should do the trick:

value_template: '{{ states.sensor.dijnet_fogaz.state.split(":")[5]}}'