I have a bit sensor whose template is as below. However, it is returning as false even if I have verified the template to return true in developer tools.
Can someone help what I am doing wrong?
>-
{% set after_sunset = as_timestamp(state_attr('sun.sun', 'next_setting')) + 1800 %}
{% set before_sunrise = as_timestamp(state_attr('sun.sun', 'next_rising')) - 1800 %}
{% set now = as_timestamp(now()) %}
{{ after_sunset <= now or now <= before_sunrise }}
By definition, the attributes next_setting and next_rising are always in the future.
So after_sunset <= now is always false because either it is before today’s sunset or because it is after today’s sunset, when next_setting will be sometime tomorrow.
Also, now <= before_sunrise will always be true except for the 30 minutes before sunrise since you subtracted 1800 from next_rising.
Are you sure? Your before_bright variable will always be in the future because you’re setting it to the next time the sun rises plus 30 minutes. The means your template is equal to true if the sun is below the horizon or otherwise it is true because now is always before next time the sun rises plus 30 minutes.