Hello all,
Struggling at the moment to get a sensor working to read out my electricity and gas meter readings.
My provider uses an API which is documented here: https://developer.octopus.energy/docs/api/ - see the section about List Consumption for a meter.
A REST GET command will return a JSON with all of the half-hourly meter usage readings for the time interval stated in the parameters. The JSON looks like this:
{
"count": 48,
"next": null,
"previous": null,
"results": [
{
"consumption": 0.063,
"interval_start": "2018-05-19T23:00:00Z",
"interval_end": "2018-05-19T23:30:00Z"
},
{
"consumption": 0.071,
"interval_start": "2018-05-19T22:30:00Z",
"interval_end": "2018-05-19T23:00:00Z"
}
]
}
I can confirm the service works using both REST and the cURL formatting using a static URL, for example:
platform: command_line
name: electricity
value_template: '{{ value_json.count }}'
command: 'curl -H "Authorization: Basic XXXXXXXXXXXXXXXXX" "https://api.octopus.energy/v1/electricity-meter-points/YYYYYYYYYYYYYYYYY/meters/18P5016779/consumption/?period_from=2019-01-31T00:00:00"'
Hereās the problem: if you donāt state the period_from
parameter the GET returns thousands of values. I want just the previous dayās values, because from what I can see, the meter readings only update once a day around midnight - so I canāt get live readings, and the most recent readings I can get are for the 24 hours leading up to midnight the previous night.
It therefore makes sense that I use a GET request with a period_from
parameter that dynamically changes to whatever yesterdayās date was.
Just to keep things simple, I made
value_template: '{{ value_json.count }}'
ā¦because that should just return a value of 48 each time (48 half-hourly readings over a 24-hour period).
This worked absolutely fine with sensor.command_line and a URL without templating - I got the value 48 returned without issues.
From what I understand - the REST sensor doesnāt support templates in the URL, but the command line sensor does (or at least, is meant to).
At first, I tried this:
command: 'curl -H "Authorization: Basic XXXXXXXXXXXXXXXXX" "https://api.octopus.energy/v1/electricity-meter-points/YYYYYYYYYYYYYYYYY/meters/18P5016779/consumption/?period_from={{ (as_timestamp(now()) - (24*3600)) | timestamp_custom("%Y-%m-%d", True) }}T00:00:00"'
ā¦which didnāt work, presumably because I ran out of " and ā to play with. So I made a separate sensor:
sensors:
date_yesterday:
friendly_name: Date Yesterday
value_template: '{{ (as_timestamp(now()) - (24*3600)) | timestamp_custom("%Y-%m-%d", True) }}'
and then tried:
command: 'curl -H "Authorization: Basic XXXXXXXXXXXXXXXXX" "https://api.octopus.energy/v1/electricity-meter-points/YYYYYYYYYYYYYYYYY/meters/18P5016779/consumption/?period_from={{ states.sensor.date_yesterday.state }}T00:00:00"'
This didnāt work either. I even tried making a separate sensor purely to generate the URL string:
electricity_url:
friendly_name: Electricity URL
value_template: 'curl -H "Authorization: Basic c2tfbGl2ZV9QbjhvYVBOZFNEMUxJV2I4dU8xS2NEZWc6" "https://api.octopus.energy/v1/electricity-meter-points/2200042780238/meters/18P5016779/consumption/?period_from={{ states.sensor.date_yesterday.state }}T00:00:00"'
ā¦followed by:
platform: command_line
name: electricity
value_template: '{{ value_json.count }}'
command: '{{ states.sensor.electricity_url.state }}'
This hasnāt worked either! I have checked the states of both sensor.electricity_url and sensor.date_yesterday and they both give the correct values.
The moment I revert back to using a URL without templating in the command line sensor - it works again.
Iām stumped as to what to try next - it really does just seem like the problem is the inclusion of templating in the command line for the sensor. Does anyone have any insights into this or suggestions I could try?
Many thanks
Al