Calculating the peak power time from two Forecast.Solar sensors

Hi!
I just set up Forecast.Solar, but my solar panels are on two different sides of the roof. As stated in the docs I configured two instances of the integration. My problem now is, that I want to combine the peak power time of both sides. My approach would be to take the time in the middle of both sensors. So if peak time 1 is 11:00 and peak time 2 is 15:00 I would say the middle is 13:00. I already tried using a simple arithmetic middle sensor, but that doesn’t work. The sensor is always Unknown.
What is the right way to do that?
Thanks in advance!

Share the entity ids of the sensors and an example of their states from Developer Tools → States.

Sure, the sensors are:

sensor.power_highest_peak_time_today
sensor.power_highest_peak_time_today_2
sensor.combined_estimated_peak_power_time_today

The first two are provided by Forcast.Solar and the last one is the one I created.
The first two currently have the values: 2023-07-05T11:00:00+00:00 and 2023-07-05T09:00:00+00:00

Ok so they are datetime objects. They have to be converted to timestamps, averaged, then converted back to a datetime. Something like this:

{{ as_datetime( ( as_timestamp(states('sensor.power_highest_peak_time_today')) + as_timestamp(states('sensor.power_highest_peak_time_today_2'))  ) / 2 ) }}

I’m not quite sure about that, because the homeassistant devtools already show the device_class as timestamp. Furthermore looking at your solution it seems that it is just an arithmtical middle, but the helper for that didn’t work. Did I miss something?

Yes I am sure. Home assistant uses the same device_class for both.

This is a datetime object: 2023-07-06 17:58:06.897186+10:00

This is a timestamp: 1688630286.897213 (seconds since the Unix epoch).

Taking the average of two timestamps will give you the middle time when converted back to a datetime object as they are just numbers of seconds and don’t have weird mod12 or mod60 counting like hours and minutes do.

I just tried it and it works!
But there is one other thing, if you could help me with that. I want to create a second sensor that contains the time until the peak power/the time since peak power. For example:

peak time: 2023-07-06 09:30:00+00:00
current time: 2023-07-06 10:43:00+0000

I tried doing so, but all my templates didn’t work. I would expect a output like: -1:13
Thanks!

Like this

{{ relative_time(states('sensor.combined_estimated_peak_power_time_today')) }}

Edit oh no. That only works for times in the past (though this is being worked on). Hang on…

Edit 2: Like this for a human readable HH:MM format:

{{ (as_timestamp(states('sensor.combined_estimated_peak_power_time_today')) - now().timestamp())|timestamp_custom('%H:%M',false) }}

Or like this for a datetime object:

{{ as_datetime(as_timestamp(states('sensor.combined_estimated_peak_power_time_today')) - now().timestamp()) }}
1 Like

It looks great, but is offset by 2 hours(probably because my timezone is GMT+2), so when it is 11:15 it says 00:15 alotugh the peak time is 9:30 not 11:30. What do I nee to change?

Change the local time option from false to true at the end of the template.

{{ (as_timestamp(states('sensor.combined_estimated_peak_power_time_today')) - now().timestamp())|timestamp_custom('%H:%M',true) }}

Still doesn’t work, the current values are:

peak time: 2023-07-06 10:30:00+00:00
now(): 2023-07-06 11:46:23.777620+02:00
template result: 01:43

I think the correct output would be -1:16?

Try this:

{{ (as_timestamp(states('sensor.combined_estimated_peak_power_time_today')|as_local) - now().timestamp())|timestamp_custom('%H:%M',false) }}

Now I get an error:
AttributeError: 'str' object has no attribute 'tzinfo'
removing |as_local resolves the error.

God I hate working with time. Try this:

{{ (as_timestamp(as_datetime(states('sensor.combined_estimated_peak_power_time_today'))|as_local) - now().timestamp())|timestamp_custom('%H:%M',false) }}

Not quite sure:

2023-07-06 09:30:00+00:00
2023-07-06 12:30:26.225601+02:00

22:59

22:59 is a quite funny result :sweat_smile:

To summarize:
For calculating the time middle of different times use the following:

{{as_datetime(( as_timestamp(states('sensor.power_highest_peak_time_today')) + as_timestamp(states('sensor.power_highest_peak_time_today_2')) ) / 2)| as_local}}

To calculate the time until:

{% if ((as_datetime(states('sensor.combined_estimated_peak_power_time_today')) - now())|string)[:6] != "-1 day"%}
{{ ((as_datetime(states('sensor.combined_estimated_peak_power_time_today')) - now())|string)[:4]}} 
{% else %}
-{{ ((now() - as_datetime(states('sensor.combined_estimated_peak_power_time_today')))|string)[:4]}}
{% endif %}

The second one is not a really elegant solution, but it works for me.
Thanks again to @tom_l !

1 Like

Glad you got it sorted.

Sorry for resurrecting.

I have the same question, but just taking the middle/average of the two peak times is not very accurate (for me).

It might be that at that point in time, it’s cloudy and then it will not be the optimal peak time.

Also, in my case, I do not have the same amount of panels in each array, so middle of the two peaks is not the optimal peak time.

Is there a way to calculate the actual peak time in these cases? The Energy Dashboard seems to be able to at least show it:

In this example, the peak time of array 1 was 09:00, array 2 is 14:00 and array 3 is 13:00.

Is there a solution for your problem? Also looking for a way to detect the maximum of the forecast solar curve with multiple solar panels. In the Energy-Dashboard the sum is already calculatet. Just want the peak time of the curve. Any ideas?