Hi everyone,
I have the following third party integration: GitHub - BottlecapDave/HomeAssistant-OctopusEnergy: Home Assistant integration for interacting with Octopus Energy
This is really useful. It has current_rate, previous_rate, and next_rate sensors which identify the price paid for electricity on a half hour basis. I can use these in my automations/sensors/dashboards, but for some reason, they do not update precisely on the half hour, sometimes being a off by several seconds, or nearly a minute. My goal is to work around this problem.
All half hourly rates for the day are known in advance. They are presented in this form as attributes of the current_rate sensor:
rate:
value_exc_vat: 21.939999999999998
value_inc_vat: 23.037
valid_from: '2023-02-19T09:30:00+00:00'
valid_to: '2023-02-19T10:00:00+00:00'
tariff_code: E-1R-SILVER-FLEX-22-11-25-J
is_export: false
is_smart_meter: false
rates:
- from: '2023-02-19T00:00:00+00:00'
to: '2023-02-19T00:30:00+00:00'
rate: 23.037
- from: '2023-02-19T00:30:00+00:00'
to: '2023-02-19T01:00:00+00:00'
rate: 23.037
- from: '2023-02-19T01:00:00+00:00'
to: '2023-02-19T01:30:00+00:00'
rate: 23.037
- from: '2023-02-19T01:30:00+00:00'
to: '2023-02-19T02:00:00+00:00'
rate: 23.037
- from: '2023-02-19T02:00:00+00:00'
to: '2023-02-19T02:30:00+00:00'
rate: 23.037
- from: '2023-02-19T02:30:00+00:00'
to: '2023-02-19T03:00:00+00:00'
rate: 23.037
- from: '2023-02-19T03:00:00+00:00'
to: '2023-02-19T03:30:00+00:00'
rate: 23.037
- from: '2023-02-19T03:30:00+00:00'
to: '2023-02-19T04:00:00+00:00'
rate: 23.037
- from: '2023-02-19T04:00:00+00:00'
to: '2023-02-19T04:30:00+00:00'
rate: 23.037
- from: '2023-02-19T04:30:00+00:00'
to: '2023-02-19T05:00:00+00:00'
rate: 23.037
- from: '2023-02-19T05:00:00+00:00'
to: '2023-02-19T05:30:00+00:00'
rate: 23.037
- from: '2023-02-19T05:30:00+00:00'
to: '2023-02-19T06:00:00+00:00'
rate: 23.037
- from: '2023-02-19T06:00:00+00:00'
to: '2023-02-19T06:30:00+00:00'
rate: 23.037
- from: '2023-02-19T06:30:00+00:00'
to: '2023-02-19T07:00:00+00:00'
rate: 23.037
- from: '2023-02-19T07:00:00+00:00'
to: '2023-02-19T07:30:00+00:00'
rate: 23.037
- from: '2023-02-19T07:30:00+00:00'
to: '2023-02-19T08:00:00+00:00'
rate: 23.037
- from: '2023-02-19T08:00:00+00:00'
to: '2023-02-19T08:30:00+00:00'
rate: 23.037
- from: '2023-02-19T08:30:00+00:00'
to: '2023-02-19T09:00:00+00:00'
rate: 23.037
- from: '2023-02-19T09:00:00+00:00'
to: '2023-02-19T09:30:00+00:00'
rate: 23.037
- from: '2023-02-19T09:30:00+00:00'
to: '2023-02-19T10:00:00+00:00'
rate: 23.037
- from: '2023-02-19T10:00:00+00:00'
to: '2023-02-19T10:30:00+00:00'
rate: 23.037
- from: '2023-02-19T10:30:00+00:00'
to: '2023-02-19T11:00:00+00:00'
rate: 23.037
- from: '2023-02-19T11:00:00+00:00'
to: '2023-02-19T11:30:00+00:00'
rate: 23.037
- from: '2023-02-19T11:30:00+00:00'
to: '2023-02-19T12:00:00+00:00'
rate: 23.037
- from: '2023-02-19T12:00:00+00:00'
to: '2023-02-19T12:30:00+00:00'
rate: 23.037
- from: '2023-02-19T12:30:00+00:00'
to: '2023-02-19T13:00:00+00:00'
rate: 23.037
- from: '2023-02-19T13:00:00+00:00'
to: '2023-02-19T13:30:00+00:00'
rate: 23.037
- from: '2023-02-19T13:30:00+00:00'
to: '2023-02-19T14:00:00+00:00'
rate: 23.037
- from: '2023-02-19T14:00:00+00:00'
to: '2023-02-19T14:30:00+00:00'
rate: 23.037
- from: '2023-02-19T14:30:00+00:00'
to: '2023-02-19T15:00:00+00:00'
rate: 23.037
- from: '2023-02-19T15:00:00+00:00'
to: '2023-02-19T15:30:00+00:00'
rate: 23.037
- from: '2023-02-19T15:30:00+00:00'
to: '2023-02-19T16:00:00+00:00'
rate: 23.037
- from: '2023-02-19T16:00:00+00:00'
to: '2023-02-19T16:30:00+00:00'
rate: 23.037
- from: '2023-02-19T16:30:00+00:00'
to: '2023-02-19T17:00:00+00:00'
rate: 23.037
- from: '2023-02-19T17:00:00+00:00'
to: '2023-02-19T17:30:00+00:00'
rate: 23.037
- from: '2023-02-19T17:30:00+00:00'
to: '2023-02-19T18:00:00+00:00'
rate: 23.037
- from: '2023-02-19T18:00:00+00:00'
to: '2023-02-19T18:30:00+00:00'
rate: 23.037
- from: '2023-02-19T18:30:00+00:00'
to: '2023-02-19T19:00:00+00:00'
rate: 23.037
- from: '2023-02-19T19:00:00+00:00'
to: '2023-02-19T19:30:00+00:00'
rate: 23.037
- from: '2023-02-19T19:30:00+00:00'
to: '2023-02-19T20:00:00+00:00'
rate: 23.037
- from: '2023-02-19T20:00:00+00:00'
to: '2023-02-19T20:30:00+00:00'
rate: 23.037
- from: '2023-02-19T20:30:00+00:00'
to: '2023-02-19T21:00:00+00:00'
rate: 23.037
- from: '2023-02-19T21:00:00+00:00'
to: '2023-02-19T21:30:00+00:00'
rate: 23.037
- from: '2023-02-19T21:30:00+00:00'
to: '2023-02-19T22:00:00+00:00'
rate: 23.037
- from: '2023-02-19T22:00:00+00:00'
to: '2023-02-19T22:30:00+00:00'
rate: 23.037
- from: '2023-02-19T22:30:00+00:00'
to: '2023-02-19T23:00:00+00:00'
rate: 23.037
- from: '2023-02-19T23:00:00+00:00'
to: '2023-02-19T23:30:00+00:00'
rate: 23.037
- from: '2023-02-19T23:30:00+00:00'
to: '2023-02-20T00:00:00+00:00'
rate: 23.037
- from: '2023-02-20T00:00:00+00:00'
to: '2023-02-20T00:30:00+00:00'
rate: 71.6415
- from: '2023-02-20T00:30:00+00:00'
to: '2023-02-20T01:00:00+00:00'
rate: 71.6415
- from: '2023-02-20T01:00:00+00:00'
to: '2023-02-20T01:30:00+00:00'
rate: 71.6415
- from: '2023-02-20T01:30:00+00:00'
to: '2023-02-20T02:00:00+00:00'
rate: 71.6415
- from: '2023-02-20T02:00:00+00:00'
to: '2023-02-20T02:30:00+00:00'
rate: 71.6415
- from: '2023-02-20T02:30:00+00:00'
to: '2023-02-20T03:00:00+00:00'
rate: 71.6415
- from: '2023-02-20T03:00:00+00:00'
to: '2023-02-20T03:30:00+00:00'
rate: 71.6415
- from: '2023-02-20T03:30:00+00:00'
to: '2023-02-20T04:00:00+00:00'
rate: 71.6415
- from: '2023-02-20T04:00:00+00:00'
to: '2023-02-20T04:30:00+00:00'
rate: 71.6415
- from: '2023-02-20T04:30:00+00:00'
to: '2023-02-20T05:00:00+00:00'
rate: 71.6415
- from: '2023-02-20T05:00:00+00:00'
to: '2023-02-20T05:30:00+00:00'
rate: 71.6415
- from: '2023-02-20T05:30:00+00:00'
to: '2023-02-20T06:00:00+00:00'
rate: 71.6415
- from: '2023-02-20T06:00:00+00:00'
to: '2023-02-20T06:30:00+00:00'
rate: 71.6415
- from: '2023-02-20T06:30:00+00:00'
to: '2023-02-20T07:00:00+00:00'
rate: 71.6415
- from: '2023-02-20T07:00:00+00:00'
to: '2023-02-20T07:30:00+00:00'
rate: 71.6415
- from: '2023-02-20T07:30:00+00:00'
to: '2023-02-20T08:00:00+00:00'
rate: 71.6415
- from: '2023-02-20T08:00:00+00:00'
to: '2023-02-20T08:30:00+00:00'
rate: 71.6415
- from: '2023-02-20T08:30:00+00:00'
to: '2023-02-20T09:00:00+00:00'
rate: 71.6415
- from: '2023-02-20T09:00:00+00:00'
to: '2023-02-20T09:30:00+00:00'
rate: 71.6415
- from: '2023-02-20T09:30:00+00:00'
to: '2023-02-20T10:00:00+00:00'
rate: 71.6415
- from: '2023-02-20T10:00:00+00:00'
to: '2023-02-20T10:30:00+00:00'
rate: 71.6415
- from: '2023-02-20T10:30:00+00:00'
to: '2023-02-20T11:00:00+00:00'
rate: 71.6415
- from: '2023-02-20T11:00:00+00:00'
to: '2023-02-20T11:30:00+00:00'
rate: 71.6415
- from: '2023-02-20T11:30:00+00:00'
to: '2023-02-20T12:00:00+00:00'
rate: 71.6415
- from: '2023-02-20T12:00:00+00:00'
to: '2023-02-20T12:30:00+00:00'
rate: 71.6415
- from: '2023-02-20T12:30:00+00:00'
to: '2023-02-20T13:00:00+00:00'
rate: 71.6415
- from: '2023-02-20T13:00:00+00:00'
to: '2023-02-20T13:30:00+00:00'
rate: 71.6415
- from: '2023-02-20T13:30:00+00:00'
to: '2023-02-20T14:00:00+00:00'
rate: 71.6415
- from: '2023-02-20T14:00:00+00:00'
to: '2023-02-20T14:30:00+00:00'
rate: 71.6415
- from: '2023-02-20T14:30:00+00:00'
to: '2023-02-20T15:00:00+00:00'
rate: 71.6415
- from: '2023-02-20T15:00:00+00:00'
to: '2023-02-20T15:30:00+00:00'
rate: 71.6415
- from: '2023-02-20T15:30:00+00:00'
to: '2023-02-20T16:00:00+00:00'
rate: 71.6415
- from: '2023-02-20T16:00:00+00:00'
to: '2023-02-20T16:30:00+00:00'
rate: 71.6415
- from: '2023-02-20T16:30:00+00:00'
to: '2023-02-20T17:00:00+00:00'
rate: 71.6415
- from: '2023-02-20T17:00:00+00:00'
to: '2023-02-20T17:30:00+00:00'
rate: 71.6415
- from: '2023-02-20T17:30:00+00:00'
to: '2023-02-20T18:00:00+00:00'
rate: 71.6415
- from: '2023-02-20T18:00:00+00:00'
to: '2023-02-20T18:30:00+00:00'
rate: 71.6415
- from: '2023-02-20T18:30:00+00:00'
to: '2023-02-20T19:00:00+00:00'
rate: 71.6415
- from: '2023-02-20T19:00:00+00:00'
to: '2023-02-20T19:30:00+00:00'
rate: 71.6415
- from: '2023-02-20T19:30:00+00:00'
to: '2023-02-20T20:00:00+00:00'
rate: 71.6415
- from: '2023-02-20T20:00:00+00:00'
to: '2023-02-20T20:30:00+00:00'
rate: 71.6415
- from: '2023-02-20T20:30:00+00:00'
to: '2023-02-20T21:00:00+00:00'
rate: 71.6415
- from: '2023-02-20T21:00:00+00:00'
to: '2023-02-20T21:30:00+00:00'
rate: 71.6415
- from: '2023-02-20T21:30:00+00:00'
to: '2023-02-20T22:00:00+00:00'
rate: 71.6415
- from: '2023-02-20T22:00:00+00:00'
to: '2023-02-20T22:30:00+00:00'
rate: 71.6415
- from: '2023-02-20T22:30:00+00:00'
to: '2023-02-20T23:00:00+00:00'
rate: 71.6415
- from: '2023-02-20T23:00:00+00:00'
to: '2023-02-20T23:30:00+00:00'
rate: 71.6415
- from: '2023-02-20T23:30:00+00:00'
to: '2023-02-21T00:00:00+00:00'
rate: 71.6415
unit_of_measurement: GBP/kWh
device_class: monetary
icon: mdi:currency-gbp
friendly_name: Octopus Energy Electricity 14E0453806 1900013341923 Current Rate
My intention is to create 3 new template sensors that update precisely on the half hour and pick up the correct rate from the array that already exists.
My templating needs some help, but I came up with this:
{% set ts_now = ((as_timestamp(now())/1800)|round(0,'floor')|int * 1800) %}
{{(ts_now | timestamp_custom ('%Y-%m-%dT%H:%M:%S+00:00', local=false)|string)}}
{{ state_attr('sensor.octopus_energy_electricity_XXXX_XXXX_current_rate', 'rates')
| selectattr('from', 'equalto', (ts_now | timestamp_custom ('%Y-%m-%dT%H:%M:%S+00:00', local=false)) )
| map(attribute='rate') }}
Can anyone tell me what I might have got wrong?
Thanks!
James





