Newbie- help recreating webcore calculation

Hi, I’m completely new to HA so forgive me if this is an easy question - I’m just finding my feet. I’m moving over from SmartThings as it’s now really hard to do anything useful now that webcore is done.

One of my most useful pistons used to calculate a time based on a dynamic offset from sunrise/sunset. The idea being that during the year it changes how quickly it gets dark.

See this thread for context Lights turning on at sunset - #9 by blackprint24 - webCoRE - SmartThings Community

So what I would like is a toggle/switch which is set dynamically by an automation (or similar), following this logic.

IsItDark=
Time is after {addMinutes($sunset, - (2.5 * $month ** 2 - 30 * $month + 120)))
OR
Time is before (addMinutes($sunrise, (2.5 * month ** 2 - 30 * $month * 120))}

I then want to be able to use the toggle/switch to run other logic/automations from.

TIA!

Sun elevatiin will give you the same result and you can use standard values for twilight… It will change the durante n Iver the year, but will always be related to the sun elevation.

If you have the Sun integration you can take a look at the entity sun.sun for its attribute named elevation:

I have a sensor that shows the diferent phases of the day, you can easily change that to a binary sensor to have your definition if night:

template:
  - sensor:
    - name: Civil Solar day status
      unique_id: 3ce1befd-f340-4c84-ad9c-beaf400550ce
      state: >-
        {% set sun_angle = state_attr('sun.sun', 'elevation') %}
        {% if sun_angle in ['unknown','undefined','none'] %}
          unknown
        {% elif sun_angle | float(0) < -6 %}
          night
        {% elif sun_angle | float(0) < 0 %}
          twilight
        {% else %}
          day
        {% endif %}
      icon: >-
        {% if is_state("sensor.civil_solar_day_status", "night") %}
          mdi:weather-night
        {% elif is_state("sensor.civil_solar_day_status", "day") %}
          mdi:weather-sunny
        {% elif is_state("sensor.civil_solar_day_status", "twilight") %}
          {% if is_state_attr('sun.sun', 'rising', true) %}
            mdi:weather-sunset-up
          {% else %}
            mdi:weather-sunset-down
          {% endif %}
        {% else %}
          mdi:help
        {% endif %}

You want a helper (Devices and Services, Helper). Make a toggle helper which becomes an input_boolean.your_device entity. You can test or set this entity from any automation or ESPHome program.

1 Like

Taking a look at this now I can see it deserves some review… I will do that later and you will find the latest version on my github repository:

1 Like

OK thanks for the tips. I’ve found the helpers and now have a toggle… I’ve worked out how to create the automation, re-evaluate it every minute, and I can control the toggle. The only bit missing is the maths bit in the middle. How would I evaluate an expression similar to the above?

Wow thanks, a different take… I’ll definitely look into this one.