Templates in command line sensor + Octopus Energy API

@SolarSurfer

Yeah mine too, I came back here to see if there were anyone else with the missing electric consumption figures from th previous day and also I lost the unit pricing for half hourly slots but managed to get that back working again.

So will the electric consumption figures come back again once Octopus puts their clocks forward! Or do we need to do something about the 48/47 time slots in the equation?

Yes itā€™s somewhat annoying. Iā€™ve logged into my api & all the data is there to be pulled but using the code above it doesnā€™t work with yesterdays useage anymore. I am still able to access the agile rates though.

Iā€™m going to post on the Octopus forum to see if anyone has any solutions there or at least bring it to octopus attention

I havenā€™t got access to that formum unfortunately.
Iā€™ve emailed octopus & requested an invite but no response as of yet.

Iā€™m currently on the Go tariff so not sure if Iā€™m allowed in?

Have you re set your request to join?

Managed to get things working again using this guys help & code ā€¦

2 Likes

Interestingly I noticed my data broke after GMT ā†’ BST switchover as well. When I viewed what the API returned I could see it brought back 30mins of the next day data as well as the previous day. Meant to post the changes I made earlier but can see others have already solved it.

Iā€™ve started looking into the idea of making a dedicated Octopus integration (partly to increase my HA knowledge and learn python). Although if someone has already built one thereā€™s no point in me reinventing the wheel (or should it be invertibrate, lol)

Another peculiar issue Iā€™ve noticed is that from the early hours until around 7am, the data from the Octopus API goes to effectively zero, which is messing up my nice graphs! Any ideas why this happens?

I asked the same question a little while ago. It appears that in the early hours of the morning the Octopus API only returns partial data for the day before, often seems to fix up around around in the morning (today for me it was nearly 0846 when it was ok, but I do only query every 2 hours.

Iā€™ve implemented a fix (similar to other examples in this thread) which checks the ā€œcountā€ of results from the API query and only updates the sensor if count equals 48.

i disagree with this recommendation as sharing you api key and account number with a 3rd party is not the best idea

Iā€™m having a problem getting data into HA. I think it must be to do with my ā€˜Authorizationā€™ because it works if I use a browser directly.

My REST Sensor:

  - platform: rest
    name: Octopus Electricity consumption yesterday
    resource_template: >
      {% set mpan = states('input_text.octopus_electricity_mpan') %}
      {% set serial = states('input_text.octopus_electricity_serial') %}
      {% set date = as_timestamp(now() - timedelta(days = 1)) | timestamp_custom('%Y-%m-%d') %}
      https://api.octopus.energy/v1/electricity-meter-points/{{ mpan }}/meters/{{ serial }}/consumption/?period_from={{ date + 'T00:00:00' }}&period_to={{ date + 'T23:59:59' }}
    headers:
      Authorization: !secret octopus_api
    value_template: "{{ value_json.results | sum(attribute='consumption') | round(3) }}"
    unit_of_measurement: 'kWh'
    json_attributes:
      - "results"

If I take the ā€˜resource_templateā€™ and paste it into ā€˜DevTools, Templatesā€™ and then copy the result into a browser it works after asking me for my userid. I provide it with my api key and it returns all the correct data.

My !secret octopus_api looks something like this sk_live_0NGZz1S8aqhzx3WrMfmKg6lE

The error I get is:

Template variable warning: 'dict object' has no attribute 'results' when rendering '{{ value_json.results | sum(attribute='consumption') | round(3) }}'

The output I get in my browser is:

{"count":48,"next":null,"previous":null,"results":[{"consumption":0.294,"interval_start":"2021-07-28T23:30:00+01:00","interval_end":"2021-07-29T00:00:00+01:00"},{"consumption":0.337,"interval_start":"2021-07-28T23:00:00+01:00","interval_end":"2021-07-28T23:30:00+01:00"},{"consumption":0.318,"interval_start":"2021-07-28T22:30:00+01:00","interval_end":"2021-07-28T23:00:00+01:00"}, ... ]}

Thanks in advance.

Your secret should be: ā€œBasic XXXXā€ where XXX is base64 encoded API key.

1 Like

Has anyone managed to get a figure for ā€˜usage so far todayā€™?

Using this:

{% set date = as_timestamp(now()) | timestamp_custom('%Y-%m-%d') %}
https://api.octopus.energy/v1/electricity-meter-points/{{ mpan }}/meters/{{ serial }}/consumption/?period_from={{ date + 'T00:00:00' }}&period_to={{ date + 'T23:59:59' }}

I only get the first half an hour returned, irrespective of what period_to that I use.

Omitting period_from and period_to (which returns a default of 100 results) like this:

{% set date = as_timestamp(now()) | timestamp_custom('%Y-%m-%d') %}
https://api.octopus.energy/v1/electricity-meter-points/{{ mpan }}/meters/{{ serial }}/consumption/

returns the first half an hour of today and then details for the previous 99 half hours.

This is the same for both gas and electricity.

Hi All,
Iā€™ve been following this thread for a few days now and took some time Friday and Last night to get this all up and working.
For me, the above code from alphasixtyfiveā€™s Git hub worked out well for me, the code location has change a little bit however and can now be found here: home-assistant-configs/octopus_energy_agile.yaml at bbd2739dfdb13777cccd0443af950d7d7e66a309 Ā· alphasixtyfive/home-assistant-configs Ā· GitHub
After updating the config with my details and setting the auth token as base64 with the string ā€œBasicā€ in front of it, Iā€™ve finally got it working :slight_smile:

My Question to however is, has anyone managed to get this setup with the new Energy monitoring recently introduced?
I think it something to do with the values on the sensors but Iā€™m not 100% sure, any help getting this sorted would be appreciated

3 Likes

This works perfectly, thanks to all involved the GitHub link in the last post worked first time.

Any idea how to have the unit of measurement in front of the figures as to me it looks wrong behind

i.e it is now 1.06 Ā£ but should be Ā£ 1.06

Hey Joe, yes Iā€™ve managed to get the information into the Energy tab of HA using this ā€œUtility Meter - Home Assistantā€ Utility Meter - Home Assistant which I pointed at a sensor adding electricity and gas consumption together and set to Daily.

The only problem is it displays yesterdayā€™s data today, which I canā€™t seem to fix and suspect needs a change in a future release.

2 Likes

Actually I just went and changed it to separate Daily Electricity and Daily Gas utility monitors I had set up. This allows me to add a fixed rate charge to them, and the Energy tab adds the consumption together for me.

1 Like

Hey @LifeBandit666

Thanks for getting back to me, iā€™ll take a look at that for service for sure.

Thank you for sharing

Hi All,

Iā€™m new to having a smart meter and iā€™ve read through all of this post and itā€™s all a bit confusingā€¦

Iā€™ve managed to get the yesterday consumption as a gaugeā€¦ but what iā€™m confused about is how to get a graph detailing the half hourly figuresā€¦

Can anyone assist me with this?

Iā€™m using the below to get the gauge

  - platform: rest
    name: Electricity consumption yesterday
    resource_template: >-
      {% set mpan = 'mpan' %}
      {% set serial = 'serial' %}
      {% set date = as_timestamp(now() - timedelta(days = 1))|timestamp_custom('%Y-%m-%d') %}
      https://api.octopus.energy/v1/electricity-meter-points/{{ mpan }}/meters/{{ serial }}/consumption/?period_from={{ date + 'T00:00:00' }}&period_to={{ date + 'T23:59:59' }}
    headers:
      Authorization: "Basic base64encoded"
    value_template: "{{ value_json.results|sum(attribute='consumption') | round(3) }}"
    unit_of_measurement: 'kWh'
    json_attributes:
      - "results"
1 Like