###################################################################### # @CCOSTAN - Follow Me on X # For more info visit https://www.vcloudinfo.com/click-here # Original Repo : https://github.com/CCOSTAN/Home-AssistantConfig # ------------------------------------------------------------------- # Holiday Package - Flag/holiday sensors and lighting mode # Related Issue: 1774 # Centralizes holiday calendar data and the active exterior lighting mode. # ------------------------------------------------------------------- # Notes: /local/json_data is served from config/www/json_data and drives lighting mode coverage. ###################################################################### # Video breakdown: https://www.vcloudinfo.com/2019/02/breaking-down-the-flag-sensor-in-home-assistant.html # Modified for my own fun stuff! homeassistant: customize: sensor.holiday: icon: mdi:beach friendly_name: US Holiday sensor.flag: icon: mdi:flag friendly_name: Flag Day ############################################################################### # Sensor updates once every 4 hours (14400 seconds) & runs 6 times in 24 hours # # First it checks for holiday in static section, if that doesn't exist, # it checks in the dynamic section. If neither exists, the value will be empty. ############################################################################### sensor: - platform: rest resource: http://localhost:8123/local/json_data/holidays.json name: Holiday scan_interval: 14400 value_template: > {% set holiday_data = value_json.MAJOR_US if value_json is defined and value_json.MAJOR_US is defined else {} %} {% set static_days = holiday_data.static if holiday_data.static is defined else {} %} {% set dynamic_days = holiday_data.dynamic if holiday_data.dynamic is defined else {} %} {% set today = now().month ~ '/' ~ now().day %} {% set today_full = now().strftime('%m/%d/%Y') %} {% set today_full_alt = now().month ~ '/' ~ now().day ~ '/' ~ now().year %} {% if today in static_days %} {{ static_days[today] }} {% elif today_full in dynamic_days %} {{ dynamic_days[today_full] }} {% elif today_full_alt in dynamic_days %} {{ dynamic_days[today_full_alt] }} {% endif %} json_attributes: - MAJOR_US ################################################################################ # Sensor Uses Flag data generated by AI ################################################################################ - platform: rest resource: http://localhost:8123/local/json_data/flag_days.json name: Flag scan_interval: 14400 value_template: >- {% set now_string = now().month ~ '/' ~ now().day %} {% set now_full_string = now().strftime('%m/%d/%Y') %} {% set now_full_alt_string = now().month ~ '/' ~ now().day ~ '/' ~ now().year %} {% set flag_data = value_json.Flag_Days_US if value_json is defined and value_json.Flag_Days_US is defined else {} %} {% set static_days = flag_data.static if flag_data.static is defined else {} %} {% set dynamic_days = flag_data.dynamic if flag_data.dynamic is defined else {} %} {% if now_string in static_days %} True {% elif now_full_string in dynamic_days or now_full_alt_string in dynamic_days %} True {% else %} False {% endif %} json_attributes: - Flag_Days_US ################################################################################ # Countdown Sensor using WolfRam Alpha Natural language queries ################################################################################ - platform: rest name: Halloween Countdown resource: !secret wolframalpha_halloween_api value_template: "{{ (value|replace(' days', '')) | int }}" unit_of_measurement: Days scan_interval: 43200 - platform: rest name: Christmas Countdown resource: !secret wolframalpha_xmas_api value_template: "{{ (value|replace(' days', '')) | int }}" unit_of_measurement: Days scan_interval: 43200 - platform: rest name: Easter Countdown resource: !secret wolframalpha_easter_api value_template: "{{ (value|replace(' days', '')) | int }}" unit_of_measurement: Days scan_interval: 43200 - platform: rest name: Mothers Countdown resource: !secret wolframalpha_mothersday_api value_template: "{{ (value|replace(' days', '')) | int }}" unit_of_measurement: Days scan_interval: 43200 - platform: rest name: Fathers Countdown resource: !secret wolframalpha_fathersday_api value_template: "{{ (value|replace(' days', '')) | int }}" unit_of_measurement: Days scan_interval: 43200 - platform: rest name: Chanukkah Countdown resource: !secret wolframalpha_chanukkah_api value_template: "{{ (value|replace(' days', '')) | int }}" unit_of_measurement: Days scan_interval: 43200 - platform: rest name: Labor Day Countdown resource: !secret wolframalpha_labor_api value_template: "{{ (value|replace(' days', '')) | int }}" unit_of_measurement: Days scan_interval: 43200 - platform: rest name: Memorial Day Countdown resource: !secret wolframalpha_memorial_api value_template: "{{ (value|replace(' days', '')) | int }}" unit_of_measurement: Days scan_interval: 43200 - platform: rest name: Thanksgiving Day Countdown resource: !secret wolframalpha_thanksgiving_api value_template: "{{ (value|replace(' days', '')) | int }}" unit_of_measurement: Days scan_interval: 43200 template: - sensor: - name: Holiday Lighting Mode unique_id: holiday_lighting_mode icon: mdi:string-lights state: >- {%- set today = today_at('00:00') -%} {%- set mmdd = now().strftime('%m%d') | int -%} {%- set holiday_data = state_attr('sensor.holiday', 'MAJOR_US') or {} -%} {%- set flag_data = state_attr('sensor.flag', 'Flag_Days_US') or {} -%} {%- set holiday_dynamic = holiday_data.get('dynamic', {}) if holiday_data is mapping else {} -%} {%- set flag_dynamic = flag_data.get('dynamic', {}) if flag_data is mapping else {} -%} {%- set days_to = namespace(easter=9999, mothers=9999, fathers=9999, memorial=9999, labor=9999, thanksgiving=9999) -%} {%- set mode = namespace(value='standard') -%} {%- for date_text, name in holiday_dynamic.items() -%} {%- set event_date = strptime(date_text, '%m/%d/%Y') -%} {%- set days = ((as_timestamp(event_date) - as_timestamp(today)) / 86400) | int -%} {%- if days >= 0 and name == 'Easter Sunday' and days < days_to.easter -%} {%- set days_to.easter = days -%} {%- elif days >= 0 and name == 'Mothers Day' and days < days_to.mothers -%} {%- set days_to.mothers = days -%} {%- elif days >= 0 and name == 'Fathers Day' and days < days_to.fathers -%} {%- set days_to.fathers = days -%} {%- elif days >= 0 and name == 'Thanksgiving Day' and days < days_to.thanksgiving -%} {%- set days_to.thanksgiving = days -%} {%- endif -%} {%- endfor -%} {%- for date_text, name in flag_dynamic.items() -%} {%- set event_date = strptime(date_text, '%m/%d/%Y') -%} {%- set days = ((as_timestamp(event_date) - as_timestamp(today)) / 86400) | int -%} {%- if days >= 0 and name == 'Memorial Day' and days < days_to.memorial -%} {%- set days_to.memorial = days -%} {%- elif days >= 0 and name == 'Labor Day' and days < days_to.labor -%} {%- set days_to.labor = days -%} {%- endif -%} {%- endfor -%} {%- set christmas = strptime(now().year ~ '-12-25', '%Y-%m-%d') -%} {%- set christmas_days = ((as_timestamp(christmas) - as_timestamp(today)) / 86400) | int -%} {%- if is_state('sensor.flag', 'True') -%} {%- set mode.value = 'RWB' -%} {%- elif mmdd == 101 -%} {%- set mode.value = 'new_years_day' -%} {%- elif mmdd >= 210 and mmdd <= 214 -%} {%- set mode.value = 'valentine' -%} {%- elif mmdd == 305 -%} {%- set mode.value = 'mardi_gras' -%} {%- elif mmdd == 314 -%} {%- set mode.value = 'pi' -%} {%- elif mmdd >= 315 and mmdd <= 317 -%} {%- set mode.value = 'st_patty' -%} {%- elif days_to.easter < 4 -%} {%- set mode.value = 'easter' -%} {%- elif mmdd == 504 -%} {%- set mode.value = 'starwars' -%} {%- elif mmdd == 505 -%} {%- set mode.value = 'cinco_de_mayo' -%} {%- elif days_to.mothers < 4 -%} {%- set mode.value = 'mothers_day' -%} {%- elif days_to.fathers < 4 -%} {%- set mode.value = 'fathers_day' -%} {%- elif days_to.memorial < 3 -%} {%- set mode.value = 'RWB' -%} {%- elif mmdd == 704 -%} {%- set mode.value = 'RWB' -%} {%- elif days_to.labor < 3 -%} {%- set mode.value = 'RWB' -%} {%- elif mmdd >= 1001 and mmdd <= 1031 -%} {%- set mode.value = 'halloween' -%} {%- elif mmdd == 1111 -%} {%- set mode.value = 'veterans' -%} {%- elif days_to.thanksgiving < 4 -%} {%- set mode.value = 'thanksgiving' -%} {%- elif states('sensor.chanukkah_countdown') | int(9999) <= 1 -%} {%- set mode.value = 'hanukkah' -%} {%- elif christmas_days >= 0 and christmas_days <= 25 -%} {%- set mode.value = 'christmas' -%} {%- elif mmdd == 1231 -%} {%- set mode.value = 'new_years_day' -%} {%- endif -%} {{- mode.value -}} - name: Holiday Lighting Scene unique_id: holiday_lighting_scene icon: mdi:palette state: >- {%- set mode = states('sensor.holiday_lighting_mode') | trim -%} {%- if mode in ['unknown', 'unavailable', 'none', ''] -%} scene.month_standard_colors {%- else -%} scene.month_{{ mode }}_colors {%- endif -%} attributes: mode: "{{ states('sensor.holiday_lighting_mode') }}" holiday: "{{ states('sensor.holiday') }}" flag_day: "{{ states('sensor.flag') }}" source: config/www/json_data holiday and flag calendars