Datetime in array (templating - Jinja)

So, I’ve made a template sensor which is supposed to adjust energy prices from the Nordpool exchange (adds some price info from my energy company). However, it won’t return a list of dictionaries if I don’t convert the datetime object to a string. The original sensor it is reading from is built the same way, but is able to have datetime objects as values in the dictionaries.

Working code where the datetime is converted to string:

            {% set chargingpricehourly=state_attr('sensor.nordpool_kwh_trheim_nok_2_095_025', 'raw_today') %}
            {% if chargingpricehourly | length == 0 %}
              {{ state_attr('sensor.nordpool_kwh_trheim_nok_2_095_025', 'raw_today') }}
            {% else %}
              {% if now().month in [1, 2, 3] %} 
                {% set price_day = 39.89/100 %}
                {% set price_night = 26.51/100 %}
              {% else %}
                {% set price_day = 48.55/100 %}
                {% set price_night = 35.18/100 %}
              {% endif %}
              
              {% set data = namespace(chargingpricehourly=[]) %}
              {% for item in chargingpricehourly %}
              {% if item.end <= item.end.replace(hour=6, minute=0, second=0) or item.start >= item.end.replace(hour=22, minute=0, second=0) %}  
              {% set customer = ({"start":item.start.isoformat(),"end": item.end.isoformat(),"value":item.value + price_night }) %}
              {% set data.chargingpricehourly=data.chargingpricehourly + [customer] %}
              {% else %}
              {% set customer = ({"start":item.start.isoformat(),"end": item.end.isoformat(),"value":item.value + price_day }) %}
              {% set data.chargingpricehourly=data.chargingpricehourly + [customer] %}
              {% endif %}
              {% endfor %}
              {{ data.chargingpricehourly }}
            {% endif %}

The following just returns a string:

            {% set chargingpricehourly=state_attr('sensor.nordpool_kwh_trheim_nok_2_095_025', 'raw_today') %}
            {% if chargingpricehourly | length == 0 %}
              {{ state_attr('sensor.nordpool_kwh_trheim_nok_2_095_025', 'raw_today') }}
            {% else %}
              {% if now().month in [1, 2, 3] %} 
                {% set price_day = 39.89/100 %}
                {% set price_night = 26.51/100 %}
              {% else %}
                {% set price_day = 48.55/100 %}
                {% set price_night = 35.18/100 %}
              {% endif %}
              
              {% set data = namespace(chargingpricehourly=[]) %}
              {% for item in chargingpricehourly %}
              {% if item.end <= item.end.replace(hour=6, minute=0, second=0) or item.start >= item.end.replace(hour=22, minute=0, second=0) %}  
              {% set customer = ({"start":item.start,"end": item.end,"value":item.value + price_night }) %}
              {% set data.chargingpricehourly=data.chargingpricehourly + [customer] %}
              {% else %}
              {% set customer = ({"start":item.start,"end": item.end,"value":item.value + price_day }) %}
              {% set data.chargingpricehourly=data.chargingpricehourly + [customer] %}
              {% endif %}
              {% endfor %}
              {{ data.chargingpricehourly }}
            {% endif %}
type or paste code here