Template query with array

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

Almost there… revise to this

| map(attribute='rate') | first }}
1 Like

Thank you, however with that I get

UndefinedError: No first item, sequence was empty.

? with me this works fine, are you sure your sensor has the attribute ‘rates’ populated?

EDIT:
I pasted your data above in the attributes of a entity “testevents” and then

1 Like

Thanks. The attributes I shared are copied straight from the state attributes yaml from Developer Tools, States, so I believe everything is ok - not sure how to confirm?

I had tried what you suggested, but yes, struggling to make it work, and not sure why.

So what does it show in Templates without the | first?

<generator object sync_do_map at 0x9e97ca00>

Which I have struggled to understand!

Because it sends a object.
What happens when you use | map(attribute=‘rate’) | list | first
or just | list ?

1 Like

This…

{% set ts_now = ((as_timestamp(now())/1800)|round(0,'floor')|int * 1800) %}
        {{ state_attr('sensor.octopus_energy_electricity_XXXX_XXXXX_current_rate', 'rates')
         }}

Returns:

[{'from': datetime.datetime(2023, 2, 19, 0, 0, tzinfo=datetime.timezone.utc), 'to': datetime.datetime(2023, 2, 19, 0, 30, tzinfo=datetime.timezone.utc), 'rate': 23.037}, {'from': datetime.datetime(2023, 2, 19, 0, 30, tzinfo=datetime.timezone.utc), 'to': datetime.datetime(2023, 2, 19, 1, 0, tzinfo=datetime.timezone.utc), 'rate': 23.037}, {'from': datetime.datetime(2023, 2, 19, 1, 0, tzinfo=datetime.timezone.utc), 'to': datetime.datetime(2023, 2, 19, 1, 30, tzinfo=datetime.timezone.utc), 'rate': 23.037}, {'from': datetime.datetime(2023, 2, 19, 1, 30, tzinfo=datetime.timezone.utc), 'to': datetime.datetime(2023, 2, 19, 2, 0, tzinfo=datetime.timezone.utc), 'rate': 23.037}, {'from': datetime.datetime(2023, 2, 19, 2, 0, tzinfo=datetime.timezone.utc), 'to': datetime.datetime(2023, 2, 19, 2, 30, tzinfo=datetime.timezone.utc), 'rate': 23.037}, {'from': datetime.datetime(2023, 2, 19, 2, 30, tzinfo=datetime.timezone.utc), 'to': datetime.datetime(2023, 2, 19, 3, 0, tzinfo=datetime.timezone.utc), 'rate': 23.037}, {'from': datetime.datetime(2023, 2, 19, 3, 0, tzinfo=datetime.timezone.utc), 'to': datetime.datetime(2023, 2, 19, 3, 30, tzinfo=datetime.timezone.utc), 'rate': 23.037}, {'from': datetime.datetime(2023, 2, 19, 3, 30, tzinfo=datetime.timezone.utc), 'to': datetime.datetime(2023, 2, 19, 4, 0, tzinfo=datetime.timezone.utc), 'rate': 23.037}, {'from': datetime.datetime(2023, 2, 19, 4, 0, tzinfo=datetime.timezone.utc), 'to': datetime.datetime(2023, 2, 19, 4, 30, tzinfo=datetime.timezone.utc), 'rate': 23.037}, {'from': datetime.datetime(2023, 2, 19, 4, 30, tzinfo=datetime.timezone.utc), 'to': datetime.datetime(2023, 2, 19, 5, 0, tzinfo=datetime.timezone.utc), 'rate': 23.037}, {'from': datetime.datetime(2023, 2, 19, 5, 0, tzinfo=datetime.timezone.utc), 'to': datetime.datetime(2023, 2, 19, 5, 30, tzinfo=datetime.timezone.utc), 'rate': 23.037}, {'from': datetime.datetime(2023, 2, 19, 5, 30, tzinfo=datetime.timezone.utc), 'to': datetime.datetime(2023, 2, 19, 6, 0, tzinfo=datetime.timezone.utc), 'rate': 23.037}, {'from': datetime.datetime(2023, 2, 19, 6, 0, tzinfo=datetime.timezone.utc), 'to': datetime.datetime(2023, 2, 19, 6, 30, tzinfo=datetime.timezone.utc), 'rate': 23.037}, {'from': datetime.datetime(2023, 2, 19, 6, 30, tzinfo=datetime.timezone.utc), 'to': datetime.datetime(2023, 2, 19, 7, 0, tzinfo=datetime.timezone.utc), 'rate': 23.037}, {'from': datetime.datetime(2023, 2, 19, 7, 0, tzinfo=datetime.timezone.utc), 'to': datetime.datetime(2023, 2, 19, 7, 30, tzinfo=datetime.timezone.utc), 'rate': 23.037}, {'from': datetime.datetime(2023, 2, 19, 7, 30, tzinfo=datetime.timezone.utc), 'to': datetime.datetime(2023, 2, 19, 8, 0, tzinfo=datetime.timezone.utc), 'rate': 23.037}, {'from': datetime.datetime(2023, 2, 19, 8, 0, tzinfo=datetime.timezone.utc), 'to': datetime.datetime(2023, 2, 19, 8, 30, tzinfo=datetime.timezone.utc), 'rate': 23.037}, {'from': datetime.datetime(2023, 2, 19, 8, 30, tzinfo=datetime.timezone.utc), 'to': datetime.datetime(2023, 2, 19, 9, 0, tzinfo=datetime.timezone.utc), 'rate': 23.037}, {'from': datetime.datetime(2023, 2, 19, 9, 0, tzinfo=datetime.timezone.utc), 'to': datetime.datetime(2023, 2, 19, 9, 30, tzinfo=datetime.timezone.utc), 'rate': 23.037}, {'from': datetime.datetime(2023, 2, 19, 9, 30, tzinfo=datetime.timezone.utc), 'to': datetime.datetime(2023, 2, 19, 10, 0, tzinfo=datetime.timezone.utc), 'rate': 23.037}, {'from': datetime.datetime(2023, 2, 19, 10, 0, tzinfo=datetime.timezone.utc), 'to': datetime.datetime(2023, 2, 19, 10, 30, tzinfo=datetime.timezone.utc), 'rate': 23.037}, {'from': datetime.datetime(2023, 2, 19, 10, 30, tzinfo=datetime.timezone.utc), 'to': datetime.datetime(2023, 2, 19, 11, 0, tzinfo=datetime.timezone.utc), 'rate': 23.037}, {'from': datetime.datetime(2023, 2, 19, 11, 0, tzinfo=datetime.timezone.utc), 'to': datetime.datetime(2023, 2, 19, 11, 30, tzinfo=datetime.timezone.utc), 'rate': 23.037}, {'from': datetime.datetime(2023, 2, 19, 11, 30, tzinfo=datetime.timezone.utc), 'to': datetime.datetime(2023, 2, 19, 12, 0, tzinfo=datetime.timezone.utc), 'rate': 23.037}, {'from': datetime.datetime(2023, 2, 19, 12, 0, tzinfo=datetime.timezone.utc), 'to': datetime.datetime(2023, 2, 19, 12, 30, tzinfo=datetime.timezone.utc), 'rate': 23.037}, {'from': datetime.datetime(2023, 2, 19, 12, 30, tzinfo=datetime.timezone.utc), 'to': datetime.datetime(2023, 2, 19, 13, 0, tzinfo=datetime.timezone.utc), 'rate': 23.037}, {'from': datetime.datetime(2023, 2, 19, 13, 0, tzinfo=datetime.timezone.utc), 'to': datetime.datetime(2023, 2, 19, 13, 30, tzinfo=datetime.timezone.utc), 'rate': 23.037}, {'from': datetime.datetime(2023, 2, 19, 13, 30, tzinfo=datetime.timezone.utc), 'to': datetime.datetime(2023, 2, 19, 14, 0, tzinfo=datetime.timezone.utc), 'rate': 23.037}, {'from': datetime.datetime(2023, 2, 19, 14, 0, tzinfo=datetime.timezone.utc), 'to': datetime.datetime(2023, 2, 19, 14, 30, tzinfo=datetime.timezone.utc), 'rate': 23.037}, {'from': datetime.datetime(2023, 2, 19, 14, 30, tzinfo=datetime.timezone.utc), 'to': datetime.datetime(2023, 2, 19, 15, 0, tzinfo=datetime.timezone.utc), 'rate': 23.037}, {'from': datetime.datetime(2023, 2, 19, 15, 0, tzinfo=datetime.timezone.utc), 'to': datetime.datetime(2023, 2, 19, 15, 30, tzinfo=datetime.timezone.utc), 'rate': 23.037}, {'from': datetime.datetime(2023, 2, 19, 15, 30, tzinfo=datetime.timezone.utc), 'to': datetime.datetime(2023, 2, 19, 16, 0, tzinfo=datetime.timezone.utc), 'rate': 23.037}, {'from': datetime.datetime(2023, 2, 19, 16, 0, tzinfo=datetime.timezone.utc), 'to': datetime.datetime(2023, 2, 19, 16, 30, tzinfo=datetime.timezone.utc), 'rate': 23.037}, {'from': datetime.datetime(2023, 2, 19, 16, 30, tzinfo=datetime.timezone.utc), 'to': datetime.datetime(2023, 2, 19, 17, 0, tzinfo=datetime.timezone.utc), 'rate': 23.037}, {'from': datetime.datetime(2023, 2, 19, 17, 0, tzinfo=datetime.timezone.utc), 'to': datetime.datetime(2023, 2, 19, 17, 30, tzinfo=datetime.timezone.utc), 'rate': 23.037}, {'from': datetime.datetime(2023, 2, 19, 17, 30, tzinfo=datetime.timezone.utc), 'to': datetime.datetime(2023, 2, 19, 18, 0, tzinfo=datetime.timezone.utc), 'rate': 23.037}, {'from': datetime.datetime(2023, 2, 19, 18, 0, tzinfo=datetime.timezone.utc), 'to': datetime.datetime(2023, 2, 19, 18, 30, tzinfo=datetime.timezone.utc), 'rate': 23.037}, {'from': datetime.datetime(2023, 2, 19, 18, 30, tzinfo=datetime.timezone.utc), 'to': datetime.datetime(2023, 2, 19, 19, 0, tzinfo=datetime.timezone.utc), 'rate': 23.037}, {'from': datetime.datetime(2023, 2, 19, 19, 0, tzinfo=datetime.timezone.utc), 'to': datetime.datetime(2023, 2, 19, 19, 30, tzinfo=datetime.timezone.utc), 'rate': 23.037}, {'from': datetime.datetime(2023, 2, 19, 19, 30, tzinfo=datetime.timezone.utc), 'to': datetime.datetime(2023, 2, 19, 20, 0, tzinfo=datetime.timezone.utc), 'rate': 23.037}, {'from': datetime.datetime(2023, 2, 19, 20, 0, tzinfo=datetime.timezone.utc), 'to': datetime.datetime(2023, 2, 19, 20, 30, tzinfo=datetime.timezone.utc), 'rate': 23.037}, {'from': datetime.datetime(2023, 2, 19, 20, 30, tzinfo=datetime.timezone.utc), 'to': datetime.datetime(2023, 2, 19, 21, 0, tzinfo=datetime.timezone.utc), 'rate': 23.037}, {'from': datetime.datetime(2023, 2, 19, 21, 0, tzinfo=datetime.timezone.utc), 'to': datetime.datetime(2023, 2, 19, 21, 30, tzinfo=datetime.timezone.utc), 'rate': 23.037}, {'from': datetime.datetime(2023, 2, 19, 21, 30, tzinfo=datetime.timezone.utc), 'to': datetime.datetime(2023, 2, 19, 22, 0, tzinfo=datetime.timezone.utc), 'rate': 23.037}, {'from': datetime.datetime(2023, 2, 19, 22, 0, tzinfo=datetime.timezone.utc), 'to': datetime.datetime(2023, 2, 19, 22, 30, tzinfo=datetime.timezone.utc), 'rate': 23.037}, {'from': datetime.datetime(2023, 2, 19, 22, 30, tzinfo=datetime.timezone.utc), 'to': datetime.datetime(2023, 2, 19, 23, 0, tzinfo=datetime.timezone.utc), 'rate': 23.037}, {'from': datetime.datetime(2023, 2, 19, 23, 0, tzinfo=datetime.timezone.utc), 'to': datetime.datetime(2023, 2, 19, 23, 30, tzinfo=datetime.timezone.utc), 'rate': 23.037}, {'from': datetime.datetime(2023, 2, 19, 23, 30, tzinfo=datetime.timezone.utc), 'to': datetime.datetime(2023, 2, 20, 0, 0, tzinfo=datetime.timezone.utc), 'rate': 23.037}, {'from': datetime.datetime(2023, 2, 20, 0, 0, tzinfo=datetime.timezone.utc), 'to': datetime.datetime(2023, 2, 20, 0, 30, tzinfo=datetime.timezone.utc), 'rate': 71.6415}, {'from': datetime.datetime(2023, 2, 20, 0, 30, tzinfo=datetime.timezone.utc), 'to': datetime.datetime(2023, 2, 20, 1, 0, tzinfo=datetime.timezone.utc), 'rate': 71.6415}, {'from': datetime.datetime(2023, 2, 20, 1, 0, tzinfo=datetime.timezone.utc), 'to': datetime.datetime(2023, 2, 20, 1, 30, tzinfo=datetime.timezone.utc), 'rate': 71.6415}, {'from': datetime.datetime(2023, 2, 20, 1, 30, tzinfo=datetime.timezone.utc), 'to': datetime.datetime(2023, 2, 20, 2, 0, tzinfo=datetime.timezone.utc), 'rate': 71.6415}, {'from': datetime.datetime(2023, 2, 20, 2, 0, tzinfo=datetime.timezone.utc), 'to': datetime.datetime(2023, 2, 20, 2, 30, tzinfo=datetime.timezone.utc), 'rate': 71.6415}, {'from': datetime.datetime(2023, 2, 20, 2, 30, tzinfo=datetime.timezone.utc), 'to': datetime.datetime(2023, 2, 20, 3, 0, tzinfo=datetime.timezone.utc), 'rate': 71.6415}, {'from': datetime.datetime(2023, 2, 20, 3, 0, tzinfo=datetime.timezone.utc), 'to': datetime.datetime(2023, 2, 20, 3, 30, tzinfo=datetime.timezone.utc), 'rate': 71.6415}, {'from': datetime.datetime(2023, 2, 20, 3, 30, tzinfo=datetime.timezone.utc), 'to': datetime.datetime(2023, 2, 20, 4, 0, tzinfo=datetime.timezone.utc), 'rate': 71.6415}, {'from': datetime.datetime(2023, 2, 20, 4, 0, tzinfo=datetime.timezone.utc), 'to': datetime.datetime(2023, 2, 20, 4, 30, tzinfo=datetime.timezone.utc), 'rate': 71.6415}, {'from': datetime.datetime(2023, 2, 20, 4, 30, tzinfo=datetime.timezone.utc), 'to': datetime.datetime(2023, 2, 20, 5, 0, tzinfo=datetime.timezone.utc), 'rate': 71.6415}, {'from': datetime.datetime(2023, 2, 20, 5, 0, tzinfo=datetime.timezone.utc), 'to': datetime.datetime(2023, 2, 20, 5, 30, tzinfo=datetime.timezone.utc), 'rate': 71.6415}, {'from': datetime.datetime(2023, 2, 20, 5, 30, tzinfo=datetime.timezone.utc), 'to': datetime.datetime(2023, 2, 20, 6, 0, tzinfo=datetime.timezone.utc), 'rate': 71.6415}, {'from': datetime.datetime(2023, 2, 20, 6, 0, tzinfo=datetime.timezone.utc), 'to': datetime.datetime(2023, 2, 20, 6, 30, tzinfo=datetime.timezone.utc), 'rate': 71.6415}, {'from': datetime.datetime(2023, 2, 20, 6, 30, tzinfo=datetime.timezone.utc), 'to': datetime.datetime(2023, 2, 20, 7, 0, tzinfo=datetime.timezone.utc), 'rate': 71.6415}, {'from': datetime.datetime(2023, 2, 20, 7, 0, tzinfo=datetime.timezone.utc), 'to': datetime.datetime(2023, 2, 20, 7, 30, tzinfo=datetime.timezone.utc), 'rate': 71.6415}, {'from': datetime.datetime(2023, 2, 20, 7, 30, tzinfo=datetime.timezone.utc), 'to': datetime.datetime(2023, 2, 20, 8, 0, tzinfo=datetime.timezone.utc), 'rate': 71.6415}, {'from': datetime.datetime(2023, 2, 20, 8, 0, tzinfo=datetime.timezone.utc), 'to': datetime.datetime(2023, 2, 20, 8, 30, tzinfo=datetime.timezone.utc), 'rate': 71.6415}, {'from': datetime.datetime(2023, 2, 20, 8, 30, tzinfo=datetime.timezone.utc), 'to': datetime.datetime(2023, 2, 20, 9, 0, tzinfo=datetime.timezone.utc), 'rate': 71.6415}, {'from': datetime.datetime(2023, 2, 20, 9, 0, tzinfo=datetime.timezone.utc), 'to': datetime.datetime(2023, 2, 20, 9, 30, tzinfo=datetime.timezone.utc), 'rate': 71.6415}, {'from': datetime.datetime(2023, 2, 20, 9, 30, tzinfo=datetime.timezone.utc), 'to': datetime.datetime(2023, 2, 20, 10, 0, tzinfo=datetime.timezone.utc), 'rate': 71.6415}, {'from': datetime.datetime(2023, 2, 20, 10, 0, tzinfo=datetime.timezone.utc), 'to': datetime.datetime(2023, 2, 20, 10, 30, tzinfo=datetime.timezone.utc), 'rate': 71.6415}, {'from': datetime.datetime(2023, 2, 20, 10, 30, tzinfo=datetime.timezone.utc), 'to': datetime.datetime(2023, 2, 20, 11, 0, tzinfo=datetime.timezone.utc), 'rate': 71.6415}, {'from': datetime.datetime(2023, 2, 20, 11, 0, tzinfo=datetime.timezone.utc), 'to': datetime.datetime(2023, 2, 20, 11, 30, tzinfo=datetime.timezone.utc), 'rate': 71.6415}, {'from': datetime.datetime(2023, 2, 20, 11, 30, tzinfo=datetime.timezone.utc), 'to': datetime.datetime(2023, 2, 20, 12, 0, tzinfo=datetime.timezone.utc), 'rate': 71.6415}, {'from': datetime.datetime(2023, 2, 20, 12, 0, tzinfo=datetime.timezone.utc), 'to': datetime.datetime(2023, 2, 20, 12, 30, tzinfo=datetime.timezone.utc), 'rate': 71.6415}, {'from': datetime.datetime(2023, 2, 20, 12, 30, tzinfo=datetime.timezone.utc), 'to': datetime.datetime(2023, 2, 20, 13, 0, tzinfo=datetime.timezone.utc), 'rate': 71.6415}, {'from': datetime.datetime(2023, 2, 20, 13, 0, tzinfo=datetime.timezone.utc), 'to': datetime.datetime(2023, 2, 20, 13, 30, tzinfo=datetime.timezone.utc), 'rate': 71.6415}, {'from': datetime.datetime(2023, 2, 20, 13, 30, tzinfo=datetime.timezone.utc), 'to': datetime.datetime(2023, 2, 20, 14, 0, tzinfo=datetime.timezone.utc), 'rate': 71.6415}, {'from': datetime.datetime(2023, 2, 20, 14, 0, tzinfo=datetime.timezone.utc), 'to': datetime.datetime(2023, 2, 20, 14, 30, tzinfo=datetime.timezone.utc), 'rate': 71.6415}, {'from': datetime.datetime(2023, 2, 20, 14, 30, tzinfo=datetime.timezone.utc), 'to': datetime.datetime(2023, 2, 20, 15, 0, tzinfo=datetime.timezone.utc), 'rate': 71.6415}, {'from': datetime.datetime(2023, 2, 20, 15, 0, tzinfo=datetime.timezone.utc), 'to': datetime.datetime(2023, 2, 20, 15, 30, tzinfo=datetime.timezone.utc), 'rate': 71.6415}, {'from': datetime.datetime(2023, 2, 20, 15, 30, tzinfo=datetime.timezone.utc), 'to': datetime.datetime(2023, 2, 20, 16, 0, tzinfo=datetime.timezone.utc), 'rate': 71.6415}, {'from': datetime.datetime(2023, 2, 20, 16, 0, tzinfo=datetime.timezone.utc), 'to': datetime.datetime(2023, 2, 20, 16, 30, tzinfo=datetime.timezone.utc), 'rate': 71.6415}, {'from': datetime.datetime(2023, 2, 20, 16, 30, tzinfo=datetime.timezone.utc), 'to': datetime.datetime(2023, 2, 20, 17, 0, tzinfo=datetime.timezone.utc), 'rate': 71.6415}, {'from': datetime.datetime(2023, 2, 20, 17, 0, tzinfo=datetime.timezone.utc), 'to': datetime.datetime(2023, 2, 20, 17, 30, tzinfo=datetime.timezone.utc), 'rate': 71.6415}, {'from': datetime.datetime(2023, 2, 20, 17, 30, tzinfo=datetime.timezone.utc), 'to': datetime.datetime(2023, 2, 20, 18, 0, tzinfo=datetime.timezone.utc), 'rate': 71.6415}, {'from': datetime.datetime(2023, 2, 20, 18, 0, tzinfo=datetime.timezone.utc), 'to': datetime.datetime(2023, 2, 20, 18, 30, tzinfo=datetime.timezone.utc), 'rate': 71.6415}, {'from': datetime.datetime(2023, 2, 20, 18, 30, tzinfo=datetime.timezone.utc), 'to': datetime.datetime(2023, 2, 20, 19, 0, tzinfo=datetime.timezone.utc), 'rate': 71.6415}, {'from': datetime.datetime(2023, 2, 20, 19, 0, tzinfo=datetime.timezone.utc), 'to': datetime.datetime(2023, 2, 20, 19, 30, tzinfo=datetime.timezone.utc), 'rate': 71.6415}, {'from': datetime.datetime(2023, 2, 20, 19, 30, tzinfo=datetime.timezone.utc), 'to': datetime.datetime(2023, 2, 20, 20, 0, tzinfo=datetime.timezone.utc), 'rate': 71.6415}, {'from': datetime.datetime(2023, 2, 20, 20, 0, tzinfo=datetime.timezone.utc), 'to': datetime.datetime(2023, 2, 20, 20, 30, tzinfo=datetime.timezone.utc), 'rate': 71.6415}, {'from': datetime.datetime(2023, 2, 20, 20, 30, tzinfo=datetime.timezone.utc), 'to': datetime.datetime(2023, 2, 20, 21, 0, tzinfo=datetime.timezone.utc), 'rate': 71.6415}, {'from': datetime.datetime(2023, 2, 20, 21, 0, tzinfo=datetime.timezone.utc), 'to': datetime.datetime(2023, 2, 20, 21, 30, tzinfo=datetime.timezone.utc), 'rate': 71.6415}, {'from': datetime.datetime(2023, 2, 20, 21, 30, tzinfo=datetime.timezone.utc), 'to': datetime.datetime(2023, 2, 20, 22, 0, tzinfo=datetime.timezone.utc), 'rate': 71.6415}, {'from': datetime.datetime(2023, 2, 20, 22, 0, tzinfo=datetime.timezone.utc), 'to': datetime.datetime(2023, 2, 20, 22, 30, tzinfo=datetime.timezone.utc), 'rate': 71.6415}, {'from': datetime.datetime(2023, 2, 20, 22, 30, tzinfo=datetime.timezone.utc), 'to': datetime.datetime(2023, 2, 20, 23, 0, tzinfo=datetime.timezone.utc), 'rate': 71.6415}, {'from': datetime.datetime(2023, 2, 20, 23, 0, tzinfo=datetime.timezone.utc), 'to': datetime.datetime(2023, 2, 20, 23, 30, tzinfo=datetime.timezone.utc), 'rate': 71.6415}, {'from': datetime.datetime(2023, 2, 20, 23, 30, tzinfo=datetime.timezone.utc), 'to': datetime.datetime(2023, 2, 21, 0, 0, tzinfo=datetime.timezone.utc), 'rate': 71.6415}]

I think this might be a clue that I’m not transforming my required date/time correctly?

This seems to be an empty list

? why did you change your code from what you originally entered? I cannot help if you start changing on the fly and stating it does not work

1 Like

Sorry, I just changed it to do some testing/get some more information.

The result of adding list filter before first is:

UndefinedError: No first item, sequence was empty.

So, the output above that you copied is not (!) the same as with with you started off from
You do see that there is a difference between
[{‘from’: datetime.datetime(2023, 2, 19, 0,
and
from: ‘2023-02-20T21:00:00+00:00’

1 Like

Yes, I absolutely do, that’s why I shared it.

The initial output is the whole yaml attributes from the sensor under Developer settings/States for the sensor.

The thing I pasted subsequently is the output of the template/filter that I shared.

It appears the way the state attributes are presented in the UI not the same way it is seen by the template filters?

I have no issues with that , copied your code and only changed my sensor name.
The templating should not change the datetime to what you sent so sorry to ask…are you really sure that the sensor is correct and has the right attributes?

Here’s a screenshot of what I see under Developer Tools/States

Is there something else I should be using? :slightly_smiling_face:

And screenshot of devtools > template when you the latest code?
I donot want to sound patronizing but it is not uncommon to miss the point and from a distance, I am asking until I too (may ) fail :slight_smile:

1 Like

Not at all, very grateful for your help

Will get the screenshot now

{% 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_14e0453806_1900013341923_current_rate', 'rates')
          | selectattr('from', 'equalto', (ts_now | timestamp_custom ('%Y-%m-%dT%H:%M:%S+00:00', local=false)) )
          | map(attribute='rate') | list | first}}

(I am no longer going to worry about hiding my meter’s serial number).