How to sort dictionary by value within template.yaml?

My goal is, to sort a dictionary via the template editor in “templates.yaml”, but I can not figure out how to solve this issue or find another solution.

I did implement a python script ‘sort_dict_energyprices’ to sort a energyprices dict by value which does work well via ‘Developer Tools|Action’.

Python script: ‘sort_dict_energyprices’

prices = data.get("prices")
order = data.get("order")
if order is None or order == "":
    sorted_dict = {k: v for k, v in sorted(prices.items(), key=lambda item: item[1])}
else:
    # reverse
    sorted_dict = {k: v for k, v in sorted(prices.items(), key=lambda item: item[1], reverse=True)}
output["prices"] = sorted_dict

Playground Action which does work with the previous python script code:

action: python_script.sort_dict_energyprices
data:
  prices: {'2024-11-25T08:00:00+01:00': [20, 4.42], '2024-11-25T09:00:00+01:00': [5, 3.188], '2024-11-25T10:00:00+01:00': [1, 2.056], '2024-11-25T11:00:00+01:00': [2, 2.403], '2024-11-25T12:00:00+01:00': [3, 2.673], '2024-11-25T13:00:00+01:00': [4, 3.083], '2024-11-25T14:00:00+01:00': [13, 3.726], '2024-11-25T15:00:00+01:00': [21, 4.968], '2024-11-25T16:00:00+01:00': [24, 5.634]}