Syntax error creating Rest sensor from API call

Hi Guys,
I have knowingly dived in way too deep and made more progress that I expected, but have run into a syntax issue I lack the skill to resolve.

Have been granted API access to my home Solar / Battery system by RedBack (no existing Integration I’m aware of) and am trying to build a sensor to pull the requisite data.

Have managed to get the Developer Tools Template Editor to give me the thumbs up for a couple of the values I want to grab, but with the next couple it is refusing to play ball…

{## Imitate available variables: ##}
{% set value_json = {
  "Data": {
    "TimestampUtc": "2022-10-06T12:16:22Z",
    "SiteId": "xxxxxxxxx",
    "Phases": [
      {
        "Id": "A",
        "ActiveExportedPowerInstantaneouskW": 0.007,
        "ActiveImportedPowerInstantaneouskW": 0,
        "VoltageInstantaneousV": 243.5,
        "CurrentInstantaneousA": 2.95,
        "PowerFactorInstantaneousMinus1to1": 0.013
      }
    ],
    "FrequencyInstantaneousHz": 49.99,
    "BatterySoCInstantaneous0to1": 0.7,
    "PvPowerInstantaneouskW": 0,
    "InverterTemperatureC": 23.8,
    "BatteryPowerNegativeIsChargingkW": 0.597,
    "PvAllTimeEnergykWh": 22025.4,
    "ExportAllTimeEnergykWh": 8404.335,
    "ImportAllTimeEnergykWh": 2828.861,
    "LoadAllTimeEnergykWh": 15282.7,
    "Status": "OK",
    "Inverters": [
      {
        "SerialNumber": "RBxxxxxxxxxxxxx",
        "PowerMode": {
          "InverterMode": "Auto",
          "PowerW": 0
        }
      }
    ]
  },
  "Metadata": null
} %}


{{ value_json ["Data"] ["Phases"] ["Id"].ActiveExportedPowerInstantaneouskW }}

The attempted value_temple here just generates the error…

UndefinedError: ‘list object’ has no attribute ‘Id’

I was able to get it working to retrieve the battery state of charge, and so foolishly though I had understood the format - but apparently not.

Where have I gone off the rails?

Not made a lot of progress here, but have mashed together lots of parts from answers to similar questions and have something that I think aught to be on track to make a sensor that will at least read the API so then I can fight with the format to get out of it what I need…

My sensor.yaml is still throwing an error that prevents a reboot so I can’t even test it properly yet, and the error message is so helpful (not) one could be forgiven for thinking it had been written by Microsoft.

My sensor.yaml is as follows…

rest:
  - authentication: OAuth2
    authorization: Bearer xxxxxxxxxxxxxxxxxxxxxxxx
    scan_interval: 600
    resource: https://api.redbacktech.com/Api/v2/EnergyData/xxxxxxxxx/Dynamic?metadata=false
    sensor:
    -  name: "Redback Battery"
       json_attributes_path: "$.Data"
       value_template:"OK"
       json_attributes:
         -  "BatterySoCInstantaneous0to1"
         -  "BatteryPowerNegativeIsChargingkW"
    -  name: "Redback Grid"
       json_attributes-path: "$.Data.Phases"
       value_template:"OK"
       json_attributes:
         -  "ActiveExportedPowerInstantaneouskW"
         -  "ActiveImportedPowerInstantaneouskW"

The error this is generating tells me…

Error loading /config/configuration.yaml: while scanning a simple key
in “/config/sensor.yaml”, line 9, column 8
could not find expected ‘:’
in “/config/sensor.yaml”, line 10, column 8

Any clues - please?

Hi
Based on your post, I contacted Redback to access data via their trial API - still waiting for their response.

With respect to the above, the error might be a missing space:

value_template:"OK"   to read   value_template: "OK"

I’m assuming the xxx’s in authorization: and resource: are supplied by Redback?

Though my home automations achieve a bit more than simple, I’m fairly good at copying others who have gone before and firmly believe in a series of simple steps work better for me than sophistication - so I do not expect to help here any further unfortunately.

Hope this helps

Indirectly - Yes. They provide a Client ID and an API Secret - using these, you can log into their API portal to generate the Bearer code and retrieve the System number. The System number is static, but as others have noted, the Bearer code is only valid for a hour or so after which a new code needs to be generated.

That is the next part of the puzzle after getting the filtering of the output working - refreshing the Bearer code. I’ve only accomplished that manually so far, but logically there must be a way. The whole API would be pointless if it required manual intervention 24 times a day.

To the best of my recollection I had that space in the value_template to start with, but took it out after it wasn’t particularly clear on the youtube video I was grabbing that part of the code from if it was there or not. Will experiment with it further one day soon when I don’t have so much overtime for work…

Thanks for the suggestion.

Hi
I have access to the APIs for RedBack, I have 3 phase hybrid.
Currently, I use an azure function to process a HA Restful API call to retrieve the current values. This works for me, just haven’t got around yet to shifting the Azure function Python code into a custom
Integration yet.
I also control the charge/discharge of the battery via HA, but I do this by the portal not the API as there is some differences in what you can do with setting of the inverter between the two.
One note to be aware of, you appear to have a single phase inverter, so the Phases section of your JSON only has one set of values, 3 phase inverters will have 3 sets of values which you need to sum to get the actual state of import/export.

If people are interested, happy to share further and setup a GitHub repo to take this integration further.

1 Like

Hi Chris
Just a clarification.

When you say you have access to the APIs, are you referring to the 1 hour limited access or ongoing access?

Thanks

Hi all, I have now completed the Redback Technologies integration and it is working well for my own system here (3 phase with battery). If you have HACS you can add it easily from the HACS interface as it is part of their default repo now. You can also manually install from here GitHub - juicejuice/homeassistant_redback: Home Assistant integration for inverter and battery systems from Redback Technologies
@cabberley @pcal @marcau

:smiley: I had given up in frustration - and then found your integration on Github (hadn’t logged in here for a while!).

It works beautifully - does everything I was trying to accomplish, and more besides. A thousand thank yous!!

@pcal that is great to hear. Feels good that others can make use of my work! If you get chance, would you mind PM’ing me a sample of your static and dynamic energy data? (You can retrieve these as JSON data packets from the API here Swagger UI)
The reason is that it seems you have a single phase inverter and I’d like to check and make sure my code is handling all that correctly. I’ve only tested the code against my own three phase inverter.
BTW, if you do send the static data please be sure to sanitise it first as it does contain some personally identifying information that neither you nor I would want shared :slight_smile:

Hi cabberly - you have intrigued me here - I don’t currently have any need to automate the charge / discharge settings, but there are some other features that only seem accessible via the portal (like control of the relay outputs for example) which I had not even attempted as it seemed too hard. I tried web scraping the portal for a long time before I got the API, and had achieved absolutely nothing with that either. Given that my Redback Control and and my HWS Heat Pump are sitting right next to each other and wifi coverage isn’t fantastic down there (Redback is hardwired to the LAN) - if I could use a Redback output relay to trigger a remote boost on the HWS, that would save messing about with another Shelly module and a wifi repeater…

How have you achieved HA control over the portal?

I would be interested to hear from @cabberley too. The public API does not allow control over relays etc. It does allow import/export/charge/discharge limits via OperatingEnvelope API and inverter mode setting via Schedule API. (Supported inverter modes appear to be: ChargeBattery, DischargeBattery, Auto, ImportPower, ExportPower, Conserve, Offgrid, Hibernate, BuyPower, SellPower, ForceChargeBattery, ForceDischargeBattery)

Hi I have downloaded and have working well the integration plugin. I would love to be able to change the inverter modes via double click by double click functionality on my entity card. Juicejuice could you point me in the direction of where I could learn to do this?

Hi there, glad to hear it is all working well! @cabberley actually developed some sort of script to control the inverter modes, check out the code here: GitHub - cabberley/Home-Assistant_Redback_Invertor_Mode_Set

This was never brought into the integration but I guess it could be, I like your idea!

1 Like

What do I need to do to get this added to the integration? Do i post in the feature requests? Do I write to @cabberley ? Not sure how I can contribute other than pushing the idea but happy to help if I can. Ps. I am not a programmer haha

It’s a cool idea but I guess it would need to find someone who can implement or adapt the necessary code to suit. I’m kind of interested but TBH the existing Redback scheduler is fine for my needs and I’m pretty busy at the moment.

1 Like