Sure:
- platform: command_line
name: [gas or electricity]
value_template: '{{ value_json.count }}'
json_attributes:
- results
command: >-
curl -H '"'Authorization: Basic [encoded API key here]'"' '"'https://api.octopus.energy/v1/[insert 'gas' or 'electricity' here]-meter-points/[mpan or mprn]/meters/[meter serial number here]/consumption/?period_from={{ (as_timestamp(now()) - (24*3600)) | timestamp_custom("%Y-%m-%d", True) }}T00:00:00'"'
Note that for authorization you will need the encoded API key - easiest way to do this is to get your API key, go to https://client.restlet.com, click ‘set an authorization’, copy the API key into the username box, and click ‘set’ - you should then see the header formed with 'Basic + [encoded API key].
This should return a state of ‘48’ which is the number of 30minute intervals returned by the request, then all the attributes are the measurements of the intervals.
I’m rubbish at coding so the way I got a useable value is with this:
gas_yesterday:
friendly_name: Gas Use Yesterday
icon_template: mdi:fire
unit_of_measurement: 'kWh'
value_template: "{{(state_attr('sensor.gas', 'results')[0]['consumption']+state_attr('sensor.gas', 'results')[1]['consumption']+....state_attr('sensor.gas', 'results')[47]['consumption'])|round(3)}}"
…yes, it’s a long line of code adding together all 48 values, then rounding it to 3 decimal places, but it works, and gives the total gas/electricity used in the previous day.
From what I can tell the meter sends the data for the previous day off around 0700h each day because that’s when the sensor refreshes with the next lot of data - so from 0000h to 0700h this won’t have a value because the API hasn’t caught up with the command line sensor.
I show both gas and electricity meter readings on the front end in a nice little graph showing my weekly energy usage:
A much more elegant solution would be a nice little bash script that loops around and adds the dictionary array of consumption values together - if you manage this please let me have a copy of your code!
Let me know if you run into any issues.
Oh - by the way - don’t be alarmed by your gas consumption numbers, the API reports it in kWh, not metres cubed!!!