Templates in command line sensor + Octopus Energy API

It’s something to do with day light savings and BST. I was reading up on the API for some reason it cannot return the last 30 minutes of the day. I’ve updated my sensor to check for 47 results for now.

Hi Chris,

You need to create a handful of sensors. I’ll summarise what they are for reference.

Rest Sensors
The first two rest sensors deal with calling the API and getting the raw data to be stored and interrogated. For these you need to add your meter details. The easiest way to get these is on the Octopus Developer page: Developer | Octopus Energy

e.g. https://api.octopus.energy/v1/electricity-meter-points/260XXXXXX83/meters/19XXXXXX97/consumption/

You should have two different URLs for gas and electricity.

API Key Encoding
Next you need to add your API key - but this needs to be encoded. Copy the key - including the sk_live part and paste it in here: Base64 Encode Online | Base64Encoder. This will give you an encoded output. Just paste this in the YOUR AUTH section of the sensor.

Secondary Sensors
The second set of sensors parse the API data and do two things - check if there is a full day’s worth of results. The API returns half-hourly results so in order to get your daily consumption we need to check that there are enough results for the whole day. As you can see above this can sometimes change. I’ve updated the code to check for 47 results. There was also a slight change to the above code so I’ve copied my version below:

  - platform: template
    sensors:
      electricity:
        unit_of_measurement: "kWh"
        value_template: >
          {% if states('sensor.electricity_data') == "47" %}
          {{ (state_attr("sensor.electricity_data", "results") | map(attribute="consumption") | sum ) | round(2) }}
          {% else %}
          {{ states('sensor.electricity') }}
          {% endif %}
      gas:
        unit_of_measurement: "m3"
        value_template: >
          {% if states('sensor.gas_data') == "47" %}
          {{ (state_attr("sensor.gas_data", "results") | map(attribute="consumption") | sum /11.36 ) | round(2) }}
          {% else %}
          {{ states('sensor.gas') }}
          {% endif %}

Finally restart home assistant and check the sensors are working. You should see the first set of sensors return something like this:

and the second sensor something like this:

image

Any issues let me know - happy to try and help out!

2 Likes
Hi bleep-io,

What an incredibly helpful and thoughtful reply, thank you!

OK so I copied your configuration.yaml entries above in to my yaml file to replace the earlier version from post #15.  I then used the base64 link to generate the code from my Octopus API, actually the only difference seems to be an "=" sign at the end of the generated number........?

Checked the configuration in Server and then restarted the server.  Not working still says "Entity is non-numeric" on the overview.

Please let me give you some further background. On finding this thread I went to my HA file structure and did not have the folder bash_scripts so I simply created it in the \config\bash_script. Was this correct thing to do?

I have not installed any components such as Bash or Json from the store.......would that be the issue?

You mentioned the Rest Sensors

so do I need to enter anything from the Octopus meter details in the Gauge card from the Overview screen? Currently I just selected the entity from the drop down list being sensor.electricity and "sensor.gas"for the gas meter card. I set the maximum value to 20000 as below:

type: gauge
entity: sensor.electricity
min: 0
max: 20000
name: Octopus Energy

In the Gauge card edit window is still says; Entity is non-numeric: sensor.electricity.

Thanx for any further guidance.

Regards
Chris

Just thought this might be useful to you as it removes the 47 or 48 check.

I have SMET 2 meters as well so gas is already in m3.

  - platform: rest
    name: Electricity
    resource_template: >
      {% set starttime = (as_timestamp(now()) - (86400)) | timestamp_custom('%Y-%m-%d', True) + 'T00:00:00' %}
      {% set endtime = (as_timestamp(now())) | timestamp_custom('%Y-%m-%d', True) + 'T00:00:00' %}
      https://api.octopus.energy/v1/electricity-meter-points/MPAN/meters/METER/consumption/?period_from={{starttime}}&period_to={{endtime}}&group_by=day
    headers:
      Authorization: Basic BASE64_APIKEY
    unit_of_measurement: 'kWh'
    value_template: '{{ value_json.results[0].consumption | round(2) }}'
    scan_interval: 7200
    json_attributes_path: "$.results[0]"
    json_attributes:
      - interval_start
      - interval_end
    
  - platform: rest
    name: Gas
    resource_template: >
      {% set starttime = (as_timestamp(now()) - (86400)) | timestamp_custom('%Y-%m-%d', True) + 'T00:00:00' %}
      {% set endtime = (as_timestamp(now())) | timestamp_custom('%Y-%m-%d', True) + 'T00:00:00' %}
      https://api.octopus.energy/v1/gas-meter-points/MPAN/meters/METER/consumption/?period_from={{starttime}}&period_to={{endtime}}&group_by=day
    headers:
      Authorization: Basic BASE64_APIKEY
    unit_of_measurement: 'm3'
    value_template: '{{ value_json.results[0].consumption | round(2) }}'
    scan_interval: 7200
    json_attributes_path: "$.results[0]"
    json_attributes:
      - interval_start
      - interval_end

It’s using the group_by day to sum them all up. I’ve also modified the timestamps midnight to midnight for the previous day.

I’m still playing with it but this is what i have so far all based on your work.

Chris

2 Likes

Thanks Chris - I’ll try this out.

Appreciate your help.

What does your sensor.gas sensor say in developer tools? Same for sensor.electricity?

1 Like

Both the Sensor electricity and gas say state “Unknown”.

Clicking on the “Set state” doesnt change the unknown status.

Login to the new portal for octopus. Scroll down to your meters, do you have any data in there for hourly or half hourly stats. Check both elec and gas. I currently have a issue where there is no data for gas which i’m speaking to their support to resolve.

1 Like

Hi Chris,

Thanx for your very helpful yaml update. I copied that in to my yaml file and now i have the electrical data! Like you my Octopus Gas meter is not reporting and I have advised Octopus and yet waiting for them to fix the issue that they said they can do remotely…

Also I would like the thank bleep-io once again for his very helpful replies!

1 Like

Hello again,

I was wondering if anyone knows the code(s) to pull the actual NOW consumption figures from Octopus? I see that on my smart meter (the stand alone monitor) can display many different readings so does anyone have a list of the required curl or other method to hook these readings and add them in to Home Assistant?

Thanx
Chris.

I had the same - took them a month or so to fix this. However, this now broke my in-home display. Been on them for about 3 weeks to get this resolved!

Hey Chris - you could in theory pull the realtime data - however their API doesn’t update in real-time. It’s usually a day or so behind.

@chriscolden - have you found this too?

Good morning,

thanx for the reply. When Octopus installed my new SMET2 meter they left behind a display unit that can show actual live consumption data and costs. Here is the webpage to the device info: https://www.octopusenergyservices.com/ihd3 So i was thinking that it should be possible to hook that data in to Home Assistant. As the display is only powered from mains and no other cables it must be receiving the data wirelessly.

I only get a full days data so no its not real time. I only have the previous day. Which is why i looked into the group_by day thing as it was a little pointless trying to grab half hourly stats for the previous day. Not sure how that could even work in homeassistant as your pulling them all at once.

Still have issues with gas stats, it wasn’t working at all, contacted them and it started working intermittently for 3 days and now hasn’t updated again since the 9th nov. So working well, lol.

1 Like

Apologies in advance, I haven’t been following the thread from the start, but I’d just like to mention that we could probably turn this into a Blueprint following the release of Home Assistant 1.0.

Or even better, a HACS intergration.

Sorry if this has been mentioned before. Just thought I’d drop this here.

Good work though, guys :slight_smile:

2 Likes

So ive entered my details into your electricity rest sensor but im getting no result returned and this in the logs:

JSON result was not a dictionary or list with 0th element a dictionary

If i run a curl manually i get a single result back so not sure what the problem is here.

Maybe related to this issue? [REST component] JSON result was not a dictionary or list with 0th element a dictionary · Issue #41753 · home-assistant/core · GitHub

If anyone happens to be using a TIG stack like me it’s quite easy to get this data using the http plugin for telegraf. I haven’t been able to get anything other than a daily total in home assistant but that’s good enough for my needs as I can go to grafana if I need a breakdown (you could use lovelace iframe).

These are the json options I used

data_format = "json"
json_time_key = "interval_start"
json_time_format = "2006-01-02T15:04:05Z07:00"
json_query = "results"

Hey Chris,

Are you still getting the previous days consumption updates? Mine has just shown UNKNOWN on my overview card for the past few days…is it a Christmas delay thing?

UPDATE 28/12/20: Today it started reporting yesterdays Electrical consumption so I guess it is just slow updating from the Octopus side…

mine is unknown on the gas. octopus have been looking at it but i have no data since 9h nov, its very annoying but all octopus issues with the api.