Templates in command line sensor + Octopus Energy API

it will take approx 24 hours for you to get data but it does work - I have been battling against it for ages and tweaking code to get it to work, but my electricity API pull has been up and running for about 1 month now:


2 Likes

thats because its not in the customize.yaml file, as you need device and state class values applied to the sensors to make it work with the energy dashboard

you don’t really need TBH, i was just using it on another dashboard to cross reference ahead of getting energy dashboard working

1 Like

Thank you. Your answers have been really helpful.

no worries at all. my pain your gain :wink:

1 Like

Been reading all this with interest. I’ve been using HA for a couple of years to monitor my boiler and central heating. I’ve recently been moved to Octopus Energy and have a SMETS2 smart meter, so would like to get that data into HA. I’ve enabled half-hourly data on my Octopus account and can download .CSVs so just need to stitch it all in HA.

I guess I’m interested in half-hourly consumption which I can then aggregate somehow by day, week, month. I don’t need realtime (yet) so the Octopus API sounds the route to go. I’m on the price-cap tariff, so not expecting frequent price changes, but it would be good if that’s retrievable through the API so the rates are up to date.

Is there a wiki or other doc summarising how to set things up? Thanks.

This thread should help you with the consumption side of things - Hildebrandglow - Smart Meters - SMETS2

if you are after csv ingestion please look here:

Hi, been quietly following this thread, many thanks for all the efforts, very useful.
I notice that the number of entries check doesn’t cater for daylight savings …

Note: I have only just implemented the entire code by [oneofthemany], and have just tested with the data from the 31st October, as my earlier version failed due to the 48 only check … it all seems to work great.

e.g. Gas cost

      {% if usage is defined and (usage|count in [48,46,50]) %}

and Electricity cost

      {% if usage is defined and (usage|count in [48,46,50]) %}
        {% set ns = namespace(total=0) %}
        {% for p in range(0, usage|count) %}

and for Electricity cost off peak

      {% if usage is defined and (usage|count in [8,6,10]) %}
        {% set ns = namespace(total=0) %}
        {% for p in range(0, usage|count) %}
1 Like

@oneofthemany - thanks for coming back.

Mention of CSV was a red herring. I’m keen to go the API route (rather than realtime with Hildebrand etc.). I can live with data 24 hours out of date - the IHD does the realtime bit for me. It’s more being able to look back at the half-hourly data.

So… just curious whether there’s a step-by-step guide to getting data via the API into HA and into the Energy view (or whatever else is appropriate). Seen lots of good stuff in this thread - it’s just piecing it all together! Would also be good if Phil at Octopus was able to drive some HA stuff from their end. We’ll see.

Thanks.

well you need the following files located in your config folder:

sensor.yaml
customize.yaml

Then simply follow the steps above and you should be good to go:

I also answered most questions that would come out of the above guide to @DaveIr

That’s about it

1 Like

Brilliant - thanks for that. Just what I needed.

I’m on the tariff for Avro Energy orphans, called “Flexible Avro”. Do I need to changed anything to reflect that or does the API key, MPAN/MPRN and meter serial numbers automatically sort it all out? Thanks.

I have no idea when and if your tariff will be exposed by Octopus or even what that looks like as octopus only took on Avro at the back end of September.

In terms of what you need to do next.

I would recommend you have a read here:

“Octopus Energy APIs — Octopus Energy API 1.0 documentation” Octopus Energy APIs — Octopus Energy API 1.0 documentation

But the best piece of advice would be getting confirmation with Octopus of what’s exposed, if anything at all prior to moving forward, as this will impact what’s required for your overall config.

Good luck with it all.

Thanks. I’m trying to start small and build up. So, just trying to get gas consumption at this point.

With this code in configuration.yaml…

- platform: rest
  name: Gas consumption
  resource_template: >-
    {% set mprn = 'MY MPRN' %}
    {% set serial = 'MY SERIAL NO' %}
    {% set date = as_timestamp(now() - timedelta(days = 1))|timestamp_custom('%Y-%m-%d') %}
    https://api.octopus.energy/v1/gas-meter-points/{{ mprn }}/meters/{{ serial }}/consumption/?period_from={{ date + 'T00:00:00' }}&period_to={{ date + 'T23:59:59' }}
  headers:
    Authorization: !secret octopus_auth
  value_template: "{{ value_json.results|sum(attribute='consumption')|round(3) }}"
  unit_of_measurement: 'kWh'
  device_class: energy
  json_attributes:
    - "results"

I’m getting errors of the form…

2021-11-06 14:30:41 WARNING (MainThread) [homeassistant.helpers.template] Template variable warning: 'dict object' has no attribute 'results' when rendering '{{ value_json.results|sum(attribute='consumption')|round(3) }}'

The first section correctly renders into a URL to get my gas consumption. When I paste it into a browser and supply my API key as the username, 48 half-hourly readings come back in JSON. So the issue appears to be the value_template section. Am I doing something really silly for this not to work? Probably…
image

I was having this issue for a long time; it was to do with the secret. You need to ensure you have encoded it to base64…

Good spot. Thanks. That’s got rid of the errors in the Core log and some sort of data is coming back but more work needed.

1 Like

Apologies - bit slow cracking this. I’ve got some data coming in…

But just one bar for the whole day at a random time rather than the 48 half-hourly data. I suspect that’s because the sensor is defined as…

value_template: "{{ value_json.results|sum(attribute='consumption')|round(3) }}"

… and I need to change that somehow to remove the ‘sum’ function? Thanks…

thats because the results only come available in retrospect and they come all at once which is why the sum value is present.

to split them up it is going to a fair bit of work which is out of my coding knowledge

as an FYI the gas consumption has been rather fiddly and I have to do the following in the sensor.yaml file to make it work:

- platform: rest
  name: Gas consumption m3
  resource_template: >-
    {% set mprn = '<insert_mprn>' %}
    {% set serial = '<insert_serial>' %}
    {% set date = as_timestamp(now() - timedelta(days = 1))|timestamp_custom('%Y-%m-%d') %}
    https://api.octopus.energy/v1/gas-meter-points/{{ mprn }}/meters/{{ serial }}/consumption/?period_from={{ date + 'T00:00:00' }}&period_to={{ date + 'T23:59:59' }}
  headers:
    Authorization: !secret octopus_auth
  value_template: "{{ value_json.results|sum(attribute='consumption')|round(3) }}"
  unit_of_measurement: 'm3'
  device_class: energy
  json_attributes:
    - "results"

- platform: template
  sensors:
    gas_consumption:
      friendly_name: "Gas consumption"
      value_template: "{{ (states('sensor.gas_consumption_m3') | float * 1.02264 * 39.1 / 3.6)|round(3)}}"
      unit_of_measurement: 'kWh'
      device_class: energy

- platform: template
  sensors:
    gas_cost:
      friendly_name: "Gas cost"
      value_template: "{{ (states('sensor.gas_consumption') | float * 3.95 / 100 + 23.85 / 100)|round(2) }}"
      unit_of_measurement: '£'
      device_class: monetary

then finally the customize.yaml to be as follows for the cost:

sensor.gas_cost:
  unit_of_measurement: GBP
  device_class: monetary
  state_class: total_increasing
  last_reset: '1970-01-01T00:00:00+00:00'

hope this helps

This is exactly the question I’ve been trying to work out the answer to… it seems to me like the entire energy dashboard as been built on the assmption metering data will arrive in real time and be stored by HA as it arrives, hence the single reading with of “consumption” being read into the graphs at the point yesterday’s data arrives (so for me at 6am today I had a single massive consumption recorded which should actually be in yesterday’s graphs.

I find the documentation for HA really confusing at the best of times, but I would love to work out how to apply the reading from @oneofthemany’s hard work to be recorded on the right day, if we can work that out, then having it record each 30 min segment through the day retrospectively may be possible.

Any pointers or thoughts from anyone would be great; I am happy to test or try any ideas people have.

very rarely use the daily element and think that the weekly tab offers better insight based on my setup:


I can understand the quest for half hourly data, but ultimately it comes down to the end user and their specific needs, but and there is a but, I personally don’t need it to be that granular and the 4 hour slots I have set up make it very easy to understand my usage and where I can perhaps be better with my consumption.

The easiest way is to get kWh usage in half hour slots, would be through smart sockets, but you are left with very little in the marketplace in terms of sockets that actually function and deliver telemetry - I have gone all out and used a £100 socket only to find that when I used it for charging my PHEV it crashed my whole zigbee network - luckily I had it all in containers and it was easy to recover the state from last backup

I can imagine moving forward the Energy tab will be better and offer far more, and I am looking forward to that, but at the moment I am more than happy with what I have until things get better.

1 Like