Temple attribute with list not created nicely

Hi All,
I am trying to save some photovoltaic production forecast from one day to another.
To achieve this I have created template sensors that store once a day existing forecast sensors in template sensors.
No problem with “Forecast.solar” forecast sensors (save_energy_estimate_fcstsolar), those are replicated exactly the same… but with “Solarcast” (save_solcast_forecast_tomorrow) it seems that the way their forecast are built is causing some troubles while replicating its attributes:

vs template:

Information are correct but the formatting is incorrect and I have hard time to use ‘period_start’ data series of the template, whereas I can with the original.

template:
	-   trigger:
        -   platform: time_pattern
            hours: 21
            minutes: 00
        sensor:
            -   unique_id: save_solcast_forecast_tomorrow
                name: "save solcast forecast tomorrow"
                state: "{{ now() }}"
                attributes:
                    forecast: "{{ state_attr('sensor.solcast_forecast_tomorrow','forecast') }}"
                    detailedForecast: "{{ state_attr('sensor.solcast_forecast_tomorrow','detailedForecast') }}"
            -   unique_id: save_energy_estimate_fcstsolar
                name: "save energy estimate fcstsolar"
                state: "{{ now() }}"
                attributes:
                    watt_hours_period: "{{ state_attr('sensor.energy_estimate_fcstsolar','watt_hours_period') }}"

Try adding a |to_json filter:

                attributes:
                    forecast: "{{ state_attr('sensor.solcast_forecast_tomorrow','forecast')|to_json }}"
                    detailedForecast: "{{ state_attr('sensor.solcast_forecast_tomorrow','detailedForecast')|to_json }}"

nice idea but didn’t make the trick:

TypeError: Object of type datetime is not JSON serializable

What about | list?

                attributes:
                    forecast: "{{ state_attr('sensor.solcast_forecast_tomorrow','forecast') | list}}"
                    detailedForecast: "{{ state_attr('sensor.solcast_forecast_tomorrow','detailedForecast') | list}}"

I tried and it actually return same-o-same, which I believe makes sense as it is already a list… instead of json.
to_json was failing because of the datetime object which it can’t interpret, as far as I can understand.
Very frustating as the data are here just strangely reported.

I have tried to support another template case with the same odd return … seems to be a bug now ?

Template query with array - Configuration - Home Assistant Community (home-assistant.io)

1 Like

seems very similar indeed

probably a bug. Though I don’t know exactly what I am doing stealling code from here and there, I tried to rebuild a json exactly the same but by calculating the value of datetime…
So here is the code if it helps anyone:

            -   unique_id: save_solcast_forecast_tmr
                name: "save solcast forecast tmr"
                state: "{{ now() }}"
                attributes:
                    forecast: >-
                            {% set d = states.sensor.solcast_forecast_tomorrow.attributes  %}
                            {% set ns = namespace(m=[]) %}
                            {% for attr in d['forecast'] %}
                                {% set p = (attr.period_start).strftime("%Y-%m-%dT%H:%M:%S%z") %}
                                {% set e = attr.pv_estimate %}
                                {% set ns.m = ns.m + [{'period_start': p, 'pv_estimate': e }] %}
                            {% endfor %}
                            {{ ns.m }}

back to square one, whereas I think I should not had to do all that and there is a bug or missing piece in HA templating for datetime values…

Hi @Taras have you looked at the other topic which I copied above, although not the exactly same it has similar odd output
Template query with array - Configuration - Home Assistant Community (home-assistant.io)

I didn’t and I’ve decided to delete my post because I don’t actually want to get involved in this topic. I was only reacting to the comment that the ability to handle time is in some way deficient.