I am attempting to retrieve hourly data, from a glowmarkt api call, directly into a rest sensor. Ideally, what I am after is the time to the last hour (i.e. if this runs at 01:45 in morning, it will pick up the 01:00 data from the API results) and the value recorded in the api for that time.
I have done daily readings using node red but thought the hourly could be done via a single rest sensor. I am sure this should be simpler although I could adapt to use node red if required.
An example call to the api is:
https://api.glowmarkt.com/api/v0-1/resource/resourceid/readings?from=2022-10-08T01:00:00&to=2022-10-08T01:00:01&period=PT1H&offset=-60&function=sum
This returns:
{
"status": "OK",
"name": "electricity cost",
"resourceTypeId": "rrrrrrr",
"resourceId": "rrrrrrr",
"query": {
"from": "2022-10-08T01:00:00",
"to": "2022-10-08T01:00:01",
"period": "PT1H",
"function": "sum"
},
"data": [
[
1665190800,
3.228244
]
],
"units": "pence",
"classifier": "electricity.consumption.cost"
}
The 1665190800 is the epoch value for Saturday, 8 October 2022 01:00:00 and the 3.228244 is the value I want to record in the sensor against that time.
I saw similar ideas online so started to experiment with a rest sensor where the GET statement is fixed to the same value as the get statement above (will want to change this at some point to ensure the from and to values are built from the population time so that every hour, it picks up that hours value only) but I clearly don’t understand how the value_template picks up the 3.228244 value as the sensor has remained with a value of unknown.
My test sensor yaml is below:
- platform: rest
name: elec_cost_per_hour_test
method: GET
resource_template: https://api.glowmarkt.com/api/v0-1/resource/resourceid/readings?from=2022-10-08T01:00:00&to=2022-10-08T01:00:01&period=PT1H&offset=-60&function=sum HTTP/1.1
headers:
applicationId: xxxxxxx
Content-Type: application/json
Host: api.glowmarkt.com
Token: tokenid
# Cookie:
value_template: >
{%- set time = utcnow().replace(minute=0).replace(second=0).replace(microsecond=0) %}
{%- set time = time.strftime("%Y-%m-%dT%H:%M:%SZ") %}
{%- set selections = value_json.indicator['data'] | selectattr('datetime_utc','eq',time) | list %}
{{ selections[0,1].value | float if selections | length > 0 else 0.0 }}
scan_interval: 120
Please note I am aware the scan interval is not every hour. While testing I wanted it to occur more often