Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ### package flextime.yaml
- template:
- - sensor:
- name: "Flextime Current Period"
- unique_id: "flextime_current_period"
- state: >
- {% set ct = now() %}
- {% set hour = ct.hour %}
- {% set is_weekend = ct.weekday() >= 5 %}
- {% if not is_weekend %}
- {% if hour >= 1 and hour < 7 %}
- Super-dal
- {% elif (hour >= 7 and hour < 11) or (hour >= 17 and hour < 22) %}
- Piek
- {% else %}
- Dal
- {% endif %}
- {% else %}
- {% if (hour >= 1 and hour < 7) or (hour >= 11 and hour < 17) %}
- Super-dal
- {% else %}
- Dal
- {% endif %}
- {% endif %}
- attributes:
- period_start: >
- {% set ct = now() %}
- {% set hour = ct.hour %}
- {% set is_weekend = ct.weekday() >= 5 %}
- {% if not is_weekend %}
- {% if hour >= 17 %}17:00
- {% elif hour >= 11 %}11:00
- {% elif hour >= 7 %}07:00
- {% elif hour >= 1 %}01:00
- {% else %}22:00
- {% endif %}
- {% else %}
- {% if hour >= 17 %}17:00
- {% elif hour >= 11 %}11:00
- {% elif hour >= 7 %}07:00
- {% elif hour >= 1 %}01:00
- {% else %}17:00
- {% endif %}
- {% endif %}
- next_change: >
- {% set ct = now() %}
- {% set hour = ct.hour %}
- {% set is_weekend = ct.weekday() >= 5 %}
- {% if not is_weekend %}
- {% if hour < 1 %}01:00
- {% elif hour < 7 %}07:00
- {% elif hour < 11 %}11:00
- {% elif hour < 17 %}17:00
- {% elif hour < 22 %}22:00
- {% else %}01:00
- {% endif %}
- {% else %}
- {% if hour < 1 %}01:00
- {% elif hour < 7 %}07:00
- {% elif hour < 11 %}11:00
- {% elif hour < 17 %}17:00
- {% else %}01:00
- {% endif %}
- {% endif %}
- next_period: >
- {% set ct = now() %}
- {% set hour = ct.hour %}
- {% set is_weekend = ct.weekday() >= 5 %}
- {% if not is_weekend %}
- {% if hour < 1 %}Super-dal
- {% elif hour < 7 %}Piek
- {% elif hour < 11 %}Dal
- {% elif hour < 17 %}Piek
- {% elif hour < 22 %}Dal
- {% else %}Super-dal
- {% endif %}
- {% else %}
- {% if hour < 1 %}Super-dal
- {% elif hour < 7 %}Dal
- {% elif hour < 11 %}Super-dal
- {% elif hour < 17 %}Dal
- {% else %}Super-dal
- {% endif %}
- {% endif %}
- time_remaining: >
- {% set ct = now() %}
- {% set next_time = state_attr('sensor.flextime_current_period', 'next_change') | default('00:00', true) %}
- {% if next_time and ":" in next_time %}
- {% set time_parts = next_time.split(':') %}
- {% if time_parts | length >= 2 %}
- {% set next_hour = time_parts[0] | int %}
- {% set next_minute = time_parts[1] | int %}
- {% set next_dt = ct.replace(hour=next_hour, minute=next_minute, second=0, microsecond=0) %}
- {% if next_dt < ct %}{% set next_dt = next_dt + timedelta(days=1) %}{% endif %}
- {% set diff = next_dt - ct %}
- {% set total_seconds = diff.total_seconds() %}
- {% set hours = (total_seconds // 3600) | int %}
- {% set minutes = ((total_seconds % 3600) // 60) | int %}
- {{ "{:02d}:{:02d}".format(hours, minutes) }}
- {% else %}
- 00:00
- {% endif %}
- {% else %}
- 00:00
- {% endif %}
- - sensor:
- name: "Flextime Super Dal"
- unique_id: "b7c8d9e0-f123-4a5b-8c9d-e0f1a2b3c4d5"
- state: >
- {% set ct = now() %}
- {% set hour = ct.hour %}
- {% set is_weekend = ct.weekday() >= 5 %}
- {% if not is_weekend %}
- {{ 'on' if (hour >= 1 and hour < 7) else 'off' }}
- {% else %}
- {{ 'on' if (hour >= 1 and hour < 7) or (hour >= 11 and hour < 17) else 'off' }}
- {% endif %}
- icon: >
- {% if states('sensor.flextime_super_dal') == 'on' %}mdi:leaf-circle
- {% else %}mdi:flash-circle
- {% endif %}
- attributes:
- current_period: >
- {{ states('sensor.flextime_current_period') | default('Unknown', true) }}
- next_change_time: >
- {{ state_attr('sensor.flextime_current_period', 'next_change') | default('Unknown', true) }}
- next_change_period: >
- {{ state_attr('sensor.flextime_current_period', 'next_period') | default('Unknown', true) }}
- hours_until_next_change: >
- {{ state_attr('sensor.flextime_current_period', 'time_remaining') | default('00:00', true) }}
- time_until_next_change: >
- {% set remaining = state_attr('sensor.flextime_current_period', 'time_remaining') | default('00:00', true) %}
- {% if remaining and ":" in remaining %}
- {% set time_parts = remaining.split(':') %}
- {% if time_parts | length >= 2 %}
- {% set hours = time_parts[0] | int %}
- {% set minutes = time_parts[1] | int %}
- {% if hours > 0 and minutes > 0 %}
- {{ hours }}h {{ minutes }}m
- {% elif hours > 0 %}
- {{ hours }} hours
- {% elif minutes > 0 %}
- {{ minutes }} minutes
- {% else %}
- Less than 1 minute
- {% endif %}
- {% else %}
- Unknown
- {% endif %}
- {% else %}
- Unknown
- {% endif %}
- binary_sensor:
- - platform: template
- sensors:
- flextime_super_dal:
- friendly_name: "Flextime Super Dal"
- unique_id: "6839f6c5-ff9a-48ab-b877-fe1ab76f1679"
- value_template: "{{ is_state('sensor.flextime_super_dal', 'on') }}"
- device_class: power
- icon_template: >
- {% if is_state('binary_sensor.flextime_super_dal', 'on') %}
- mdi:leaf-circle
- {% else %}
- mdi:flash-circle
- {% endif %}
- automation:
- - alias: "Smart Flextime Notifications"
- id: smart_flextime_notifications
- description: "Sends Telegram alerts only for valid period changes"
- trigger:
- - platform: state
- entity_id: sensor.flextime_current_period
- condition:
- # Only notify if period actually changed (not just time update)
- - condition: template
- value_template: >-
- {% if not trigger.from_state or trigger.from_state.state != trigger.to_state.state %}
- true
- {% else %}
- false
- {% endif %}
- # Verify the new period matches current time rules
- - condition: template
- value_template: >-
- {% set ct = now() %}
- {% set hour = ct.hour %}
- {% set is_weekend = ct.weekday() >= 5 %}
- {% if trigger.to_state.state == 'Super-dal' %}
- {% if not is_weekend %}
- hour >= 1 and hour < 7
- {% else %}
- (hour >= 1 and hour < 7) or (hour >= 11 and hour < 17)
- {% endif %}
- {% elif trigger.to_state.state == 'Piek' %}
- not is_weekend and ((hour >= 7 and hour < 11) or (hour >= 17 and hour < 22))
- {% elif trigger.to_state.state == 'Dal' %}
- {% if not is_weekend %}
- (hour >= 11 and hour < 17) or (hour >= 22) or (hour < 1)
- {% else %}
- (hour >= 7 and hour < 11) or (hour >= 17)
- {% endif %}
- {% endif %}
- action:
- - service: notify.xxxxxxx
- data:
- title: >-
- {% if trigger.to_state.state == 'Super-dal' %}🟢 Super-dal Actief
- {% elif trigger.to_state.state == 'Piek' %}🔴 Piek Actief
- {% else %}🟡 Dal Actief
- {% endif %}
- message: >-
- {% set next_change = state_attr('sensor.flextime_current_period', 'next_change') | default('onbekend') %}
- {% set remaining = state_attr('sensor.flextime_current_period', 'time_remaining') | default('00:00') %}
- **Periode**: {{ trigger.to_state.state }}
- **Start**: {{ now().strftime('%H:%M') }}
- **Einde**: {{ next_change if next_change != 'onbekend' else 'kon niet worden bepaald' }}
- **Duur**:
- {% if remaining != '00:00' %}
- {{ remaining.split(':')[0] }}h {{ remaining.split(':')[1] }}m
- {% else %}
- onbekend
- {% endif %}
- {% if trigger.to_state.state == 'Super-dal' %}
- âš¡ Nu opladen/goedkoop verbruiken!
- {% elif trigger.to_state.state == 'Piek' %}
- âš ï¸ Hoog tarief! Verbruik beperken.
- {% else %}
- 🌙 Normaal tarief.
- {% endif %}
- mode: single
- - id: "update_flextime_super_dal_sensor_hourly"
- alias: "Update Flextime at Hourly Intervals"
- trigger:
- - platform: time_pattern
- minutes: "0"
- action:
- - service: homeassistant.update_entity
- target:
- entity_id:
- - sensor.flextime_super_dal
- - sensor.flextime_current_period
- - binary_sensor.flextime_super_dal
- - id: "update_flextime_super_dal_sensor_countdown"
- alias: "Update Flextime Countdown Every Minute"
- trigger:
- - platform: time_pattern
- minutes: "*"
- seconds: "0"
- action:
- - service: homeassistant.update_entity
- target:
- entity_id:
- - sensor.flextime_super_dal
- - sensor.flextime_current_period
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement