AitorDB / home-assistant-sun-card

Home assistant sun card based on Google weather design
MIT License
407 stars 184 forks source link

Add total hours of sun and remaining sun hours #20

Open AitorDB opened 3 years ago

AitorDB commented 3 years ago

Add to the card 2 new elements to display the total hours of sun in the day and the remaining hours. More context: https://www.reddit.com/r/homeassistant/comments/ne2mx4/sun_card_for_home_assistant/gyh7yrs

ChristophHoltmann commented 3 years ago

I really like your sun-card.

Here is some code I found/wrote some time ago with the help of the community. Maybe it could help you:

# Sunrise/set
time_to_sun_rise_set:
  friendly_name: Sunrise/set remaining time phrase
  value_template: >-
    {% set rise_set = state_attr('sun.sun', 'next_rising') 
      if is_state('sun.sun', 'below_horizon') 
      else state_attr('sun.sun', 'next_setting') 
    %}
    {% set rise_set_time = as_timestamp(rise_set) - now().timestamp() 
      if rise_set != None
      else None
    %}

    {%- macro get_time_string(tstring, number) %}
      {%- if number > 0 %}
      {%- set ret = '{} {} ' if number == 1 else '{} {}s ' %}
        {{- ret.format(number, tstring) }}
      {%- else %}
        {{- '' }}
      {%- endif %}
    {%- endmacro %}
    {%- macro get_phrase(time) %}
      {%- if time != None %}
        {%- set minutes = get_time_string('minute', ((time % 3600) / 60) | int ) %}
        {%- set hours = get_time_string('hour', ((time % 86400) / 3600) | int ) %}
        {%- set days = get_time_string('day', (time / 86400) | int ) %}
        {%- set ret = days + hours + minutes %}
        {{- ret.strip() }}
      {%- endif %}
    {%- endmacro %}

    {{ get_phrase(rise_set_time) }}
AitorDB commented 3 years ago

Thank you so much! Sadly the code in this card it's a bit different to what you would usually do with yaml, but good to see other solutions the community is using!

AitorDB commented 3 years ago

Currently on progress here https://github.com/AitorDB/home-assistant-sun-card/pull/36