Template help needed - convert dictionary to list so I can take first x entries

Hi,

Hope a template ninja can help me.
I’m trying to automate my powerwall overnight charging.
I want to calculate the maximum electric price that it should charge at to get enough charge.

{{ states.octopusagile.rates.attributes }}

gives me:
OrderedDict([('2020-11-06T22:30:00Z', 8.589), ('2020-11-06T22:00:00Z', 9.954), ('2020-11-06T21:30:00Z', 10.458), ('2020-11-06T21:00:00Z', 11.592), ('2020-11-06T20:30:00Z', 9.8805), ('2020-11-06T20:00:00Z', 10.017), ...... ('2020-11-06T08:30:00Z', 13.335)])

For example I want to take the first 10 of these entries, sort them and then take the 4th one.

{% set rates =  states.octopusagile.rates.attributes.values() | sort %}
{{ rates[4] }}

would give me the 4th one of the whole list, but I can’t work out how to take just the first x entries of the dictionary and sort those.

Hope this makes sense.
Hope someone can help.

Thanks,
Bruce.

Select the first x items then sort:

{% set rates =  states.octopusagile.rates.attributes.values()[0:x] | sort %}

Thank you, but…
I have already tried that, I get the error:
TypeError: 'odict_values' object is not subscriptable

I think I need to convert the ordered dictionary into a list before I select the first items, but I don’t know how.

Perhaps this works (I couldn’t work out how to do it on one line):

{% set rates =  states.octopusagile.rates.attributes.values() | list %}
{% set rates = rates[:4] | sort %}
{{ rates[3] }}

That’s brilliant thanks.
I’d swear I’d already tried that.
But I must have had something different.

Thank you very much !

1 Like

Turns out I’m not quite there, the list isn’t necessarily in order !!

{{ states.octopusagile.rates.attributes }}
gives:
OrderedDict([('2020-11-09T22:30:00Z', 8.337), ('2020-11-09T22:00:00Z', 10.6995), ('2020-11-09T21:30:00Z', 9.8805), ('2020-11-09T21:00:00Z', 11.445), ('2020-11-09T20:30:00Z', 9.2715), ('2020-11-09T20:00:00Z', 11.424), ('2020-11-09T19:30:00Z', 11.277), ('2020-11-09T19:00:00Z', 12.6525), ('2020-11-09T18:30:00Z', 25.5255), ('2020-11-09T18:00:00Z', 29.6835), ('2020-11-09T17:30:00Z', 32.991), ('2020-11-09T17:00:00Z', 32.0985), ('2020-11-09T16:30:00Z', 32.613), ('2020-11-09T16:00:00Z', 24.465), ('2020-11-09T15:30:00Z', 13.1355), ('2020-11-09T15:00:00Z', 12.075), ('2020-11-09T14:30:00Z', 11.424), ('2020-11-09T14:00:00Z', 11.424), ('2020-11-09T13:30:00Z', 11.592), ('2020-11-09T13:00:00Z', 12.9465), ('2020-11-09T12:30:00Z', 13.041), ('2020-11-09T12:00:00Z', 13.041), ('2020-11-09T11:30:00Z', 12.075), ('2020-11-09T11:00:00Z', 10.8675), ('2020-11-09T10:30:00Z', 10.5525), ('2020-11-09T10:00:00Z', 11.886), ('2020-11-09T09:30:00Z', 11.907), ('2020-11-09T09:00:00Z', 12.264), ('2020-11-09T08:30:00Z', 10.752), ('2020-11-09T08:00:00Z', 10.962), ('2020-11-09T07:30:00Z', 11.6655), ('2020-11-09T07:00:00Z', 9.2505), ('2020-11-09T06:30:00Z', 10.017), ('2020-11-09T06:00:00Z', 8.211), ('2020-11-09T05:30:00Z', 9.3975), ('2020-11-09T05:00:00Z', 7.854), ('2020-11-09T04:30:00Z', 6.762), ('2020-11-09T04:00:00Z', 6.762), ('2020-11-09T03:30:00Z', 7.917), ('2020-11-09T03:00:00Z', 8.694), ('2020-11-09T02:30:00Z', 9.66), ('2020-11-09T02:00:00Z', 10.017), ('2020-11-09T01:30:00Z', 11.109), ('2020-11-09T01:00:00Z', 12.096), ('2020-11-09T00:30:00Z', 11.13), ('2020-11-09T00:00:00Z', 10.626), ('2020-11-08T23:30:00Z', 12.075), ('2020-11-08T23:00:00Z', 12.558), ('2020-11-08T22:30:00Z', 8.4525)])

So I either need to sort it by key before I start, or I need to filter it to those that have timestamsp in the next 9 hours ?

Any thoughts?

Thanks,
Bruce.

Looks ordered on timestamp, descending to me… so oldest timestamp last. Where do you see that it is not in order?

From this dat, it looks like to get the next 9 hours, you need to use the last 18 entries.

Fair point.
Must have been getting tired last night…

So, I’d do…

{% set octopusrates = rates[-4:] | sort %}
to take the last 4 entries

then:
{{ (octopusrates[1] | float ) }}
to get the 2nd item from that list.