Advertisement
JLindvig

DST

Apr 4th, 2021 (edited)
882
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. dst:
  2.   friendly_name: Daylight Savings Times
  3.   unique_id: f2a8791bf12449c4a67d149c609c21c4
  4.   device_class: timestamp
  5.   value_template: >-
  6.     {%- set ns = namespace(spring = none, fall = none) %}
  7.     {%- set today = strptime(states('sensor.date'), '%Y-%m-%d') %}
  8.     {%- for i in range(365) if (today + timedelta(days = i)).timetuple().tm_mon in [3, 10] and (today + timedelta(days = i)) | as_timestamp | timestamp_custom("%w") | int == 0 %}
  9.       {%- set day = (today + timedelta(days = i)).astimezone() %}
  10.       {%- if day.timetuple().tm_mon == 3 %}
  11.         {%- set ns.spring = day.replace(hour = 2) %}
  12.       {%- else %}
  13.         {%- set ns.fall = day.replace(hour = 3) %}
  14.       {%- endif %}
  15.     {%- endfor %}
  16.     {{ [ns.spring, ns.fall] | min }}
  17.   attribute_templates:
  18.     next_spring: >-
  19.       {%- set ns = namespace(spring = none) %}
  20.       {%- set today = strptime(states('sensor.date'), '%Y-%m-%d') %}
  21.       {%- for i in range(365) if (today + timedelta(days = i)).timetuple().tm_mon == 3 and (today + timedelta(days = i)) | as_timestamp | timestamp_custom("%w") | int == 0 %}
  22.         {%- set ns.spring = (today + timedelta(days = i)).astimezone().replace(hour = 2) %}
  23.       {%- endfor %}
  24.       {{ ns.spring }}
  25.     next_fall: >-
  26.       {%- set ns = namespace(fall = none) %}
  27.       {%- set today = strptime(states('sensor.date'), '%Y-%m-%d') %}
  28.       {%- for i in range(365) if (today + timedelta(days = i)).timetuple().tm_mon == 10 and (today + timedelta(days = i)) | as_timestamp | timestamp_custom("%w") | int == 0 %}
  29.         {%- set ns.fall = (today + timedelta(days = i)).astimezone().replace(hour = 3) %}
  30.       {%- endfor %}
  31.       {{ ns.fall }}
  32.     is_dst: >-
  33.       {{ now().timetuple().tm_isdst == 1 }}
  34.     dst_change_tomorrow: >-
  35.       {{ now().astimezone().tzinfo != (now() + timedelta(days = 1)).astimezone().tzinfo }}
  36.     days_to_event: "{{ ((strptime(states('sensor.dst'), '%Y-%m-%d') | as_timestamp - strptime(states('sensor.date'), '%Y-%m-%d') | as_timestamp) / 86400) | int }}"
  37.     phrase: "{{ 'lose' if states('sensor.dst') == state_attr('sensor.dst', 'next_spring') else 'gain' }} an hour"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement