Advertisement
elradix

Untitled

Jul 5th, 2025
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
YAML 9.53 KB | None | 0 0
  1. ### package flextime.yaml
  2.  
  3. template:
  4.  - sensor:
  5.     name: "Flextime Current Period"
  6.     unique_id: "flextime_current_period"
  7.     state: >
  8.      {% set ct = now() %}
  9.       {% set hour = ct.hour %}
  10.       {% set is_weekend = ct.weekday() >= 5 %}
  11.       {% if not is_weekend %}
  12.         {% if hour >= 1 and hour < 7 %}
  13.           Super-dal
  14.         {% elif (hour >= 7 and hour < 11) or (hour >= 17 and hour < 22) %}
  15.           Piek
  16.         {% else %}
  17.           Dal
  18.         {% endif %}
  19.       {% else %}
  20.         {% if (hour >= 1 and hour < 7) or (hour >= 11 and hour < 17) %}
  21.           Super-dal
  22.         {% else %}
  23.           Dal
  24.         {% endif %}
  25.       {% endif %}
  26.     attributes:
  27.       period_start: >
  28.        {% set ct = now() %}
  29.         {% set hour = ct.hour %}
  30.         {% set is_weekend = ct.weekday() >= 5 %}
  31.         {% if not is_weekend %}
  32.           {% if hour >= 17 %}17:00
  33.           {% elif hour >= 11 %}11:00
  34.           {% elif hour >= 7 %}07:00
  35.           {% elif hour >= 1 %}01:00
  36.           {% else %}22:00
  37.           {% endif %}
  38.         {% else %}
  39.           {% if hour >= 17 %}17:00
  40.           {% elif hour >= 11 %}11:00
  41.           {% elif hour >= 7 %}07:00
  42.           {% elif hour >= 1 %}01:00
  43.           {% else %}17:00
  44.           {% endif %}
  45.         {% endif %}
  46.       next_change: >
  47.        {% set ct = now() %}
  48.         {% set hour = ct.hour %}
  49.         {% set is_weekend = ct.weekday() >= 5 %}
  50.         {% if not is_weekend %}
  51.           {% if hour < 1 %}01:00
  52.           {% elif hour < 7 %}07:00
  53.           {% elif hour < 11 %}11:00
  54.           {% elif hour < 17 %}17:00
  55.           {% elif hour < 22 %}22:00
  56.           {% else %}01:00
  57.           {% endif %}
  58.         {% else %}
  59.           {% if hour < 1 %}01:00
  60.           {% elif hour < 7 %}07:00
  61.           {% elif hour < 11 %}11:00
  62.           {% elif hour < 17 %}17:00
  63.           {% else %}01:00
  64.           {% endif %}
  65.         {% endif %}
  66.       next_period: >
  67.        {% set ct = now() %}
  68.         {% set hour = ct.hour %}
  69.         {% set is_weekend = ct.weekday() >= 5 %}
  70.         {% if not is_weekend %}
  71.           {% if hour < 1 %}Super-dal
  72.           {% elif hour < 7 %}Piek
  73.           {% elif hour < 11 %}Dal
  74.           {% elif hour < 17 %}Piek
  75.           {% elif hour < 22 %}Dal
  76.           {% else %}Super-dal
  77.           {% endif %}
  78.         {% else %}
  79.           {% if hour < 1 %}Super-dal
  80.           {% elif hour < 7 %}Dal
  81.           {% elif hour < 11 %}Super-dal
  82.           {% elif hour < 17 %}Dal
  83.           {% else %}Super-dal
  84.           {% endif %}
  85.         {% endif %}
  86.       time_remaining: >
  87.        {% set ct = now() %}
  88.         {% set next_time = state_attr('sensor.flextime_current_period', 'next_change') | default('00:00', true) %}
  89.         {% if next_time and ":" in next_time %}
  90.           {% set time_parts = next_time.split(':') %}
  91.           {% if time_parts | length >= 2 %}
  92.             {% set next_hour = time_parts[0] | int %}
  93.             {% set next_minute = time_parts[1] | int %}
  94.             {% set next_dt = ct.replace(hour=next_hour, minute=next_minute, second=0, microsecond=0) %}
  95.             {% if next_dt < ct %}{% set next_dt = next_dt + timedelta(days=1) %}{% endif %}
  96.             {% set diff = next_dt - ct %}
  97.             {% set total_seconds = diff.total_seconds() %}
  98.             {% set hours = (total_seconds // 3600) | int %}
  99.             {% set minutes = ((total_seconds % 3600) // 60) | int %}
  100.             {{ "{:02d}:{:02d}".format(hours, minutes) }}
  101.           {% else %}
  102.             00:00
  103.           {% endif %}
  104.         {% else %}
  105.           00:00
  106.         {% endif %}
  107.        
  108.  
  109.  - sensor:
  110.     name: "Flextime Super Dal"
  111.     unique_id: "b7c8d9e0-f123-4a5b-8c9d-e0f1a2b3c4d5"
  112.     state: >
  113.      {% set ct = now() %}
  114.       {% set hour = ct.hour %}
  115.       {% set is_weekend = ct.weekday() >= 5 %}
  116.       {% if not is_weekend %}
  117.         {{ 'on' if (hour >= 1 and hour < 7) else 'off' }}
  118.       {% else %}
  119.         {{ 'on' if (hour >= 1 and hour < 7) or (hour >= 11 and hour < 17) else 'off' }}
  120.       {% endif %}
  121.     icon: >
  122.      {% if states('sensor.flextime_super_dal') == 'on' %}mdi:leaf-circle
  123.       {% else %}mdi:flash-circle
  124.       {% endif %}
  125.     attributes:
  126.       current_period: >
  127.        {{ states('sensor.flextime_current_period') | default('Unknown', true) }}
  128.       next_change_time: >
  129.        {{ state_attr('sensor.flextime_current_period', 'next_change') | default('Unknown', true) }}
  130.       next_change_period: >
  131.        {{ state_attr('sensor.flextime_current_period', 'next_period') | default('Unknown', true) }}
  132.       hours_until_next_change: >
  133.        {{ state_attr('sensor.flextime_current_period', 'time_remaining') | default('00:00', true) }}
  134.       time_until_next_change: >
  135.        {% set remaining = state_attr('sensor.flextime_current_period', 'time_remaining') | default('00:00', true) %}
  136.         {% if remaining and ":" in remaining %}
  137.           {% set time_parts = remaining.split(':') %}
  138.           {% if time_parts | length >= 2 %}
  139.             {% set hours = time_parts[0] | int %}
  140.             {% set minutes = time_parts[1] | int %}
  141.             {% if hours > 0 and minutes > 0 %}
  142.               {{ hours }}h {{ minutes }}m
  143.             {% elif hours > 0 %}
  144.               {{ hours }} hours
  145.             {% elif minutes > 0 %}
  146.               {{ minutes }} minutes
  147.             {% else %}
  148.               Less than 1 minute
  149.             {% endif %}
  150.           {% else %}
  151.             Unknown
  152.           {% endif %}
  153.         {% else %}
  154.           Unknown
  155.         {% endif %}
  156.  
  157. binary_sensor:
  158.   - platform: template
  159.     sensors:
  160.       flextime_super_dal:
  161.         friendly_name: "Flextime Super Dal"
  162.         unique_id: "6839f6c5-ff9a-48ab-b877-fe1ab76f1679"
  163.         value_template: "{{ is_state('sensor.flextime_super_dal', 'on') }}"
  164.         device_class: power
  165.         icon_template: >
  166.          {% if is_state('binary_sensor.flextime_super_dal', 'on') %}
  167.             mdi:leaf-circle
  168.           {% else %}
  169.             mdi:flash-circle
  170.           {% endif %}
  171.  
  172. automation:
  173.   - alias: "Smart Flextime Notifications"
  174.     id: smart_flextime_notifications
  175.     description: "Sends Telegram alerts only for valid period changes"
  176.     trigger:
  177.       - platform: state
  178.         entity_id: sensor.flextime_current_period
  179.     condition:
  180.      # Only notify if period actually changed (not just time update)
  181.       - condition: template
  182.         value_template: >-
  183.           {% if not trigger.from_state or trigger.from_state.state != trigger.to_state.state %}
  184.             true
  185.           {% else %}
  186.             false
  187.           {% endif %}
  188.       # Verify the new period matches current time rules
  189.       - condition: template
  190.         value_template: >-
  191.           {% set ct = now() %}
  192.           {% set hour = ct.hour %}
  193.           {% set is_weekend = ct.weekday() >= 5 %}
  194.           {% if trigger.to_state.state == 'Super-dal' %}
  195.             {% if not is_weekend %}
  196.               hour >= 1 and hour < 7
  197.             {% else %}
  198.               (hour >= 1 and hour < 7) or (hour >= 11 and hour < 17)
  199.             {% endif %}
  200.           {% elif trigger.to_state.state == 'Piek' %}
  201.             not is_weekend and ((hour >= 7 and hour < 11) or (hour >= 17 and hour < 22))
  202.           {% elif trigger.to_state.state == 'Dal' %}
  203.             {% if not is_weekend %}
  204.               (hour >= 11 and hour < 17) or (hour >= 22) or (hour < 1)
  205.             {% else %}
  206.               (hour >= 7 and hour < 11) or (hour >= 17)
  207.             {% endif %}
  208.           {% endif %}
  209.     action:
  210.       - service: notify.xxxxxxx
  211.         data:
  212.           title: >-
  213.             {% if trigger.to_state.state == 'Super-dal' %}🟢 Super-dal Actief
  214.             {% elif trigger.to_state.state == 'Piek' %}🔴 Piek Actief
  215.             {% else %}🟡 Dal Actief
  216.             {% endif %}
  217.           message: >-
  218.             {% set next_change = state_attr('sensor.flextime_current_period', 'next_change') | default('onbekend') %}
  219.             {% set remaining = state_attr('sensor.flextime_current_period', 'time_remaining') | default('00:00') %}
  220.            
  221.             **Periode**: {{ trigger.to_state.state }}
  222.             **Start**: {{ now().strftime('%H:%M') }}
  223.             **Einde**: {{ next_change if next_change != 'onbekend' else 'kon niet worden bepaald' }}
  224.             **Duur**:
  225.             {% if remaining != '00:00' %}
  226.               {{ remaining.split(':')[0] }}h {{ remaining.split(':')[1] }}m
  227.             {% else %}
  228.               onbekend
  229.             {% endif %}
  230.            
  231.             {% if trigger.to_state.state == 'Super-dal' %}
  232.             âš¡ Nu opladen/goedkoop verbruiken!
  233.             {% elif trigger.to_state.state == 'Piek' %}
  234.             ⚠️ Hoog tarief! Verbruik beperken.
  235.             {% else %}
  236.             🌙 Normaal tarief.
  237.             {% endif %}
  238.            
  239.     mode: single
  240.  
  241.  
  242.  
  243.   - id: "update_flextime_super_dal_sensor_hourly"
  244.     alias: "Update Flextime at Hourly Intervals"
  245.     trigger:
  246.       - platform: time_pattern
  247.         minutes: "0"
  248.     action:
  249.       - service: homeassistant.update_entity
  250.         target:
  251.           entity_id:
  252.             - sensor.flextime_super_dal
  253.             - sensor.flextime_current_period
  254.             - binary_sensor.flextime_super_dal
  255.  
  256.   - id: "update_flextime_super_dal_sensor_countdown"
  257.     alias: "Update Flextime Countdown Every Minute"
  258.     trigger:
  259.       - platform: time_pattern
  260.         minutes: "*"
  261.         seconds: "0"
  262.     action:
  263.       - service: homeassistant.update_entity
  264.         target:
  265.           entity_id:
  266.            - sensor.flextime_super_dal
  267.             - sensor.flextime_current_period
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement