I need to be able to determine the number of minutes between sunrise and sunset each day for a fish feeding project.
The plan is to determine the number of daylight minutes and then calculate the feed interavals and amounts.
I need the calculations to be done on an ESP32 running ESPHome as I need it to be a standalone device, independent from WiFi and internet just in case any connection issue arises.
I found this website, which shows what I would need if using HA. I need to find a way to do the same in ESPHome.
Four sensors are created:
nextsunrise:
friendly_name: 'sunrise'
value_template: >
{{ as_timestamp(states.sun.sun.attributes.next_rising) | timestamp_custom('%I:%M%p') | replace(" 0", "") }}
icon_template: mdi:weather-sunset-up
nextsunset:
friendly_name: 'sunset'
value_template: >
{{ as_timestamp(states.sun.sun.attributes.next_setting) | timestamp_custom(' %I:%M%p') | replace(" 0", "") }}
icon_template: mdi:weather-sunset-down
sunhours:
friendly_name: 'sun hours'
value_template: >
{% set nrise = as_timestamp(state_attr('sun.sun','next_rising')) %}
{% set nset = as_timestamp(state_attr('sun.sun','next_setting')) %}
{% if nrise > nset %}
{% set nrise = nrise - 606024 %}
{% endif %}
{{ (nset - nrise)|timestamp_custom('%H:%M',false) }}
icon_template: mdi:weather-sunny
sunminutes:
friendly_name: 'sun mins'
value_template: >
{{ (states.sensor.sunhours.state.split(':')[0]|int * 60) + (states.sensor.sunhours.state.split(':')[1] | int) }}
icon_template: mdi:weather-sunny
In ESPHome a text sensor can be created, giving next sunrise and next sunset.
text_sensor:
- platform: sun
name: Sun Next Sunrise
type: sunrise
- platform: sun
name: Sun Next Sunset
type: sunset
I need to start by converting the text sensors to integers, I think, so that I can then use a lambda to generate the number of minutes like the YAML above does.
40 or so years ago, I learnt basic C, but it’s all but left me now. However, I could probably muddle along with the C++ lambdas if I could get going with it.
Where should I start?