-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathHomeAssistant_template_weather_dynamic.yaml
320 lines (319 loc) · 18.4 KB
/
HomeAssistant_template_weather_dynamic.yaml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
# Dynamic WeatherBoard
- trigger:
- platform: time_pattern
minutes: /5
variables:
hourlytimes: >
{# Edit and order the schedule from midnight 00:00 to 23:00 as needed - 1st handles from midnight to a certain AM time and the last will handle all others not in the if/elif's #}
{# You MUST use leading zeros on all time comparisons for 9am and earlier - results will be unpredictable if you forget to do this #}
{# Because of the way Home Assistant updates sensors, it can take up to 2 time pattern cycles to fully update #}
{# Be sure all of your hourlytimes end in :00 unless you live in a "weird" time zone that uses +/- 30 minutes then you probably have to end all hourlytimes with :30 #}
{# Do an service call in Home Assistant Developer tools with openweathermap/hourly and you can see why this is important #}
{%- set hourlytimes = [] %}
{%- if now().strftime('%H.%M') < '06.10' %} {# for waketime 06:05 #}
{%- set hourlytimes = hourlytimes + ["09:00"] %}
{%- set hourlytimes = hourlytimes + ["12:00"] %}
{%- set hourlytimes = hourlytimes + ["15:00"] %}
{%- set hourlytimes = hourlytimes + ["18:00"] %}
{%- elif now().strftime('%H.%M') < '08.10' %} {# for waketime 08:05 #}
{%- set hourlytimes = hourlytimes + ["10:00"] %}
{%- set hourlytimes = hourlytimes + ["14:00"] %}
{%- set hourlytimes = hourlytimes + ["16:00"] %}
{%- set hourlytimes = hourlytimes + ["20:00"] %}
{%- elif now().strftime('%H.%M') < '10.10' %} {# for waketime 10:05 #}
{%- set hourlytimes = hourlytimes + ["12:00"] %}
{%- set hourlytimes = hourlytimes + ["15:00"] %}
{%- set hourlytimes = hourlytimes + ["18:00"] %}
{%- set hourlytimes = hourlytimes + ["21:00"] %}
{%- elif now().strftime('%H.%M') < '12.10' %} {# for waketime 12:05 #}
{%- set hourlytimes = hourlytimes + ["14:00"] %}
{%- set hourlytimes = hourlytimes + ["16:00"] %}
{%- set hourlytimes = hourlytimes + ["19:00"] %}
{%- set hourlytimes = hourlytimes + ["22:00"] %}
{%- elif now().strftime('%H.%M') < '14.10' %} {# for waketime 14:05 #}
{%- set hourlytimes = hourlytimes + ["16:00"] %}
{%- set hourlytimes = hourlytimes + ["18:00"] %}
{%- set hourlytimes = hourlytimes + ["20:00"] %}
{%- set hourlytimes = hourlytimes + ["22:00"] %}
{%- elif now().strftime('%H.%M') < '16.10' %} {# for waketime 16:05 #}
{%- set hourlytimes = hourlytimes + ["18:00"] %}
{%- set hourlytimes = hourlytimes + ["20:00"] %}
{%- set hourlytimes = hourlytimes + ["22:00"] %}
{%- set hourlytimes = hourlytimes + ["00:00"] %}
{%- elif now().strftime('%H.%M') < '18.10' %} {# for waketime 18:05 #}
{%- set hourlytimes = hourlytimes + ["20:00"] %}
{%- set hourlytimes = hourlytimes + ["21:00"] %}
{%- set hourlytimes = hourlytimes + ["22:00"] %}
{%- set hourlytimes = hourlytimes + ["00:00"] %}
{%- else %} {# for waketimes after 19:00, will show next day's forecast #}
{%- set hourlytimes = hourlytimes + ["08:00"] %}
{%- set hourlytimes = hourlytimes + ["12:00"] %}
{%- set hourlytimes = hourlytimes + ["16:00"] %}
{%- set hourlytimes = hourlytimes + ["20:00"] %}
{%- endif %}
{{hourlytimes}}
# Here you can edit the fallback intervals in case the above fails
default_0: "2"
default_1: "4"
default_2: "6"
default_3: "8"
titles: >
{# Similar as above but basically make sure it handles the hourly report shows a different title #}
{# Handle it in the order of the 3 titles (WEATHER, TODAY, OUTLOOK are defaults #}
{# Be sure the font in your ESPHome YAML can handle the characters! ie. "WEATHR" for font_title and "TODAY MRWULK" for the font_small_title #}
{%- set titles = [] %}
{%- set titles = titles + ["WEATHER"] %}
{%- if now().strftime('%H.%M') < '00.10' %} {# for waketime 00:05 (it's TOMORROW until the sun rises #}
{%- set titles = titles + ["TOMORROW"] %}
{%- elif now().strftime('%H.%M') < '18.10' %} {# for waketimes 06:05 to 18:05 #}
{%- set titles = titles + ["TODAY"] %}
{%- else %} {# for waketimes after 18:10, will show next day's forecast #}
{%- set titles = titles + ["TOMORROW"] %}
{%- endif %}
{%- set titles = titles + ["OUTLOOK"] %}
{{titles}}
dailydays: >
{# Similar as above but handles the daily forecast/outlook (it doesn't need to show tomorrow when that's handled by the hourly #}
{%- set dailydays = [] %}
{%- if now().strftime('%H.%M') < '00.10' %} {# for waketime 00:05 (kludge fix: service still contains this day's daily forecast as tomorrow's) #}
{%- set dailydays = [2,3,4,5] %}
{%- elif now().strftime('%H.%M') < '18.10' %} {# for waketimes 06:05 to 18:05 #}
{%- set dailydays = [1,2,3,4] %}
{%- else %} {# for waketimes after 18:10, will skip showing tomorrow's forecast #}
{%- set dailydays = [2,3,4,5] %}
{%- endif %}
{{dailydays}}
daily_0: "{{ dailydays[0]|int }}"
daily_1: "{{ dailydays[1]|int }}"
daily_2: "{{ dailydays[2]|int }}"
daily_3: "{{ dailydays[3]|int }}"
# Edit how timestamps are shown
time_format: "%I:00%p" # %H:00 = 24hrs (13:00) / %I:00%p = 12hrs + AM/PM (01:00PM)
strip_zero: "0" # 0 to strip leading zero (1:00PM), empty quotes if not
action:
- service: weather.get_forecasts
data:
type: hourly
target:
entity_id: weather.openweathermap
response_variable: hourly
- service: weather.get_forecasts
data:
type: daily
target:
entity_id: weather.openweathermap
response_variable: daily
sensor:
- name: eInk WeatherBoard Data
unique_id: eink_weatherboard_data
state: "OK"
attributes:
hourly_times: "{{hourlytimes}}"
hourly_intervals: >
{%- set thishourly = 3 %} {# should match what variable is the max #}
{%- set count = namespace(this = 0) %}
{%- set counter = namespace(this = 0) %}
{%- set hours = namespace(this = []) -%}
{%- for hourlycheck in hourly["weather.openweathermap"].forecast %}
{%- if (hourlytimes[count.this]|string + ":00") in (as_datetime(hourlycheck.datetime).astimezone()|string) %}
{%- set count.this = count.this + 1 %}
{%- set hours.this = hours.this + [counter.this] %}
{%- if count.this == 1 + thishourly %}
{%- break %}
{%- endif %}
{%- endif %}
{%- set counter.this = counter.this + 1 %}
{%- endfor %}
{{hours.this}}
weather_now_title: "{{ titles[0] }}"
weather_now_temperature: >
{{ hourly["weather.openweathermap"].forecast[0].temperature | round }}
weather_now_condition: >
{%- set cond_now = hourly["weather.openweathermap"].forecast[0].condition %}
{%- if states('sun.sun') == 'below_horizon' %}
{%- if cond_now == 'sunny' %} night {% elif cond_now == 'partlycloudy' %} night-partly-cloudy {% else %} {{ cond_now }} {% endif %}
{%- else %}
{{ cond_now }}
{%- endif %}
weather_hourly_title: "{{ titles[1] }}"
weather_hourly_condition_0: >
{%- if 'hourly_intervals' not in this.attributes %}
{%- set hourlytime = (default_0 | int) %}
{%- else %}
{%- set hourlytime = ((this.attributes.hourly_intervals[0] | default(default_0)) | int) %}
{%- endif %}
{%- set cond0 = hourly["weather.openweathermap"].forecast[hourlytime].condition %}
{%- set next_setting = as_timestamp(state_attr('sun.sun', 'next_setting')) %}
{%- set next_rising = as_timestamp(state_attr('sun.sun', 'next_rising')) %}
{%- set cond0_time = as_timestamp(hourly["weather.openweathermap"].forecast[hourlytime].datetime) %}
{%- if cond0_time > next_setting and cond0_time < next_rising %}
{%- if cond0 == 'sunny' %} night {% elif cond0 == 'partlycloudy' %} night-partly-cloudy {% else %} {{ cond0 }} {% endif %}
{%- else %}
{{ cond0 }}
{%- endif %}
weather_hourly_temperature_0: >
{%- if 'hourly_intervals' not in this.attributes %}
{%- set hourlytime = (default_0 | int) %}
{%- else %}
{%- set hourlytime = ((this.attributes.hourly_intervals[0] | default(default_0)) | int) %}
{%- endif %}
{{ hourly["weather.openweathermap"].forecast[hourlytime].temperature | round }}
weather_hourly_timestamp_0: >
{%- if 'hourly_intervals' not in this.attributes %}
{%- set hourlytime = (default_0 | int) %}
{%- else %}
{%- set hourlytime = ((this.attributes.hourly_intervals[0] | default(default_0)) | int) %}
{%- endif %}
{%- set timestamp = as_timestamp(hourly["weather.openweathermap"].forecast[hourlytime].datetime) | timestamp_custom(time_format) %} {{ timestamp.lstrip(strip_zero) }}
weather_hourly_condition_1: >
{%- if 'hourly_intervals' not in this.attributes %}
{%- set hourlytime = (default_1 | int) %}
{%- else %}
{%- set hourlytime = ((this.attributes.hourly_intervals[1] | default(default_1)) | int) %}
{%- endif %}
{%- set cond0 = hourly["weather.openweathermap"].forecast[hourlytime].condition %}
{%- set next_setting = as_timestamp(state_attr('sun.sun', 'next_setting')) %}
{%- set next_rising = as_timestamp(state_attr('sun.sun', 'next_rising')) %}
{%- set cond0_time = as_timestamp(hourly["weather.openweathermap"].forecast[hourlytime].datetime) %}
{%- if cond0_time > next_setting and cond0_time < next_rising %}
{%- if cond0 == 'sunny' %} night {% elif cond0 == 'partlycloudy' %} night-partly-cloudy {% else %} {{ cond0 }} {% endif %}
{%- else %}
{{ cond0 }}
{%- endif %}
weather_hourly_temperature_1: >
{%- if 'hourly_intervals' not in this.attributes %}
{%- set hourlytime = (default_1 | int) %}
{%- else %}
{%- set hourlytime = ((this.attributes.hourly_intervals[1] | default(default_1)) | int) %}
{%- endif %}
{{ hourly["weather.openweathermap"].forecast[hourlytime].temperature | round }}
weather_hourly_timestamp_1: >
{%- if 'hourly_intervals' not in this.attributes %}
{%- set hourlytime = (default_1 | int) %}
{%- else %}
{%- set hourlytime = ((this.attributes.hourly_intervals[1] | default(default_1)) | int) %}
{%- endif %}
{%- set timestamp = as_timestamp(hourly["weather.openweathermap"].forecast[hourlytime].datetime) | timestamp_custom(time_format) %} {{ timestamp.lstrip(strip_zero) }}
weather_hourly_condition_2: >
{%- if 'hourly_intervals' not in this.attributes %}
{%- set hourlytime = (default_2 | int) %}
{%- else %}
{%- set hourlytime = ((this.attributes.hourly_intervals[2] | default(default_2)) | int) %}
{%- endif %}
{%- set cond0 = hourly["weather.openweathermap"].forecast[hourlytime].condition %}
{%- set next_setting = as_timestamp(state_attr('sun.sun', 'next_setting')) %}
{%- set next_rising = as_timestamp(state_attr('sun.sun', 'next_rising')) %}
{%- set cond0_time = as_timestamp(hourly["weather.openweathermap"].forecast[hourlytime].datetime) %}
{%- if cond0_time > next_setting and cond0_time < next_rising %}
{%- if cond0 == 'sunny' %} night {% elif cond0 == 'partlycloudy' %} night-partly-cloudy {% else %} {{ cond0 }} {% endif %}
{%- else %}
{{ cond0 }}
{%- endif %}
weather_hourly_temperature_2: >
{%- if 'hourly_intervals' not in this.attributes %}
{%- set hourlytime = (default_2 | int) %}
{%- else %}
{%- set hourlytime = ((this.attributes.hourly_intervals[2] | default(default_2)) | int) %}
{%- endif %}
{{ hourly["weather.openweathermap"].forecast[hourlytime].temperature | round }}
weather_hourly_timestamp_2: >
{%- if 'hourly_intervals' not in this.attributes %}
{%- set hourlytime = (default_2 | int) %}
{%- else %}
{%- set hourlytime = ((this.attributes.hourly_intervals[2] | default(default_2)) | int) %}
{%- endif %}
{%- set timestamp = as_timestamp(hourly["weather.openweathermap"].forecast[hourlytime].datetime) | timestamp_custom(time_format) %} {{ timestamp.lstrip(strip_zero) }}
weather_hourly_condition_3: >
{%- if 'hourly_intervals' not in this.attributes %}
{%- set hourlytime = (default_3 | int) %}
{%- else %}
{%- set hourlytime = ((this.attributes.hourly_intervals[3] | default(default_3)) | int) %}
{%- endif %}
{%- set cond0 = hourly["weather.openweathermap"].forecast[hourlytime].condition %}
{%- set next_setting = as_timestamp(state_attr('sun.sun', 'next_setting')) %}
{%- set next_rising = as_timestamp(state_attr('sun.sun', 'next_rising')) %}
{%- set cond0_time = as_timestamp(hourly["weather.openweathermap"].forecast[hourlytime].datetime) %}
{%- if cond0_time > next_setting and cond0_time < next_rising %}
{%- if cond0 == 'sunny' %} night {% elif cond0 == 'partlycloudy' %} night-partly-cloudy {% else %} {{ cond0 }} {% endif %}
{%- else %}
{{ cond0 }}
{%- endif %}
weather_hourly_temperature_3: >
{%- if 'hourly_intervals' not in this.attributes %}
{%- set hourlytime = (default_3 | int) %}
{%- else %}
{%- set hourlytime = ((this.attributes.hourly_intervals[3] | default(default_3)) | int) %}
{%- endif %}
{{ hourly["weather.openweathermap"].forecast[hourlytime].temperature | round }}
weather_hourly_timestamp_3: >
{%- if 'hourly_intervals' not in this.attributes %}
{%- set hourlytime = (default_3 | int) %}
{%- else %}
{%- set hourlytime = ((this.attributes.hourly_intervals[3] | default(default_3)) | int) %}
{%- endif %}
{%- set timestamp = as_timestamp(hourly["weather.openweathermap"].forecast[hourlytime].datetime) | timestamp_custom(time_format) %} {{ timestamp.lstrip(strip_zero) }}
weather_daily_title: "{{ titles[2] }}"
weather_daily_condition_0: >
{{ daily["weather.openweathermap"].forecast[daily_0].condition }}
weather_daily_temperature_0: >
{{ daily["weather.openweathermap"].forecast[daily_0].temperature | round }}
weather_daily_timestamp_0: >
{{ as_timestamp(daily["weather.openweathermap"].forecast[daily_0].datetime) | timestamp_custom('%a') }}
weather_daily_condition_1: >
{{ daily["weather.openweathermap"].forecast[daily_1].condition }}
weather_daily_temperature_1: >
{{ daily["weather.openweathermap"].forecast[daily_1].temperature | round }}
weather_daily_timestamp_1: >
{{ as_timestamp(daily["weather.openweathermap"].forecast[daily_1].datetime) | timestamp_custom('%a') }}
weather_daily_condition_2: >
{{ daily["weather.openweathermap"].forecast[daily_2].condition }}
weather_daily_temperature_2: >
{{ daily["weather.openweathermap"].forecast[daily_2].temperature | round }}
weather_daily_timestamp_2: >
{{ as_timestamp(daily["weather.openweathermap"].forecast[daily_2].datetime) | timestamp_custom('%a') }}
weather_daily_condition_3: >
{{ daily["weather.openweathermap"].forecast[daily_3].condition }}
weather_daily_temperature_3: >
{{ daily["weather.openweathermap"].forecast[daily_3].temperature | round }}
weather_daily_timestamp_3: >
{{ as_timestamp(daily["weather.openweathermap"].forecast[daily_3].datetime) | timestamp_custom('%a') }}
- trigger:
- platform: time_pattern
minutes: /1
variables:
waketime: >
{# Edit and order the schedule from midnight 00:00 to 23:59 as needed #}
{%- set waketimes = [] %}
{%- set waketimes = waketimes + ["00:05"] %}
{%- set waketimes = waketimes + ["06:05"] %}
{%- set waketimes = waketimes + ["08:05"] %}
{%- set waketimes = waketimes + ["10:05"] %}
{%- set waketimes = waketimes + ["12:05"] %}
{%- set waketimes = waketimes + ["14:05"] %}
{%- set waketimes = waketimes + ["16:05"] %}
{%- set waketimes = waketimes + ["18:05"] %}
{%- set waketimes = waketimes + ["21:05"] %}
{%- set waketime = namespace(next = "") %}
{%- set waketime_all = [] %}
{%- for test in waketimes %}
{%- set waketime.next = (now().strftime('%Y-%m-%d')|string) + " " + (test|string) %}
{%- if (now().strftime('%Y-%m-%d %H:%M:%S')|string|as_datetime) < (waketime.next|as_datetime) %}
{%- break %}
{%- endif %}
{%- endfor %}
{%- if (now().strftime('%Y-%m-%d %H:%M:%S')|string|as_datetime) > (waketime.next|as_datetime) %}
{%- set waketime.next = (now().strftime('%Y-%m-%d')|string) + " " + (waketimes[0]|string) %}
{%- set waketime_helper = ((as_timestamp(waketime.next|as_datetime + timedelta(days=1)) - as_timestamp(now()|as_datetime))|int(0)) %}
{%- else %}
{%- set waketime_helper = ((as_timestamp(waketime.next|as_datetime) - as_timestamp(now()|as_datetime))|int(0)) %}
{%- endif %}
{%- set waketime_all = [waketime.next] + [waketime_helper] + [waketimes] %}
{{waketime_all}}
sensor:
- name: eInk WeatherBoard Wake Times
unique_id: eink_weatherboard_wake_times
state: "{{ this.attributes.next_wake_time | default('Not Available') }}"
attributes:
wake_times: "{{ waketime[2] | default('Not Available') }}"
next_wake_time: "{{ waketime[0] | default(999999) }}"
wake_time_helper: "{{ waketime[1] | default('Not Available') }}"