How to edit REST address on the fly?

Hi all, newbie here trying to figure out how to integrate an outside API into HA. This will be for Toggl, which I use for time tracking and it would be amazing if HA could make decisions based on it, since it gives very accurate readings of what I’m doing.

I want to fetch this data with REST sensors, but I unfortunately need to make 2, one after the other. I’ve already got the first one sorted:

 #toggl
  #get current time entry
  - platform: rest
    resource: https://www.toggl.com/api/v8/time_entries/current
    name: Toggl
    json_attributes:
        - data
    username: !secret TOGGL_TOKEN
    authentication: basic
    password: api_token
    value_template: '{{ value_json["data"]["description"]}}'
  - platform: template
    sensors:
      toggl_description:
        friendly_name: 'Description'
        value_template: '{{ states.sensor.Toggl.attributes.data.description }}'
      toggl_project_id:
        friendly_name: 'Project ID'
        value_template: '{{ states.sensor.Toggl.attributes.data.pid }}'
      toggl_duration:
        friendly_name: 'Duration'
        value_template: '{{ (states.sensor.Toggl.attributes.data.duration + (states.sensor.seconds_since_epoch.state|float))|timestamp_custom("%-H:%M", false) }}'

That returns me lots of good info, the most relevant of which is the project_id. I need this to make my second REST request, to get the project name. But for that, the URL needs to take the form of https://www.toggl.com/api/v8/projects/[PROJECT_ID].

I can’t figure out how to generate this URL on the fly. My failing attempt is below, although I’m now fairly sure that’s a dead end attempt.

- platform: rest
    resource: https://www.toggl.com/api/v8/projects/{{sensor.toggl_project_id}}
    name: Toggl Project
    json_attributes:
        - data
    username: !secret TOGGL_TOKEN
    authentication: basic
    password: api_token
    value_template: '{{ value_json["data"]["name"]}}'

Any advice/tricks on how to accomplish this easily without too much convolution? I’ve researched and can’t find a good way to make an appended string like this on the fly.

Any help is greatly appreciated.

Well, I think your main problem is you didn’t enclose the template in quotes. Also, you need to change the actual expression. Try this:

resource: "https://www.toggl.com/api/v8/projects/{{ states('sensor.toggl_project_id') }}"

Hmmm, definitely good thought. I ran that and still get a response of Invalid Project ID. I’m attempting to find out how to inspect what the ending address actually is when it makes the request, maybe in the logs somewhere.

Just a note, that I tried the address that it should be in curl and it’s working fine there.

Honestly, I’m not very surprised. I can go into details later if necessary. For now, what does sensor.toggl_project_id look like on the states page? Or, if you put this into the template editor:

{{ states.sensor.toggl_project_id }}

what do you get?

Screen caps to answer your questions:

Oof, I can see why the template was having issues. That’s enlightening.

Actually, not really. The template used:

{{ states('sensor.toggl_project_id') }}

and the states function should have returned the state, which was 96189062. Is that right?

Ah, my mistake. Put in the old instead of the new.

That looks like the address I want. Still getting a bad response back of Invalid Project ID. I’ve tried hardcoding the value into the the resource address, and that gets a valid response. So it still isn’t coming out right. Something about that implementation is still wrong somehow. Is there a way to view logs on rest requests made to see the requested address? Right now the only error I know how to see is what the response is.

Actually, I think the problem is that the REST Sensor’s resource parameter doesn’t seem to support templates. :frowning: I ran into the same thing earlier today. I just assumed it would support templates. It certainly should.

Found a few feature requests regarding this issue, I’m voting for this one here: RESTful Sensor - Support template in resource

3 Likes