Using current date in API call

Hi everyone, I am not a coder, but I am trying my best, so bare with me.

I am trying to create a sensor in my configuration that will pull data from a rest API. This API is accessed using HTTP and returns JSON. This is my current configuration. (I tried to use code block, hopefully it works)

sensor:
  - platform: rest
    name: WaterSignal_ColesCrossing_Domestic
    #ventkey: &ventkey "redacted"
    #meterid: &meterid "70802"
    resource: 'https://www.watersignal.com/admin2/api/meterdata/authid/redacted/meterid/20069/sdate/2021-07-01-07-00/edate/2021-07-02-12-00/compound/N'
    json_attributes:
      - meterid
      - compound
      - meterdata
      - stamp
      - gallons
      - alert
    value_template: '{{ value_json.meterdata[0].stamp}}'
    device_class: current
    authentication: basic
    scan_interval: 3600

The code I wrote above was able to function, however, as you will see, start data and end date (sdate, edate) are specified in the URL call. Is it possible for me to write this somehow where it will use the current date for start date and the time from 1 hour ago for end date every time it is called?

I’m sure there is a way to do this easily, but it’s taken me several hours just to piece the above together from forum posts for other hardware.

Thank you.

sensor:
  - platform: rest
    name: WaterSignal_ColesCrossing_Domestic
    #ventkey: &ventkey "redacted"
    #meterid: &meterid "70802"
    resource_template: 'https://www.watersignal.com/admin2/api/meterdata/authid/redacted/meterid/20069/sdate/{{ (now()-timedelta(hours=1)).strftime("%Y-%m-%d-%H-%m") }}/edate/{{ now().strftime("%Y-%m-%d-%H-%m") }}/compound/N'
    json_attributes:
      - meterid
      - compound
      - meterdata
      - stamp
      - gallons
      - alert
    value_template: '{{ value_json.meterdata[0].stamp}}'
    device_class: current
    authentication: basic
    scan_interval: 3600
2 Likes

Thank you Chris,

I think this must be real close. It is returning a value of “unknown” now. Is this because the code you provided is being inserted in the middle of a string? Wouldn’t this cause it to be ignored?

What is the json returned by, e.g., https://www.watersignal.com/admin2/api/meterdata/authid/redacted/meterid/20069/sdate/2021-07-06-18-07/edate/2021-07-06-19-07/compound/N

You can check what it is sending by putting this:

'https://www.watersignal.com/admin2/api/meterdata/authid/redacted/meterid/20069/sdate/{{ (now()-timedelta(hours=1)).strftime("%Y-%m-%d-%H-%m") }}/edate/{{ now().strftime("%Y-%m-%d-%H-%m") }}/compound/N'

In the Developer tools Template Editor:

Open your Home Assistant instance and show your template developer tools.

Thank you Tom and Chris.

I really don’t understand what’s wrong at this point. When I do as Tom suggested and lay Chris’s code into the template editor, I can copy that URL into my browser and it pulls the data just fine, but the home assistant code returns “unknown”. Similarly if I paste the output from the template editor back into my yaml config (ie, static dates and times) home assistant also populates the fields correctly.

I’m at a loss.

Well, pretty please, show us the JSON as I asked earlier.

An unknown value likely means:

  • that the JSON is not what you expect
  • or that there is an issue in the value_template

I do not know what is being returned, and I do not know how to see the result.

When I send the following:
https://www.watersignal.com/admin2/api/meterdata/authid/redacted/meterid/20075/sdate/2021-07-05-16-07/edate/2021-07-06-04-07/compound/N

I expect to see the following returned:

{
"meterid":20075,"compound":"N","meterdata":
  [
    {"stamp":"2021-07-05 17:00","gallons":0,"alert":"N"},
{"stamp":"2021-07-05 18:00","gallons":0,"alert":"N"},
{"stamp":"2021-07-05 19:00","gallons":0,"alert":"N"},
{"stamp":"2021-07-05 20:00","gallons":0,"alert":"N"},
{"stamp":"2021-07-05 21:00","gallons":0,"alert":"N"},
{"stamp":"2021-07-05 22:00","gallons":0,"alert":"N"},
{"stamp":"2021-07-05 23:00","gallons":0,"alert":"N"},
{"stamp":"2021-07-06 00:00","gallons":0,"alert":"N"},
{"stamp":"2021-07-06 01:00","gallons":0,"alert":"N"},
{"stamp":"2021-07-06 02:00","gallons":0,"alert":"N"},
{"stamp":"2021-07-06 03:00","gallons":0,"alert":"N"},
{"stamp":"2021-07-06 04:00","gallons":0,"alert":"N"}
]
}

How did you build the rest sensor without seeing the actual JSON? On assumptions from some documentation?
Can’t you just open the url in a browser with the proper authentication?

I suspect empty json might be returned if you use non-specific datetime or too short interval in the url.

Chris,

I can see the returned JSON in a browser, that is how I provided the above example. What confuses me is how the rest sensor returns the correct data in home assistant when my resource url is a simple string, but when I use code for the start and end times, I get a returned value of unknown.

Upon using the template editor it appears that the code is operating correctly, and when I post the output into a browser, the correct JSON is returned, again, confusing me as to why home assistant does not achieve the same result.

But what you showed has a range of 12h.
Not 1h as you were looking for.

So we need to know what, e.g. https://www.watersignal.com/admin2/api/meterdata/authid/redacted/meterid/20075/sdate/2021-07-07-13-28/edate/2021-07-07-14-28/compound/N, i.e. a 1h range, returns.

Hi Chris,

I can’t thank you enough for helping me through this problem. The fault was mine from the beginning. I missed a change you made to the code. You switched “resource” to “resource_template” and I just skimmed over this by accident.

I’ve been racking my brain to figure out why your code did not work when it all checked out in the template editor…

Thank you again for your support!