Templates in command line sensor + Octopus Energy API

Wanted to drop into the thread to say the issue I was having with the scripts provided by oneofthemany is that my configuration.yaml was the default.

I had to modify it so that the sensors.yaml and the customizations actually loaded like so:

# Configure a default setup of Home Assistant (frontend, api, etc)
default_config:

# Text to speech
tts:
  - platform: google_translate

group: !include groups.yaml
automation: !include automations.yaml
script: !include scripts.yaml
scene: !include scenes.yaml

## New entries

homeassistant:
  customize: !include customize.yaml

sensor: !include sensor.yaml

With those last 3 lines added everything started working as expected. Thanks a bunch oneofthemany!

2 Likes

@BookOfGreg I have edited and added thanks to my original post :slight_smile:

and you’re most certainly welcome!

I set up my integration using this forum thread, thanks to all for the detailed setup, works like a charm.

I did find that my MPAN on the Octopus developer portal is not always the import one. We have Outgoing Octopus (the smart export guarantee since we have solar panels), so we have two MPANs - one for import from the grid and one for export.

The one that the developer portal shows is the export one in my case. I referred to the latest bill to get the correct MPAN for import.

In case this is useful, with the help (or unhelp, to be perfectly honest) I found a way to filter and create a summary stat from a list of rates based on times. There is probably a better way to do this but I am quite happy with how flexible it is.

{% set rates = (state_attr('sensor.agile_rates_today', 'results')) %}

{% set valid_from_cutoff = now().replace(hour=6, minute=0, second=0, microsecond=0) %}
{% set valid_to_cutoff = now().replace(hour=20, minute=0, second=0, microsecond=0) %}

        {% set ns = namespace(total=0, length = 0) %}

{% for item in rates if item.valid_from | as_datetime >= valid_from_cutoff and item.valid_from | as_datetime <= valid_to_cutoff %}
    {% set ns.total = ns.total + item.value_inc_vat | float %}
    {% set ns.length = loop.index %}
{% endfor %}
{{(ns.total / ns.length) | float | round(2)}}

This also feels quite clumsy but I believe this has calculated the median

{% set rates = (state_attr('sensor.agile_rates_today', 'results'))| sort(attribute = 'value_inc_vat') %}
{% set valid_from_cutoff = now().replace(hour=6, minute=0, second=0, microsecond=0) %}
{% set valid_to_cutoff = now().replace(hour=20, minute=0, second=0, microsecond=0) %}
{% set ns = namespace(index=0) %}
{% for item in rates if item.valid_from | as_datetime >= valid_from_cutoff and item.valid_from | as_datetime <= valid_to_cutoff %}
{% set ns.index = loop.index %}
{% endfor %}
{% set l = (ns.index/2)  | round(0) %}
{{ rates[l].value_inc_vat | round(2) }}

Can anyone tell me if there is an official or unofficial integration for octopus? Looks like it would be straightforward given they document their API

If you’re on Agile, there’s a cool UI for the half hourly rates: GitHub - lozzd/octopus-energy-rates-card: This lovelace card for Home Assistant displays the Octopus Energy rate prices per each 30 minute slot

2 Likes