Converting sunrise/sunset time from text to integer

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?

I can’t say if this is doable in ESPHome, but for the HA side of things, you could take the very good custom_component from “pnbruckner”, called sun2. There you already have the calculated times and a lot of useful sensors to work with.

Nonetheless, this is a HA component, not an ESPHome component. But as you seem to be using a text sensor from HA, your “stand alone” method isn’t really working without HA.

Take a look here: GitHub - pnbruckner/ha-sun2: Home Assistant Sun2 Sensor

Thank you.

The text sensor is in ESPHome.

I figure I first need to do something like:

Convert first two characters of Sun Next Sunrise to integer using something like stringstream intValue(stringValue);
Multiply result by 60 to get hours in minutes.
Repeat for characters 4 and 5 to get minutes.
Add hours in minutes to minutes to get sunrise minutes.

Then repeat for Sun Next Sunset to get sunset minutes.

Subtract sunrise minutes from sunset minutes to get daylight minutes.

I just need some help with the coding up part.

Hopefully, the rest is easy!

Are all C++ functions (as they were in my day) available to lambdas?

That’s the point. If your text sensor is in ESPHome, how do you feed data to it without HA? :wink: So using a text sensor isn’t a stand alone installation on your ESPHome. But if you already feed the data from HA to ESPHome, why calculating yourself, when you already have a component that does this for you? :slight_smile: You’d just need to query the HA sensor from withinESPHome.

I very much doubt, that these complex formulas will work reliable in ESPHome, at least not without a great ammount of work and control.

And to your question, lambdas in ESPHome are C++ code that is just executed “as-is”. ESPHome doesn’t check any of that code. And if necessary you can always load any kind of C++ library into your code.

1 Like

I don’t need to constantly feed data do it. I do, however, need to periodically read data from it - but that’s not mission critical.

I just need it to be independent of Wifi, internet, API connection issues etc. so that the fish keep getting fed.

If ESPHome can’t handle it then that’s the problem answered. Just thought I’d explore the possibilities.

As you say, I can read the calculated result from HA, and that may well be sufficient.

Thank you for your help. I appreciate it.

This is my understanding, but I’m in no way an ESPHome-Pro, so that may very well be the better way! :slight_smile: What I want to say is, I have a hunch, that this will not fit, but I can neither confirm or deny! :slight_smile: I didn’t want to detain you from something. :smiley:

I’d use the HA custom component to do the math, and send this periodically to your ESP. Maybe you could set up some template sensors with special “feeding times”, that are pushed to your ESP and afterwards check for their correct upload. If something happened, you can use something like an alert or a notification to send a telegram message.

Nonetheless it wouldn’t be nice, if your fish didn’t get something to eat, so at least it should alert you, if something went sideways. :slight_smile: Would be a shame, I once lost a whole aquarium to a power shortage in the pump… Not as nice as one would think, right before your first coffee… :woozy_face:

All those day length calculations have already been done by someone else, why do them again everyday? Unless you are facing tight memory constraints, I think it might be easier to just get all the day length data into an array and then have a daily automation apply the value to a variable. And unless these are some super-sensitive, time-telling fish you could probably simplify it into a weekly average to cut the memory needed for the array by 86%.

Add a battery backed-up RTC to maintain the date and time, and the device would rarely need any input from Home Assistant.

I would probably get a value from internet or calculate it in HA and set it as input_number.
Use this in ESP-Home.
If something goes wrong the input_number will remain it’s value so tomorrow will be the same as today.
No big deal…

It’s just a few minutes per day it changes.

Thank you. That’s just the direction I needed. Implementation is another matter, however. Templates and arrays are still something of a mystery to me.

I need to be able to factor in temperature as well as daylight hours. Fish weight is another factor, though this only changes once per year and is a guestimate any way.

I already have an RTC built into the control system. The hardware is so much easier!

Ash,

NOAA has a a downloadable spreadsheet that lists all the minutes of daylight for your location once you put in the lat and long, as well as the actual equations if you decide to attack it from that direction.

Two options to look at: