JSON Value Template

Hi All,

Just wondering if anyone can help me with formatting a template for a JSON REST GET sensor. My current config is below but is only returning a unknown value.

  - platform: rest
    resource: http://192.168.0.4/solar_api/v1/GetMeterRealtimeData.cgi?Scope=System
    method: GET
    name: "House Comsumption"
    value_template: '{(value_json.Body.Data.1.PowerApparent_S_Sum /100 | float / 10)|round(2)}'
    unit_of_measurement: 'kW'
    force_update: true

The JSON response

{
  "Body": {
    "Data": {
      "1": {
        "Current_AC_Phase_1": 5.4400000000000004,
        "Current_AC_Phase_2": 0.92000000000000004,
        "Current_AC_Phase_3": 1.6000000000000001,
        "Details": {
          "Manufacturer": "Fronius",
          "Model": "Smart Meter 50kA-3",
          "Serial": "----------"
        },
        "Enable": 1,
        "EnergyReactive_VArAC_Sum_Consumed": 1106,
        "EnergyReactive_VArAC_Sum_Produced": 7632,
        "EnergyReal_WAC_Minus_Absolute": 776,
        "EnergyReal_WAC_Plus_Absolute": 22466,
        "EnergyReal_WAC_Sum_Consumed": 22466,
        "EnergyReal_WAC_Sum_Produced": 776,
        "Frequency_Phase_Average": 49.899999999999999,
        "Meter_Location_Current": 0,
        "PowerApparent_S_Phase_1": 1308.864,
        "PowerApparent_S_Phase_2": 221.72,
        "PowerApparent_S_Phase_3": 386.56,
        "PowerApparent_S_Sum": 1818,
        "PowerFactor_Phase_1": -0.98999999999999999,
        "PowerFactor_Phase_2": -0.89000000000000001,
        "PowerFactor_Phase_3": -0.96999999999999997,
        "PowerFactor_Sum": 1,
        "PowerReactive_Q_Phase_1": 179.80000000000001,
        "PowerReactive_Q_Phase_2": 95.599999999999994,
        "PowerReactive_Q_Phase_3": 90.400000000000006,
        "PowerReactive_Q_Sum": 174.59999999999999,
        "PowerReal_P_Phase_1": 1276,
        "PowerReal_P_Phase_2": 181.80000000000001,
        "PowerReal_P_Phase_3": 353,
        "PowerReal_P_Sum": 1810.8,
        "TimeStamp": 1564741004,
        "Visible": 1,
        "Voltage_AC_PhaseToPhase_12": 417.10000000000002,
        "Voltage_AC_PhaseToPhase_23": 417.89999999999998,
        "Voltage_AC_PhaseToPhase_31": 417.60000000000002,
        "Voltage_AC_Phase_1": 240.59999999999999,
        "Voltage_AC_Phase_2": 241,
        "Voltage_AC_Phase_3": 241.59999999999999
      }
    }
  },
  "Head": {
    "RequestArguments": {
      "DeviceClass": "Meter",
      "Scope": "System"
    },
    "Status": {
      "Code": 0,
      "Reason": "",
      "UserMessage": ""
    },
    "Timestamp": "-------------------------------------"
  }
}

Thanks any help is appreciated.

You will get the value with

value_template: "{{ value_json.Body.Data['1'].PowerApparent_S_Sum }}"

The maths that you do, i don’t understand.
Play with it in Dev Tools/Templates.

EDIT: Uups, sorry, corrected the quotes.

1 Like

Having some issues trying to do a caculation with a json value. In the template editor it works if I change value_json[0].value to the actual value, but then in command line sensor it doesn’t work after reboot. This is my template. It’s converting from bytes to TB, then working out the percentage.

value_template: '{{ ((value_json[0].size /1024 /1024 /1024 /1024) *100 /5.5) |round(0) }}'

hi
need some help figuring out how to extract value from the following response from a http sensor:
http request:

http://192.168.1.30:8688/controller/rest/devices/Zooz%20dimmer%20zen22/status?name=zooz%20dimmer%20a%20switch%20status

Raw response:

<sensors><sensor name="zooz dimmer a switch status">off</sensor></sensors>

I have this setup as a rest sensor in HA

sensor:
  - platform: rest
    resource: http://192.168.1.30:8688/controller/rest/devices/Zooz%20dimmer%20zen22/status?name=zooz%20dimmer%20a%20switch%20status
    name: Dinning Light Status 
    json_attributes: '{{sensor.#text}}'

on the UI it is displaying status as

{"sensors": {"sensor": {"@name": "zooz dimmer a switch status", "#text": "off"}}}

trying to figue out how to extract just the status and display “off” and not the entire raw response

i tried the following but value:template does not seem to like “#”

- platform: template
    sensors:
      dinning_light_status: 
        value_template: '{{value_json.sensors.#text }}'

error message is

Error rendering template: TemplateSyntaxError: unexpected char '#' at 110

any ideas how to extract and display just the on / off value?

Copy and paste your value_json in Dev Tools/templates and you can play with it.

6 Likes

Can you guys help me with another sensor with a similar situation,
Y get the following JSON via REST command and I can’t get the templating to work, the thing is that the JSON Is longer than the one I paste here, and I am currently selecting the value with the number it appears on the list as you can see in the template, the problem is that this JSON sometimes the value I want is the 5th sometimes it is the 7th and other times it is the 4th, so using the number to extract the sensor value via templating is not an option, is there any possibility to do it by “description” or something like that?

If I want for example to get the " description: Fw Build date" I would use this template for this example:

'{{ states.sensor.xxx_data.attributes.records[5]["formattedValue"] | float () }}'

.
which outputs the following value:

'20210830181758.0'

.

But if this FW Build date is then the 8th on the list, then it won’t work :s

Any idea how to template this based on the description name of the value?

Thanks!

JSON:

success: true
records: 
- idSite: 444444
  timestamp: 1636725259
  Device: Gateway
  instance: 0
  idDataAttribute: 641
  description: Bad non-unique identifier
  formatWithUnit: '%s'
  dbusServiceType: null
  dbusPath: null
  code: o
  bitmask: 0
  formattedValue: xxxxxxxx
  id: 1
- idSite: 444444
  timestamp: 1634301837
  Device: Gateway
  instance: 0
  idDataAttribute: 1
  description: gatewayID
  formatWithUnit: '%s'
  dbusServiceType: null
  dbusPath: null
  code: d
  bitmask: 0
  formattedValue: OS
  dataAttributeEnumValues:
    - nameEnum: VGR, VGR2 or VER
      valueEnum: 0
    - nameEnum: OS
      valueEnum: 1
    - nameEnum: OS
      valueEnum: 2
    - nameEnum: LoRaWAN lopy
      valueEnum: 3
    - nameEnum: PHYSEE NodeMCU
      valueEnum: 4
  id: 2
- idSite: 444444
  timestamp: 1634301837
  Device: Gateway
  instance: 0
  idDataAttribute: 386
  description: Productid
  formatWithUnit: '%s'
  dbusServiceType: null
  dbusPath: null
  code: mi
  bitmask: 0
  formattedValue: xxxx
  id: 3
- idSite: 444444
  timestamp: 1634301837
  Device: Gateway
  instance: 0
  idDataAttribute: 387
  description: MachineName
  formatWithUnit: '%s'
  dbusServiceType: null
  dbusPath: null
  code: mn
  bitmask: 0
  formattedValue: MaMa

  id: 4
- idSite: 444444
  timestamp: 1634301837
  Device: Gateway
  instance: 0
  idDataAttribute: 2
  description: Fw Version
  formatWithUnit: '%s'
  dbusServiceType: null
  dbusPath: null
  code: v
  bitmask: 0
  formattedValue: v2.73
  id: 5
- idSite: 444444
  timestamp: 1634301837
  Device: Gateway
  instance: 0
  idDataAttribute: 3
  description: Fw Build date
  formatWithUnit: '%s'
  dbusServiceType: null
  dbusPath: null
  code: build
  bitmask: 0
  formattedValue: '20210830181758'
  id: 6
- idSite: 444444
  timestamp: 1634301837
  Device: Gateway
  instance: 0
  idDataAttribute: 556
  description: Machine serial number
  formatWithUnit: '%s'
  dbusServiceType: null
  dbusPath: null
  code: ms
  bitmask: 0
  formattedValue: HQXXXXX
  id: 7
- idSite: 444444
  timestamp: 1634301837
  Device: Gateway
  instance: 0
  idDataAttribute: 453
  description: Capabilities
  formatWithUnit: '%s'
  dbusServiceType: null
  dbusPath: null
  code: cp
  bitmask: 1
  formattedValue: many
  id: 8
- idSite: 444444
  timestamp: 1634301837
  Device: Gateway
  instance: 0
  idDataAttribute: 237
  description: Boot type
  formatWithUnit: '%s'
  dbusServiceType: null
  dbusPath: null
  code: w
  bitmask: 0
  formattedValue: Old kernel, or hw without boottype support, etc.
  dataAttributeEnumValues:
    - nameEnum: Tmp file already read, vrmlogger restarted!?
      valueEnum: '-3'
    - nameEnum: Reading tmp file failed
      valueEnum: '-2'
    - nameEnum: Reading watchdog register failed
      valueEnum: '-1'
    - nameEnum: Old kernel, or hw without boottype support, etc.
      valueEnum: 0
    - nameEnum: Cold boot or reboot
      valueEnum: 1
    - nameEnum: Unreproducable reset on CCGX
      valueEnum: 2
    - nameEnum: Reset button
      valueEnum: 3
    - nameEnum: Cold boot
      valueEnum: 4
    - nameEnum: Reboot command
      valueEnum: 5
    - nameEnum: Watchdog reboot
      valueEnum: 17
    - nameEnum: Max load avg exceeded (systems <= v2.06)
      valueEnum: 444444
    - nameEnum: Watchdog-ENOMEM
      valueEnum: 444444
    - nameEnum: Watchdog-EMAXLOAD (load average too high)
      valueEnum: 444444
  id: 9
- idSite: 444444
  timestamp: 1636292722
  Device: Gateway
  instance: 0
  idDataAttribute: 154
  description: Remote support
  formatWithUnit: '%s'
  dbusServiceType: settings
  dbusPath: /Settings/System/RemoteSupport
  code: rss
  bitmask: 0
  formattedValue: Disabled
  dataAttributeEnumValues:
    - nameEnum: Disabled
      valueEnum: 0
    - nameEnum: Enabled
      valueEnum: 1
  id: 10
- idSite: 444444
  timestamp: 1636689746
  Device: Gateway
  instance: 0
  idDataAttribute: 543
  description: Remote support IP and port
  formatWithUnit: '%s'
  dbusServiceType: settings
  dbusPath: /Settings/System/RemoteSupportIpAndPort
  code: rsIpPort
  bitmask: 0
  formattedValue: xx.yyy.zzz.xx;xxxxx
  id: 11




Hey, I am using your wonderful guide to access an API.

It’s an API that reacts when there is an alarm for my local fire brigade.

When there is no alarm the Api shows just basic information. But as soon as there is an alarm there are more things on the json. Including an attribute that I need to read. But as long as its not there my log shows a permanent stream of errors that it can’t access the attribute on the json. I tried to change the sensor to the way of “{{ state_attr(value_json.alarms[0].creatorName) }}”. But that does not help against the stream of errors.

Does anyone have an idea how to change the sensor or something to get the errors stopped?

thanks

Hi there,
I have an extra wish for this problem. I got it to work with this example. But I do have a second smartmeter in the system. That is complicating it.
Maybe someone can help, and maybe others can learn from this. (as I did)
So the two smartmetes are distinguished by an index {0/1}. I can run the thing twice, once for each smartmeter. But I would like to extract all data I need from this one call.

So how do I extract the identically named values from both smarmeters at once?

I guess read the whole thing into the entitiy and disect it using templates. But how?

Here is what I have:

  - platform: rest
    name: "fronius_smartm_rest"
    unique_id: "fronius_smartm_rest"
    scan_interval: 60
    value_template: "OK"
    json_attributes_path: "$.Body.Data['0']"
    json_attributes:
      - EnergyReal_WAC_Sum_Consumed
      - EnergyReal_WAC_Sum_Produced
      - PowerReal_P_Sum
    resource: http://192.168.210.81/solar_api/v1/GetMeterRealtimeData.cgi
This is the response from fronius:

JSON
`
{
   "Body" : {
      "Data" : {
         "0" : {
            "Current_AC_Phase_1" : 2.4540000000000002,
            "Current_AC_Phase_2" : 5.2060000000000004,
            "Current_AC_Phase_3" : 3.5419999999999998,
            "Details" : {
               "Manufacturer" : "Fronius",
               "Model" : "Smart Meter 63A",
               "Serial" : "22040266"
            },
            "Enable" : 1,
            "EnergyReactive_VArAC_Sum_Consumed" : 125180.0,
            "EnergyReactive_VArAC_Sum_Produced" : 1935820.0,
            "EnergyReal_WAC_Minus_Absolute" : 1612372.0,
            "EnergyReal_WAC_Plus_Absolute" : 11198.0,
            "EnergyReal_WAC_Sum_Consumed" : 11198.0,
            "EnergyReal_WAC_Sum_Produced" : 1612372.0,
            "Frequency_Phase_Average" : 50.0,
            "Meter_Location_Current" : 0.0,
            "PowerApparent_S_Phase_1" : 558.7758,
            "PowerApparent_S_Phase_2" : 1153.1290000000001,
            "PowerApparent_S_Phase_3" : 804.03399999999999,
            "PowerApparent_S_Sum" : 312.0,
            "PowerFactor_Phase_1" : -0.92000000000000004,
            "PowerFactor_Phase_2" : 0.98999999999999999,
            "PowerFactor_Phase_3" : -0.98999999999999999,
            "PowerFactor_Sum" : -0.089999999999999997,
            "PowerReactive_Q_Phase_1" : -163.37,
            "PowerReactive_Q_Phase_2" : -51.109999999999999,
            "PowerReactive_Q_Phase_3" : -97.269999999999996,
            "PowerReactive_Q_Sum" : -311.75,
            "PowerReal_P_Phase_1" : -411.02999999999997,
            "PowerReal_P_Phase_2" : 1116.9200000000001,
            "PowerReal_P_Phase_3" : -736.38999999999999,
            "PowerReal_P_Sum" : -30.5,
            "TimeStamp" : 1662807526,
            "Visible" : 1,
            "Voltage_AC_PhaseToPhase_12" : 389.0,
            "Voltage_AC_PhaseToPhase_23" : 388.39999999999998,
            "Voltage_AC_PhaseToPhase_31" : 393.80000000000001,
            "Voltage_AC_Phase_1" : 227.69999999999999,
            "Voltage_AC_Phase_2" : 221.5,
            "Voltage_AC_Phase_3" : 227.0
         },
         "1" : {
            "Current_AC_Phase_1" : -2.339,
            "Current_AC_Phase_2" : -2.3559999999999999,
            "Current_AC_Phase_3" : -2.3319999999999999,
            "Current_AC_Sum" : -7.0270000000000001,
            "Details" : {
               "Manufacturer" : "Fronius",
               "Model" : "Smart Meter TS 65A-3",
               "Serial" : "2265186801"
            },
            "Enable" : 1,
            "EnergyReactive_VArAC_Sum_Consumed" : 13560.0,
            "EnergyReactive_VArAC_Sum_Produced" : 48511.0,
            "EnergyReal_WAC_Minus_Absolute" : 5869.0,
            "EnergyReal_WAC_Plus_Absolute" : 473490.0,
            "EnergyReal_WAC_Sum_Consumed" : 5869.0,
            "EnergyReal_WAC_Sum_Produced" : 473490.0,
            "Frequency_Phase_Average" : 49.899999999999999,
            "Meter_Location_Current" : 3.0,
            "PowerApparent_S_Phase_1" : 520.10000000000002,
            "PowerApparent_S_Phase_2" : 510.60000000000002,
            "PowerApparent_S_Phase_3" : 518.0,
            "PowerApparent_S_Sum" : 1548.8,
            "PowerFactor_Phase_1" : 1.0,
            "PowerFactor_Phase_2" : 1.0,
            "PowerFactor_Phase_3" : 1.0,
            "PowerFactor_Sum" : 1.0,
            "PowerReactive_Q_Phase_1" : 12.4,
            "PowerReactive_Q_Phase_2" : 11.6,
            "PowerReactive_Q_Phase_3" : 0.0,
            "PowerReactive_Q_Sum" : 24.100000000000001,
            "PowerReal_P_Phase_1" : 520.0,
            "PowerReal_P_Phase_2" : 510.39999999999998,
            "PowerReal_P_Phase_3" : 518.0,
            "PowerReal_P_Sum" : 1548.5,
            "TimeStamp" : 1662807527,
            "Visible" : 1,
            "Voltage_AC_PhaseToPhase_12" : 390.30000000000001,
            "Voltage_AC_PhaseToPhase_23" : 390.0,
            "Voltage_AC_PhaseToPhase_31" : 390.10000000000002,
            "Voltage_AC_Phase_1" : 227.80000000000001,
            "Voltage_AC_Phase_2" : 220.69999999999999,
            "Voltage_AC_Phase_3" : 227.40000000000001
         }
      }
   },
   "Head" : {
      "RequestArguments" : {
         "Scope" : "System"
      },
      "Status" : {
         "Code" : 0,
         "Reason" : "",
         "UserMessage" : ""
      },
      "Timestamp" : "2022-09-10T10:58:47+00:00"
   }
}

Hi :wave:!
Doesn’t the Fronius integration take care of that? I think it should support multiple meters. The used endpoint and update interval is the same.

Good morning,

yes it does. But I need to reduce polling load to the fronius. Sometimes the response takes seconds, in addition to that the fronius integration does not give individual data for the different MPPT’s. Also I get no energy data on battery and MPPTs from that Integration (the workaround using integration is not very precise, I find). I would like to know how much energy is produced and consumed, that data is there using the sunspec data. I want to steer home energy consumption!
That is why I use https://github.com/CJNE/ha-sunspec/, which works fine for that. Now I need to get the smartmeter stuff separately, once in a while…
By the way, I want to learn how to handle these things, maybe it is useful for somebody else.
Maybe you want to invest few minutes to educate me and others? I would be grateful.

You can disable the other endpoints of the integration if you like. MPPT data is not provided by SolarAPI - that is why it is not supported. Other than that imho it does exactly what you are trying to achieve - with some additional exception handling.
If you want to get most datapoints in tight update intervals from a Fronius inverter, I’d recommend using Modbus. The Http endpoints are quite flaky (I wrote the integration so have some experience with these since).

You are right, it reads all smartmeters fine.
I will reinstall the fronius integration then.
Will deactivating endpoints reduce load or is it just cosmetics in the table?
For power data I will use modbus on top.
Thanks you for great your work!

Iirc it should reduce load too by some extent (it should still poll once in a while to detect eg. unpowered inverters). Just give it a try.
If it still polls in high frequency, maybe open an issue at GitHub.
You can also disable polling for the whole integration (integration system options) and set up an automation to call homeassistant.update_entity Service periodically (once every minute or so) on one Meter entity (the others will be updated too because it is the same query).

That said you’ll probably get the most reliability benefits from updating the inverters and dataloggers firmware (solar.web) and not using the inverters integrated Wi-Fi.

Thanks, the inverter is not using WiFi, but Lan. The problem arises because of a native KNX device is polling it in parallel… I’ll manage somehow. :grinning:

Not sure but you just have one attribute body and add the extra layer in your templates. Or use command_line and “jq” to restructure how you wish.