Restful integration help

Hi there, I’m really struggling to get my head around this stuff as I’m a total noob, so would love some advice, please… I’m essentially trying to pull a few bits from the Pocketsmith API to display on a dashboard.

Here’s what I have in my config:

rest:
  - resource: >
      {% set last_day =  
      31 if now().month in (1,3,5,7,8,10,12) else 
        30 if now().month in (4,6,9,11) else 
          29 if now().month == 2 and now().year % 4 == 0 and (now().year % 100 != 0 or now().year % 400 == 0) else 28
      %}
      {% set first_day = '01' %}
      {% set current_year_month = now().strftime('%Y-%m') %}
      https://api.pocketsmith.com/v2/users/MY_USER_ID/budget_summary?period=months&interval=1&start_date={{current_year_month}}-01&end_date={{current_year_month}}-{{last_day}}
    method: GET
    headers:
      Accept: application/json
      X-Developer-Key: MY_DEVELOPER_KEY
    name: Pocketsmith Budget Summary 
    scan_interval: 86400
    sensor:
      - name: "Under by"
        value_template: "{{ value_json.x.expense.periods[0].under_by }}"
    sensor:
      - name: "Over budget?"
        value_template: "{{ value_json.x.expense.periods[0].over_budget }}"
    sensor:
      - name: "Under budget?"
        value_template: "{{ value_json.x.expense.periods[0].under_budget }}"

This doesn’t throw an error but it also doesn’t show any sensor data at all… There’s a lot going on here so I’m not sure what’s failing. There may even be a better way to do it… but I’m stumped.

This is the endpoint https://api.pocketsmith.com/v2/users/USER_ID/budget_summary?period=months&interval=1&start_date=2022-02-01&end_date=2022-02-31

And here is the json it returns:

{
  "income": {
    "start_date": "2022-08-01",
    "end_date": "2022-08-31",
    "currency_code": "nzd",
    "total_actual_amount": 9560.56,
    "average_actual_amount": 9560.56,
    "total_forecast_amount": 21309.55,
    "average_forecast_amount": 21309.55,
    "total_under_by": 11748.99,
    "total_over_by": 0,
    "periods": [
      {
        "start_date": "2022-08-01",
        "end_date": "2022-08-31",
        "currency_code": "nzd",
        "actual_amount": 9560.56,
        "forecast_amount": 21309.55,
        "refund_amount": 0,
        "current": true,
        "over_budget": false,
        "under_budget": true,
        "over_by": 0,
        "under_by": 11748.99,
        "percentage_used": 44
      }
    ]
  },
  "expense": {
    "start_date": "2022-08-01",
    "end_date": "2022-08-31",
    "currency_code": "nzd",
    "total_actual_amount": -382.5,
    "average_actual_amount": -382.5,
    "total_forecast_amount": -16527.94,
    "average_forecast_amount": -16527.94,
    "total_under_by": 16145.44,
    "total_over_by": 0,
    "periods": [
      {
        "start_date": "2022-08-01",
        "end_date": "2022-08-31",
        "currency_code": "nzd",
        "actual_amount": -382.5,
        "forecast_amount": -16527.94,
        "refund_amount": 0,
        "current": true,
        "over_budget": false,
        "under_budget": true,
        "over_by": 0,
        "under_by": 16145.44,
        "percentage_used": 2
      }
    ]
  }
}

If your resource has templates, use resource_template: not resource:.

Also only one sensor: option:

    sensor:
      - name: "Under by"
        value_template: "{{ value_json.x.expense.periods[0].under_by }}"

      - name: "Over budget?"
        value_template: "{{ value_json.x.expense.periods[0].over_budget }}"

      - name: "Under budget?"
        value_template: "{{ value_json.x.expense.periods[0].under_budget }}"

Hey, thanks for that @tom_l Works a treat now.

1 Like