Octopus Agile Tariff -Turn device ON and OFF

I’m finding it quite difficult to understand a lot of the advanced settings around Octopus Agile automatons etc etc, but have managed to get a basic display of the 30min rates from my own electric meter sensor.

Time            Price
{%- set results = state_attr('sensor.octopus_energy_electricity_19lxxxxxxx_11zzzzzzzzzzz_current_rate', 'rates') %}
{%- for record in results %}
{%- set ts = as_timestamp(record.from) %}
{%- set ts_now = ((as_timestamp(now())/1800)|round(0,'floor')|int * 1800) %}
{%- if ts >= ts_now %}
{{ ts|timestamp_custom ('%H:%M', local=true)
}}            {{
record.rate }}
{%- endif %}
{%- endfor %}

The card above simply shows the rates on the dashboard, but is enough for me for now

I’d like to achieve is a simple automation, turn ON a device when the current rate is below a certain amount, and turn it OFF if it goes above.

I’ve read until my head is spinning! I came up with what I thought would be a basic script to do this, but it doesn’t work. Could anyone point me in the right direction?

alias: Agile
sequence:
  - condition: numeric_state
    entity_id: sensor.octopus_energy_electricity_19lxxxxxxx_11zzzzzzzzz_current_rate
    below: 0.24
  - type: turn_on
    device_id: f94xxxxxxxxxxxxxxxxxxxxxf
    entity_id: switch.sonoff_plug_3
    domain: switch
mode: restart

Have you not seen the main integration for Octopus Agile?

That will give you the information to file automation as required.

1 Like

Thank you, I haven’t got it working yet but still trying.

Would I be right in thinking that the sensors can be setup to find the lowest price period, but you can’t specify what the price can be?

I don’t see how to make it so that I can set up an automation to turn ON only if the current 30min rate is (say) below 15p.

I have an automation for my car charger that does exactly this. It looks at the current rate and if it’s below the number I set (0.20 for example) then HA enables the charger. Another automation turns it off when the rate goes up.

1 Like

The automation example of mine above doesn’t work, would you mind sharing yours so I can see how to do it?

alias: Agile
sequence:
  - condition: numeric_state
    entity_id: sensor.octopus_energy_electricity_19lxxxxxxx_11zzzzzzzzz_current_rate
    below: 0.24
  - type: turn_on
    device_id: f94xxxxxxxxxxxxxxxxxxxxxf
    entity_id: switch.sonoff_plug_3
    domain: switch
mode: restart

The error from the logs is…

Error initializing 'websocket_api' trigger: In 'numeric_state' condition: entity sensor.octopus_energy_electricity_19lxxxxxxx_110zzzzzzzzzz_current_rate state '{'value_inc_vat': 26.25, 'valid_from': datetime.datetime(2023, 3, 16, 6, 0, tzinfo=datetime.timezone.utc), 'valid_to': datetime.datetime(2023, 3, 16, 6, 30, tzinfo=datetime.timezone.utc), 'tariff_code': 'E-1R-AGILE-FLEX-22-11-25-B'}' cannot be processed as a number

Going back through my setup, I have an additional template involved, which is what drives the automations. Completely forgot about that.

This is the template:

      octopus_current:
       friendly_name: 'Octopus Current Half Hour'
       unit_of_measurement: 'GBP/kWh'
       value_template: "{{ states('sensor.octopus_energy_electricity_21l43xxxxx_20000xxxx_current_rate')| float | round(2) }}"
       icon_template: mdi:currency-gbp

That creates a new sensor, called “sensor.octops_current” which is then used in the automation:

alias: "Car Charger: Enable When Electric Cost Below 5p"
description: ""
trigger:
  - platform: numeric_state
    entity_id: sensor.octopus_current
    below: 0.2
condition: []
action:
  - type: turn_off
    device_id: xxxxx
    entity_id: switch.openevse_sleep_mode
    domain: switch
mode: single

Many thanks, that’s very helpful.

I also discovered whilst trying different approaches that if I just remove the ‘attribute’ from my example above it also works, I thought attribute ‘rate’ was needed, but it isn’t and is what causes the condition to fail. Also as I can see from your example the rate is in £, so the decimal point needed for pence.

Thanks for you help, now working for me one way or the other.

Hi, I’m trying to achieve exactly what you are, but with a lock/unlock on my EV charger (as it’s in plug and charge mode; only unlock when specified Agile rate is met, then lock again if rate goes above specified rate).

However, after reading this thread and trying to create an automation, the trigger isn’t happening automatically to change the state of my lock. I have to run the automation manually for it to change state.

Any visuals on what you’ve done?

How did you make that template? Sorry I’m not vastly experienced in Home Assistant and trying to work out how you did stage 1 “this is the template”. Where do you go in Home Assistant to make that template please?

Did you work this out? I have eventually by doing this:
Using file editor add new file. Call it sensors.yaml and it will sit in config folder. This needs pasting into it with your Octopus entity sensor for current rate instead of the bold below:

platform: template
sensors:
octopus_current:
friendly_name: ‘Octopus Current Half Hour’
unit_of_measurement: ‘GBP/kWh’
value_template: “{{ states(‘sensor.octopus_energy_electricity_20l348xxxx_200002113xxxx_current_rate’)| float | round(2) }}”
icon_template: mdi:currency-gbp

Save it

Then add this single line to the existing configuration.yaml under scene: !include scenes.yaml

sensor: !include sensors.yaml

Restart HA and now you will find a new sensor in entities called Octopus Current Half Hour aka octopus_current. This is the ‘sensor’ you specify when writing any automations such as above. Finally this worked for me as the built in Octopus ‘sensor’ couldn’t trigger anything, so now my immersion turns on below 10p, off above 10p etc etc

Since we’re on the subject, it’s nice to automate when a price drops ‘below’ a static value, however how would you add a sensor or something to iterate over the rates and set its value to the lowest rate for the day? That would be extremely useful for my Dishwasher automation so I don’t need to keep checking the rates manually and updating its ‘below’ threshold.

Hi Owen,

assuming you installed the octopus energy integration, you can open the integration screen and click “add entry”. I pasted here an example of one of my entities “octopus_energy_target_water_heater_morning_schedule” that I use to warm up the boiler water before we wake up.

The entity is set up so that it finds the 3 cheapest 30 minutes slots between the set start and end time.
In the integration doc you can find more details about all the options to define your entity.

Once you created the entity in your HA you can find a new binary_sensor available.
You can use the new binary sensor to define an automation trigger, this is one of my triggers:

{% if states('binary_sensor.octopus_energy_target_water_heater_morning_schedule') == 'on' %}
  True
{% else %}
  False
{% endif %}

Then I add conditions if you have any or I leave it blank.
And last in the actions I add the device I want to turn on, and any other action I want to perform like changing the value of a State.

I hope this helps you so you don’t need to check anymore what are the cheapest time slots :wink:

2 Likes

That’s awesome! Thank you for sharing that, I never knew it had this built in. That’s simplified my automation. I ended up using the current rate as a trigger and the following template in my “condition”:

{{ state_attr('sensor.octopus_energy_electricity_22j0236676_1300054647323_current_rate', 'rate')['value_inc_vat'] == state_attr('sensor.octopus_energy_electricity_22j0236676_1300054647323_current_rate', 'current_day_min_rate') }}

This worked in the sense that at the exact cheapest rate for the day it will run, but not ideal when that’s usually around mid-day - especially for a dishwasher so you’re waiting for it to run.

I’m a bit lost in this one too and would appreciate a bit of help. I have the correct time in the binary sensor but I’m not absolutely clear on how to write the value part of the automation.

From the example given above it’s

{% if states('binary_sensor.octopus_energy_target_dishwasher') %} True

Is that right? if so, is the "== ‘on’ " in the example above just checking it isn’t already on?

EDIT: Just tried it, with the “if” it wants an else but I haven’t got any else to put in :-). Removing the if it fails with states being an unknown tag

DOUBLE EDIT: The editor doesn’t complain now it looks like this

{states('binary_sensor.octopus_energy_dishwasher') == 'on' } True

So is that OK?

Hi @owen_a,
I’ve just set up my dishwasher automation this week, just like you pointed out is not great if it runs when you need it.
To avoid that I created three sensors, this is the set of triggers I used to turn it on:

alias: Start Dishwasher on cheapest energy time
description: ""
trigger:
  - platform: template
    value_template: >-
      {% if is_state('binary_sensor.octopus_energy_target_dishwasher_night_start_time', 'on') %}
        True
      {% else %}
        False
      {% endif %}
    for:
      hours: 0
      minutes: 0
      seconds: 15
  - platform: template
    value_template: >-
      {% if is_state('binary_sensor.octopus_energy_target_dishwasher_morning_start_time', 'on') %}
        True
      {% else %}
        False
      {% endif %}
    for:
      hours: 0
      minutes: 0
      seconds: 15
  - platform: template
    value_template: >-
      {% if is_state('binary_sensor.octopus_energy_target_dishwasher_afternoon_start_time', 'on') %}
        True
      {% else %}
        False
      {% endif %}
    for:
      hours: 0
      minutes: 0
      seconds: 15

This way it doesn’t mean you get the cheapest time of the day but at least you balance convenience with cost.

1 Like

@GeoffUK, hopefully I understood what you are trying to do …

I suppose you want to create a Trigger for an automation based on an Octopus Agile Target event.
If that is the case you should create something like this:

Add Trigger > Template > Enter a template code similar to the one in my screenshot.
It must print a True or False.

Or similar to what you were trying, use the following within your Trigger template:

{{ states('binary_sensor.octopus_energy_dishwasher') == 'on' }}

Thanks for that, seems to do the trick!!

Hi, I am also trying to achieve the same thing, but am having lots of issues setting up a new template.

any idea what am i doing wrong?

Thank you!

If you can let me know what am I doing wrong with setting up a template ( see my post above) that would be much appreciated!