Creating Sensors and filtering specific JSON Value from Curb Energy API

You can simplify it like this:

rest:
  - resource: https://app.energycurb.com/api/latest/{{location_id}}
    scan_interval: 10
    method: GET
    headers:
      Authorization : !secret curb_secret_token
      Content-Type: application/json
    sensor:
      - name: "Curb Fridge Freezer"
        value_template: "{{ value_json.circuits[0].w }}"
        unique_id: curb-0
      - name: "Curb Office Bath and Old Kitchen"
        value_template: "{{ value_json.circuits[1].w }}"
        unique_id: curb-1
      - name: etc...

Thanks again, I just did the changes and it’s indeed less lines on it.

  - resource: https://app.energycurb.com/api/latest/32d29fe8-be29-4184-a159-7fde34a4ff34
    scan_interval: 10
    method: GET
    headers:
      Authorization : !secret curb_secret_token
      Content-Type: application/json
    sensor:
      - name: "Curb Fridge Freezer"
        device_class: power
        unit_of_measurement: w
        value_template: "{{ value_json.circuits[0].w }}"
        unique_id: curb-0

I added the device_class and the unit_of_measurment so I can create another sensor to be able to display it on the Energy Dashboard:

I’ve converted them from a power (W) sensor to a energy (KwH) sensor using the Reimann Integartion

  - platform: integration
    source: sensor.curb_fridge_freezer
    name: energy_spent_curb_fridge_freezer
    unit_prefix: k
    round: 2

Unfortunately the newly created sensor does not show on the energy dashboard. But it does show up as a sensor on HA:

As you can guess the follow up question is: How can I make the new energy sensors show up in my energy dashboard?

Change the unit of the rest sensors to a capital “W”. To be used in the energy dashboard the Riemann sensor must have a unit of Wh or kWh, the capitalisation is important.

Ok i changed all rest sensors to a capital “W”

    sensor:
      - name: "Curb Fridge Freezer"
        device_class: power
        unit_of_measurement: "W"
        value_template: "{{ value_json.circuits[0].w }}"
        unique_id: curb-0

And the Riemann Integration works because the new energy sensor shows up as a kwh sensor:

But I am not able to add that Riemann sensor to the Energy Dashboard under the Individual Devices section, because it does not show up.

*** UPDATE FOR AUTO-RENEW ACCESS TOKEN ****

As you know the token for the API lasts only 24 hours, and then you have to make another curl POST to get the new access_token…

Well I figure it out how to auto-refresh that token automatically so the sensors would get the updated token every time.

Here’s what I did:

First I created a rest sensor that would call the API and retrieve the access_token in json format:

### CURB ACCESS_TOKEN REFRESH ###
  - platform: rest
    name: curb_token_request
    resource: https://energycurb.auth0.com/oauth/token
    method: POST
    headers:
      Content-Type: application/json
      Cache-Control: no-cache
    payload: '{"grant_type": "password", "audience": "app.energycurb.com/api", "username": "{{USERNAME}}", "password": "{{PASSWORD}}", "client_id": "iKAoRkr3qyFSnJSr3bodZRZZ6Hm3GqC3", "client_secret": "dSoqbfwujF72a1DhwmjnqP4VAiBTqFt3WLLUtnpGDmCf6_CMQms3WEy8DxCQR3KY"}'  
    value_template: 'OK'
    scan_interval: 43200
    json_attributes:
      - access_token

Replace {{USERNAME}} and {{PASSWORD}} with your own credentials.

You can’t have the access_token on the value_template section because it exceeds 255 characters limited by HA. But we CAN use the sensor’s attribute to call it from the rest integration. Also I set a scan_interval of 43200 seconds (12 hours) even tough the access_token last for 24 hours, just to play it safe.

To do this I modified the rest integration (not the rest sensor platform) to use the attribute on the sensor.curb_token_request on the Authorization parameter.

rest:
  - resource: https://app.energycurb.com/api/latest/32d29fe8-be29-4184-a159-7fde34a4ff34
    scan_interval: 10
    method: GET
    headers:
      Authorization: >
        Bearer {{ state_attr('sensor.curb_token_request', 'access_token') }}
      Content-Type: application/json

Now the integration does not need us to refresh the access_token every 24 hours. It will auto renew.

Go to Developer Tools → Statistics. Are there errors there to fix?

You are correct!

There were errors on the statistics.

After I fixed the issue the sensor showed up on the energy dashboard. Thanks

1 Like

First of all let me say thank you. I have been trying to find a way to integrate Curb into HA for over a year.

I got everything working up until I tried to automate the token refresh. When I view the sensor.curb_token_request it shows an access_token but the sensors no longer are giving data. I tested the token it gave me using it in a CURL request and it allows me to pull data but for some reason it will not work within HA.
I am using this in my sensors.yaml

### CURB ACCESS_TOKEN REFRESH ###
  - platform: rest
    name: curb_token_request
    resource: https://energycurb.auth0.com/oauth/token
    method: POST
    headers:
      Content-Type: application/json
      Cache-Control: no-cache
    payload: '{"grant_type": "password", "audience": "app.energycurb.com/api", "username": "myemail.com", "password": "mypassword", "client_id": "iKAoRkr3qyFSnJSr3bodZRZZ6Hm3GqC3", "client_secret": "dSoqbfwujF72a1DhwmjnqP4VAiBTqFt3WLLUtnpGDmCf6_CMQms3WEy8DxCQR3KY"}'  
    value_template: 'OK'
    scan_interval: 43200
    json_attributes:
      - access_token  

Followed by this in my configuration.yaml

rest:
  - resource: https://app.energycurb.com/api/latest/045ae1b3-e47b-479d-ad65-6904e02c9c37
    scan_interval: 10
    method: GET
    headers:
      Authorization: >
        Bearer "{{ state_attr('sensor.curb_token_request', 'access_token') }}"
      Content-Type: application/json
    sensor:
      - name: "Living Room outlets"
        value_template: "{{ value_json.circuits[0].w }}"
        unique_id: curb-0

If I remove the automated token update and just use my secrets.yaml with the token it works with no issues.

After reviewing the code it looks like removing the “” from this part of the code fixed it.
Your code:

 Authorization: >
        Bearer "{{ state_attr('sensor.curb_token_request', 'access_token') }}"

Working code for me:

Authorization: >
        Bearer {{ state_attr('sensor.curb_token_request', 'access_token') }}

You are right I just checked my working code and it does not have the “ on it. I didn’t copy the updated code when doing my post. Glad you got it working.

If you find a way to get webhooks connection to curb’s working on home assistant to get live data and not 1 min refresh data, let me know, but for now this will work.

1 Like

My coding knowledge is based off chatGPT and copying other peoples code :sweat_smile:

I made a new thread with an issue I can’t figure out. One of my sensors from curb I am using to see how much power I am returning to grid (NET) which outputs a negative value. Using the sensor for the energy dashboard gives improper data because of the negative value when I am returning power to the grid. I cannot figure out how to properly utilize this sensor so I can have data on energy returned to the grid.

Heres my thread Energy Dashboard Sensor Help

Let me buy you a beer for at least figuring out how to get the sensors into HA.

I just replied to your new post regarding negative values. I’m always up for a beer! :grinning:

1 Like

Bumping this thread again because I updated to the newest HA and it seems to have broke everything. What version are you running?

The system cannot restart because the configuration is not valid: Invalid config for [rest]: expected SensorStateClass or one of ‘measurement’, ‘total’, ‘total_increasing’ for dictionary value @ data[‘rest’][0][‘sensor’][12][‘state_class’]. Got None. (See /config/configuration.yaml, line 201).

Line 201

rest:
  - resource: https://app.energycurb.com/api/latest/045ae1b3-e47b-479d-ad65-6904e02c9c37
    scan_interval: 10
    method: GET
    headers:
      Authorization: >
        Bearer {{ state_attr('sensor.curb_token_request', 'access_token') }}
      Content-Type: application/json

I haven’t updated to the latest version 2023.8. I’m using 2023.7

But it seems that the problem is within the sensor created by the rest platform.

Can you show us the full yaml config of the rest sensors? Specifically the part that show the class for the sensors created

Like this:


    sensor:
      - name: "Curb Water Pump & Deck"
        device_class: power
        unit_of_measurement: W
        value_template: "{{ value_json.circuits[6].w }}"
        unique_id: curb-0
      - name: "Curb Office Bath and Old Kitchen"
        device_class: power
        unit_of_measurement: "W"
        value_template: "{{ value_json.circuits[1].w }}"
        unique_id: curb-1
      - name: "Curb AC Master"
        device_class: power
        unit_of_measurement: "W"
        value_template: "{{ value_json.circuits[2].w }}"
        unique_id: curb-2
      - name: "Curb AC Living Room"
        device_class: power
        unit_of_measurement: "W"
        value_template: "{{ value_json.circuits[3].w }}"
        unique_id: curb-3
      - name: "Curb Washer and Dryer"
        device_class: power
        unit_of_measurement: "W"
        value_template: "{{ value_json.circuits[4].w }}"
        unique_id: curb-4
      - name: "Curb Mini Bar Wine Cooler"
        device_class: power
        unit_of_measurement: "W"
        value_template: "{{ value_json.circuits[5].w }}"
        unique_id: curb-5
      - name: "Curb Fridge and Freezer"
        device_class: power
        unit_of_measurement: "W"
        value_template: "{{ value_json.circuits[0].w }}"
        unique_id: curb-6
      - name: "Curb AC Baby Room"
        device_class: power
        unit_of_measurement: "W"
        value_template: "{{ value_json.circuits[7].w }}"     
        unique_id: curb-7
      - name: "Curb Baby Room and Bathroom"
        device_class: power
        unit_of_measurement: "W"
        value_template: "{{ value_json.circuits[8].w }}"
        unique_id: curb-8
      - name: "Curb Dishwasher Disposal"
        device_class: power
        unit_of_measurement: "W"
        value_template: "{{ value_json.circuits[9].w }}"
        unique_id: curb-9
      - name: "Curb Solar Panels 1"
        device_class: power
        unit_of_measurement: "W"
        value_template: "{{ value_json.circuits[10].w }}"
        unique_id: curb-10
      - name: "Curb Living Dining and Office Equip"
        device_class: power
        unit_of_measurement: "W"
        value_template: "{{ value_json.circuits[11].w }}"
        unique_id: curb-11
      - name: "Curb AC Office"
        device_class: power
        unit_of_measurement: "W"
        value_template: "{{ value_json.circuits[12].w }}"
        unique_id: curb-12
      - name: "Curb Master Bathroom"
        device_class: power
        unit_of_measurement: "W"
        value_template: "{{ value_json.circuits[13].w }}"
        unique_id: curb-13
      - name: "Curb Garage Door"
        device_class: power
        unit_of_measurement: "W"
        value_template: "{{ value_json.circuits[14].w }}"
        unique_id: curb-14
      - name: "Curb Solar Panels 2"
        device_class: power
        unit_of_measurement: "W"
        value_template: "{{ value_json.circuits[15].w }}"
        unique_id: curb-15

I ended up updating to the same version as you. The newest version broke too many other things I had setup and I don’t have the time to track down fixes for all of them.

Thanks

Well I updated to 2023.8 and I don’t have any problems with the sensors or the rest platform. If you have the time share your full rest config for curb so we can see what might be causing it.

Hello, I know this is much later, but I am going through this thread trying to recreate this for my Curb setup. I am able to pull my access token and get all my values via curl commands as you have documented above, so I am hoping that I am just missing something.

I don’t see any evidence of it failing in the logs, but I don’t see my sample sensor show up for sensor 0.

Here’s what I have:

configuration.yaml:

rest:
  - resource: https://app.energycurb.com/api/latest/11-22-33-my-id
    scan_interval: 10
    method: GET
    headers:
      Authorization: >
        Bearer {{ state_attr('sensor.curb_token_request', 'access_token') }}
      Content-Type: application/json
    sensor:
      - name: "Zebra"
        value_template: "{{ value_json.circuits[0].w }}"
        unique_id: curb-0

secrets.yaml:

curb_secret_token: Bearer e123myaccesstoken #(Replace with your own access token taken before)

sensors.yaml:

### CURB ACCESS_TOKEN REFRESH ###
  - platform: rest
    name: curb_token_request
    resource: https://energycurb.auth0.com/oauth/token
    method: POST
    headers:
      Content-Type: application/json
      Cache-Control: no-cache
    payload: '{"grant_type": "password", "audience": "app.energycurb.com/api", "username": "[email protected]", "password": "password123", "client_id": "iKAoRkr3qyFSnJSr3bodZRZZ6Hm3GqC3", "client_secret": "dSoqbfwujF72a1DhwmjnqP4VAiBTqFt3WLLUtnpGDmCf6_CMQms3WEy8DxCQR3KY"}'  
    value_template: 'OK'
    scan_interval: 43200
    json_attributes:
      - access_token
rest:
  - resource: https://app.energycurb.com/api/latest/11-22-33-my-id
    scan_interval: 10
    method: GET
    headers:
      Authorization: >
        Bearer {{ state_attr('sensor.curb_token_request', 'access_token') }}
      Content-Type: application/json
    sensor:
      - name: "Zebra"
        value_template: "{{ value_json.circuits[0].w }}"
        unique_id: curb-0

Instead of https://app.energycurb.com/api/latest/11-22-33-my-id use this address instead

https://app.energycurb.com/api/v3/latest/11-22-33-my-id

I think adding the v3 will fix it.

Thank you! This thread has been awesome and has helped me get Curb up and running when the search results normally seem to imply it can’t be done. This has been a huge help, if there’s a way to buy you a coffee let me know. Also for anyone else finding this, I had a typo in my configuration.yaml, here’s a working example:

rest:
  - resource: https://app.energycurb.com/api/v3/latest/my-crub-id-123456
    scan_interval: 10
    method: GET
    headers:
      Authorization: >
        Bearer {{ state_attr('sensor.curb_token_request', 'access_token') }}
      Content-Type: application/json
    sensor:
      - name: "curb_power_workbench"
        device_class: power
        unit_of_measurement: "W"
        value_template: "{{ value_json.circuits[0].w }}"
        unique_id: curb-0

I’m glad to help and that you solved your problem, currently I’m still trying to get live data from curb and not 1 min refresh data into Home Assistant, if I make it happen I’ll update this same thread.

If you wish to buy me a coffee you can do so here.

Thanks