I am trying to get a template together to set the correct weather icon (mainly for cloudy state, to add the sun or moon symbol) but I can’t get the correct template to know if it is between sunset and sunrise.
I am using the OpenWeatherMap hourly data to display the state of the next 4 hours. But if it is in the evening and it is classed as partly cloudy it always shows it with a sun, while it should be with a moon. The template I have now doesn’t work right since it still shows a sun since either of the 2 if statements is true.
{% set cond0 = state_attr('weather.openweathermap_hourly', 'forecast')[0].condition %}
{% set next_setting = as_timestamp(state_attr('sun.sun', 'next_setting')) %}
{% set next_rising = as_timestamp(state_attr('sun.sun', 'next_rising')) %}
{% set cond0_time = as_timestamp(state_attr('weather.openweathermap_hourly', 'forecast')[0].datetime) %}
{% if cond0_time > next_setting and cond0_time < next_rising %}
{% if cond0 == 'sunny' %} night {% elif cond0 == 'partlycloudy' %} night-partly-cloudy {% else %} {{ cond0 }} {% endif %}
{% else %}
{{ cond0 }}
{% endif %}
The check if cond0_time > next_setting
won’t ever be valid since as soon as the sun has set, it will be set to the next days new sunset timestamp.
Does somebody have a working template or suggestions how to resolve this?