Petro31 / easy-time-jinja

Easy Time calculations for Home Assistant templates
MIT License
68 stars 25 forks source link

several issues using sensor.next_alarm_timestamp #17

Closed Mariusthvdb closed 9 months ago

Mariusthvdb commented 1 year ago

as followup from https://discord.com/channels/330944238910963714/672223497736421388/1129046663105892403

using the main source timestamp template for various other template sensors:

      - unique_id: next_alarm_timestamp
        state: >
          {% macro getalarm(offsetday=0) %}
          {% set day = (now().weekday() + offsetday) % 7 %}
          {% set offset = offsetday * 86400 %}
          {% if day in range(5) %}
            {% set hour = state_attr('input_datetime.alarmclock_wd','hour') %}
            {% set minute = state_attr('input_datetime.alarmclock_wd','minute') %}
          {% else %}
            {% set hour = state_attr('input_datetime.alarmclock_we','hour') %}
            {% set minute = state_attr('input_datetime.alarmclock_we','minute') %}
          {% endif %}
          {% set alarm = hour|float|multiply(3600) + minute|float|multiply(60) %}
          {% set time = now().hour * 3600 + now().minute * 60 %}
          {{(offset - time) + alarm if offsetday else alarm - time}}
          {% endmacro %}

          {% set weekday = is_state('input_boolean.alarmclock_wd_enabled','on') %}
          {% set weekend = is_state('input_boolean.alarmclock_we_enabled','on') %}
          {% set weekdays = [0,1,2,3,4] %}
          {% set weekends = [5,6] %}
          {% set day = now().weekday() %}
          {% set nextday = day + 1 if day != 6 else 0 %}
          {% if nextday in weekdays %}
            {% if weekday %}
              {% set offsetday = nextday %}
            {% elif weekend %}
              {% set offsetday = weekends[0] %}
            {% else %}
              {% set offsetday = None %}
            {% endif %}
          {% elif nextday in weekends %}
            {% if weekend %}
              {% set offsetday = nextday %}
            {% elif weekday %}
              {% set offsetday = weekdays[0] %}
            {% else %}
              {% set offsetday = None %}
            {% endif %}
          {% else %}
            {% set offsetday = None %}
          {% endif %}

          {% if offsetday != None %}
            {% set offset = offsetday-day if offsetday > day else offsetday - day + 7 %}
            {% if (day in weekdays and weekday) or (day in weekends and weekend) %}
              {% set today_alarm = getalarm()|float %}
            {% else %}
              {% set today_alarm = -1 %}
            {% endif %}
            {% set next_alarm = getalarm(offset)|float %}
            {% set time = now().replace(second=0).replace(microsecond=0) %}
            {% if today_alarm > 0 %}
              {{as_timestamp(time) + today_alarm}}
            {% else %}
              {{as_timestamp(time) + next_alarm}}
            {% endif %}
          {% else %}
            0
          {% endif %}

and eg datetime sensor:

      - unique_id: next_alarm_datetime
        state: >
          {% set timestamp = states('sensor.next_alarm_timestamp')|int(default=0) %}
          {{as_datetime(timestamp)|as_local}}
        device_class: timestamp
        availability: >
          {% set timestamp = states('sensor.next_alarm_timestamp')|int(default=0) %}
          {{timestamp != 0}}

several the easy_time sensors produce unexpected results:

{% from 'easy_time.jinja' import speak_the_days %}
{{speak_the_days('sensor.next_alarm_datetime')}}

returns 'in 1 day', and you've posted it should be 'tomorrow'

{{states('sensor.next_alarm_timestamp')}} returns 1689309000.0

{{states('sensor.next_alarm_datetime')}} returns 2023-07-14 06:30:00+02:00

I also noted that because the main source sensors sets 0 when no alarm is set, the sensors using easy_time throw an error in that case. Hope this suffices for you to check functionality, if not please let me know

Petro31 commented 9 months ago

2.0.0