homeassistant系列之另类天气玩法

折腾系列-home-assistant智能家居中枢-0x03

Posted by chaoxiaodi on June 24, 2023

前言

想要更好的玩转智能家居

天气信息可以说是必不可少的一项集成

不管是根据天气信息发送预警信息

还是根据天气调整灯光、窗帘、空调等

论坛里也已经有了不少非常厉害的天气集成插件

和风 彩云 不需要key的nmc等

这篇文章通过一种另辟蹊径的玩法接入下天气信息

实现

通过刷论坛找到了一个除nmc之外的另一个不需要key就能获取到天气数据的接口

1
2
# citykey 这个是城市代码 通过百度就能搜到一大堆
https://zhwnlapi.etouch.cn/Ecalender/api/v2/weather?citykey=101091001

收集天气信息

冒起这个念头是想到ha是支持rest接入的

本着学习下用法实现简单的接入

接口反馈的信息很多可以根据需要自己修改配置

主要的配置就三项

json_attributes_path value_template json_attributes

json_attributes_path 用来定位数据在返回信息中的位置

value_template 可以通过模版来获取/处理信息

json_attributes_path 这个配置下的数据 会作为输入 传递给 value_template

value_template 可以通过 value value_json 来接收

value_template 最后输出的值为sensor的state 不能超过255个字符

如果返回结果是 json 可以进一步使用 模版语法进行处理 参考 提醒项

唯一不足的地方是数组处理起来不太友好

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
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
# configuration.yaml
rest: !include rest.yaml

# rest.yaml
- scan_interval: 600
  resource: https://zhwnlapi.etouch.cn/Ecalender/api/v2/weather?citykey=101091001
  headers: 
    User-Agent: "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36"
  sensor:
    - name: zhwnlapi.提醒
      unique_id: zhwnlapi.future_remind
      json_attributes_path: '$'
      value_template: ""
    # anomaly
    - name: zhwnlapi.异常
      unique_id: zhwnlapi.anomaly
      json_attributes_path: '$.anomaly'
      value_template: "OK"
      json_attributes:
        - desc
        - detail
        - short_desc
    # meta
    - name: zhwnlapi.元数据
      unique_id: zhwnlapi.meta
      json_attributes_path: '$.meta'
      value_template: "OK"
      json_attributes:
        - up_date
        - up_time
        - city
        - citykey
        - upper
    # alarm
    - name: zhwnlapi.预警
      unique_id: zhwnlapi.alarm
      json_attributes_path: '$.alarm'
      value_template: "OK"
      json_attributes:
        - degree
        - city_range
        - type
        - short_title
        - details
        - location
        - short_desc
        - desc
    # forecast15
    - name: zhwnlapi.forecast15.0
      unique_id: zhwnlapi.forecast15.0
      json_attributes_path: '$.forecast15[0]'
      value_template: "OK"
      json_attributes:
        - date
        - uv_level
        - aqi
        - aqi_level
        - aqi_level_name
        - sunrise
        - sunset
        - rainIntensity
        - high
        - low
        - humidity
    - name: zhwnlapi.forecast15.1
      unique_id: zhwnlapi.forecast15.1
      json_attributes_path: '$.forecast15[1]'
      value_template: "OK"
      json_attributes:
        - date
        - uv_level
        - aqi
        - aqi_level
        - aqi_level_name
        - sunrise
        - sunset
        - rainIntensity
        - high
        - low
        - humidity
    - name: zhwnlapi.forecast15.2
      unique_id: zhwnlapi.forecast15.2
      json_attributes_path: '$.forecast15[2]'
      value_template: "OK"
      json_attributes:
        - date
        - uv_level
        - aqi
        - aqi_level
        - aqi_level_name
        - sunrise
        - sunset
        - rainIntensity
        - high
        - low
        - humidity
    - name: zhwnlapi.forecast15.3
      unique_id: zhwnlapi.forecast15.3
      json_attributes_path: '$.forecast15[3]'
      value_template: "OK"
      json_attributes:
        - date
        - uv_level
        - aqi
        - aqi_level
        - aqi_level_name
        - sunrise
        - sunset
        - rainIntensity
        - high
        - low
        - humidity
    - name: zhwnlapi.forecast15.4
      unique_id: zhwnlapi.forecast15.4
      json_attributes_path: '$.forecast15[4]'
      value_template: "OK"
      json_attributes:
        - date
        - uv_level
        - aqi
        - aqi_level
        - aqi_level_name
        - sunrise
        - sunset
        - rainIntensity
        - high
        - low
        - humidity
    - name: zhwnlapi.forecast15.5
      unique_id: zhwnlapi.forecast15.5
      json_attributes_path: '$.forecast15[5]'
      value_template: "OK"
      json_attributes:
        - date
        - uv_level
        - aqi
        - aqi_level
        - aqi_level_name
        - sunrise
        - sunset
        - rainIntensity
        - high
        - low
        - humidity
    - name: zhwnlapi.forecast15.6
      unique_id: zhwnlapi.forecast15.6
      json_attributes_path: '$.forecast15[6]'
      value_template: "OK"
      json_attributes:
        - date
        - uv_level
        - aqi
        - aqi_level
        - aqi_level_name
        - sunrise
        - sunset
        - rainIntensity
        - high
        - low
        - humidity
    - name: zhwnlapi.forecast15.7
      unique_id: zhwnlapi.forecast15.7
      json_attributes_path: '$.forecast15[7]'
      value_template: "OK"
      json_attributes:
        - date
        - uv_level
        - aqi
        - aqi_level
        - aqi_level_name
        - sunrise
        - sunset
        - rainIntensity
        - high
        - low
        - humidity
    - name: zhwnlapi.forecast15.8
      unique_id: zhwnlapi.forecast15.8
      json_attributes_path: '$.forecast15[8]'
      value_template: "OK"
      json_attributes:
        - date
        - uv_level
        - aqi
        - aqi_level
        - aqi_level_name
        - sunrise
        - sunset
        - rainIntensity
        - high
        - low
        - humidity
    - name: zhwnlapi.forecast15.9
      unique_id: zhwnlapi.forecast15.9
      json_attributes_path: '$.forecast15[9]'
      value_template: "OK"
      json_attributes:
        - date
        - uv_level
        - aqi
        - aqi_level
        - aqi_level_name
        - sunrise
        - sunset
        - rainIntensity
        - high
        - low
        - humidity
    - name: zhwnlapi.forecast15.10
      unique_id: zhwnlapi.forecast15.10
      json_attributes_path: '$.forecast15[10]'
      value_template: "OK"
      json_attributes:
        - date
        - uv_level
        - aqi
        - aqi_level
        - aqi_level_name
        - sunrise
        - sunset
        - rainIntensity
        - high
        - low
        - humidity
    - name: zhwnlapi.forecast15.11
      unique_id: zhwnlapi.forecast15.11
      json_attributes_path: '$.forecast15[11]'
      value_template: "OK"
      json_attributes:
        - date
        - uv_level
        - aqi
        - aqi_level
        - aqi_level_name
        - sunrise
        - sunset
        - rainIntensity
        - high
        - low
        - humidity
    - name: zhwnlapi.forecast15.12
      unique_id: zhwnlapi.forecast15.12
      json_attributes_path: '$.forecast15[12]'
      value_template: "OK"
      json_attributes:
        - date
        - uv_level
        - aqi
        - aqi_level
        - aqi_level_name
        - sunrise
        - sunset
        - rainIntensity
        - high
        - low
        - humidity
    - name: zhwnlapi.forecast15.13
      unique_id: zhwnlapi.forecast15.13
      json_attributes_path: '$.forecast15[13]'
      value_template: "OK"
      json_attributes:
        - date
        - uv_level
        - aqi
        - aqi_level
        - aqi_level_name
        - sunrise
        - sunset
        - rainIntensity
        - high
        - low
        - humidity
    - name: zhwnlapi.forecast15.14
      unique_id: zhwnlapi.forecast15.14
      json_attributes_path: '$.forecast15[14]'
      value_template: "OK"
      json_attributes:
        - date
        - uv_level
        - aqi
        - aqi_level
        - aqi_level_name
        - sunrise
        - sunset
        - rainIntensity
        - high
        - low
        - humidity
    # hourforecast
    - name: zhwnlapi.hourfc.0
      unique_id: zhwnlapi.hourfc.0
      json_attributes_path: '$.hourfc[0]'
      value_template: "OK"
      json_attributes:
        - time
        - wthr
        - aqi
        - aqi_level
        - aqi_level_name
        - type_desc
        - wd
        - wp
        - shidu
    - name: zhwnlapi.hourfc.1
      unique_id: zhwnlapi.hourfc.1
      json_attributes_path: '$.hourfc[1]'
      value_template: "OK"
      json_attributes:
        - time
        - wthr
        - aqi
        - aqi_level
        - aqi_level_name
        - type_desc
        - wd
        - wp
        - shidu
    - name: zhwnlapi.hourfc.2
      unique_id: zhwnlapi.hourfc.2
      json_attributes_path: '$.hourfc[2]'
      value_template: "OK"
      json_attributes:
        - time
        - wthr
        - aqi
        - aqi_level
        - aqi_level_name
        - type_desc
        - wd
        - wp
        - shidu
    - name: zhwnlapi.hourfc.3
      unique_id: zhwnlapi.hourfc.3
      json_attributes_path: '$.hourfc[3]'
      value_template: "OK"
      json_attributes:
        - time
        - wthr
        - aqi
        - aqi_level
        - aqi_level_name
        - type_desc
        - wd
        - wp
        - shidu
    - name: zhwnlapi.hourfc.4
      unique_id: zhwnlapi.hourfc.4
      json_attributes_path: '$.hourfc[4]'
      value_template: "OK"
      json_attributes:
        - time
        - wthr
        - aqi
        - aqi_level
        - aqi_level_name
        - type_desc
        - wd
        - wp
        - shidu
    - name: zhwnlapi.hourfc.5
      unique_id: zhwnlapi.hourfc.5
      json_attributes_path: '$.hourfc[5]'
      value_template: "OK"
      json_attributes:
        - time
        - wthr
        - aqi
        - aqi_level
        - aqi_level_name
        - type_desc
        - wd
        - wp
        - shidu
    - name: zhwnlapi.hourfc.6
      unique_id: zhwnlapi.hourfc.6
      json_attributes_path: '$.hourfc[6]'
      value_template: "OK"
      json_attributes:
        - time
        - wthr
        - aqi
        - aqi_level
        - aqi_level_name
        - type_desc
        - wd
        - wp
        - shidu
    - name: zhwnlapi.hourfc.7
      unique_id: zhwnlapi.hourfc.7
      json_attributes_path: '$.hourfc[7]'
      value_template: "OK"
      json_attributes:
        - time
        - wthr
        - aqi
        - aqi_level
        - aqi_level_name
        - type_desc
        - wd
        - wp
        - shidu
    - name: zhwnlapi.hourfc.8
      unique_id: zhwnlapi.hourfc.8
      json_attributes_path: '$.hourfc[8]'
      value_template: "OK"
      json_attributes:
        - time
        - wthr
        - aqi
        - aqi_level
        - aqi_level_name
        - type_desc
        - wd
        - wp
        - shidu
    - name: zhwnlapi.hourfc.9
      unique_id: zhwnlapi.hourfc.9
      json_attributes_path: '$.hourfc[9]'
      value_template: "OK"
      json_attributes:
        - time
        - wthr
        - aqi
        - aqi_level
        - aqi_level_name
        - type_desc
        - wd
        - wp
        - shidu
    - name: zhwnlapi.hourfc.10
      unique_id: zhwnlapi.hourfc.10
      json_attributes_path: '$.hourfc[10]'
      value_template: "OK"
      json_attributes:
        - time
        - wthr
        - aqi
        - aqi_level
        - aqi_level_name
        - type_desc
        - wd
        - wp
        - shidu
    - name: zhwnlapi.hourfc.11
      unique_id: zhwnlapi.hourfc.11
      json_attributes_path: '$.hourfc[11]'
      value_template: "OK"
      json_attributes:
        - time
        - wthr
        - aqi
        - aqi_level
        - aqi_level_name
        - type_desc
        - wd
        - wp
        - shidu
    - name: zhwnlapi.hourfc.12
      unique_id: zhwnlapi.hourfc.12
      json_attributes_path: '$.hourfc[12]'
      value_template: "OK"
      json_attributes:
        - time
        - wthr
        - aqi
        - aqi_level
        - aqi_level_name
        - type_desc
        - wd
        - wp
        - shidu
    - name: zhwnlapi.hourfc.13
      unique_id: zhwnlapi.hourfc.13
      json_attributes_path: '$.hourfc[13]'
      value_template: "OK"
      json_attributes:
        - time
        - wthr
        - aqi
        - aqi_level
        - aqi_level_name
        - type_desc
        - wd
        - wp
        - shidu
    - name: zhwnlapi.hourfc.14
      unique_id: zhwnlapi.hourfc.14
      json_attributes_path: '$.hourfc[14]'
      value_template: "OK"
      json_attributes:
        - time
        - wthr
        - aqi
        - aqi_level
        - aqi_level_name
        - type_desc
        - wd
        - wp
        - shidu
    - name: zhwnlapi.hourfc.15
      unique_id: zhwnlapi.hourfc.15
      json_attributes_path: '$.hourfc[15]'
      value_template: "OK"
      json_attributes:
        - time
        - wthr
        - aqi
        - aqi_level
        - aqi_level_name
        - type_desc
        - wd
        - wp
        - shidu
    - name: zhwnlapi.hourfc.16
      unique_id: zhwnlapi.hourfc.16
      json_attributes_path: '$.hourfc[16]'
      value_template: "OK"
      json_attributes:
        - time
        - wthr
        - aqi
        - aqi_level
        - aqi_level_name
        - type_desc
        - wd
        - wp
        - shidu
    - name: zhwnlapi.hourfc.17
      unique_id: zhwnlapi.hourfc.17
      json_attributes_path: '$.hourfc[17]'
      value_template: "OK"
      json_attributes:
        - time
        - wthr
        - aqi
        - aqi_level
        - aqi_level_name
        - type_desc
        - wd
        - wp
        - shidu
    - name: zhwnlapi.hourfc.18
      unique_id: zhwnlapi.hourfc.18
      json_attributes_path: '$.hourfc[18]'
      value_template: "OK"
      json_attributes:
        - time
        - wthr
        - aqi
        - aqi_level
        - aqi_level_name
        - type_desc
        - wd
        - wp
        - shidu
    - name: zhwnlapi.hourfc.19
      unique_id: zhwnlapi.hourfc.19
      json_attributes_path: '$.hourfc[19]'
      value_template: "OK"
      json_attributes:
        - time
        - wthr
        - aqi
        - aqi_level
        - aqi_level_name
        - type_desc
        - wd
        - wp
        - shidu
    - name: zhwnlapi.hourfc.20
      unique_id: zhwnlapi.hourfc.20
      json_attributes_path: '$.hourfc[20]'
      value_template: "OK"
      json_attributes:
        - time
        - wthr
        - aqi
        - aqi_level
        - aqi_level_name
        - type_desc
        - wd
        - wp
        - shidu
    - name: zhwnlapi.hourfc.21
      unique_id: zhwnlapi.hourfc.21
      json_attributes_path: '$.hourfc[21]'
      value_template: "OK"
      json_attributes:
        - time
        - wthr
        - aqi
        - aqi_level
        - aqi_level_name
        - type_desc
        - wd
        - wp
        - shidu
    - name: zhwnlapi.hourfc.22
      unique_id: zhwnlapi.hourfc.22
      json_attributes_path: '$.hourfc[22]'
      value_template: "OK"
      json_attributes:
        - time
        - wthr
        - aqi
        - aqi_level
        - aqi_level_name
        - type_desc
        - wd
        - wp
        - shidu
    - name: zhwnlapi.hourfc.23
      unique_id: zhwnlapi.hourfc.23
      json_attributes_path: '$.hourfc[23]'
      value_template: "OK"
      json_attributes:
        - time
        - wthr
        - aqi
        - aqi_level
        - aqi_level_name
        - type_desc
        - wd
        - wp
        - shidu

使用了 json_attributes 配置的最后会显示为下图效果

展示天气信息

像这样收集起来的信息

展示起来是不能通过那个dashboard的 weather-forecast 去添加的

当然了就做不到像和风、彩云看起那么高大上了

除了收集另辟蹊径 展示也另辟蹊径一下

直接在ha中集成一个其他的天气页面

这个在这个接口中也有一个返回

source.link 这个返回里的链接可以直接用到ha里

添加卡片-选择网页-填入 url就成了

那么怎么使用收集起来的数据呢

ha支持的模版语法就够用了

state_attr(实体id, 属性)

如: state_attr(‘sensor.zhwnlapi_forecast15_0’, ‘humidity’)

表示获取今天的湿度数据

下面提供一份通过模版语法使用收集起来数据的作业

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
# 添加卡片选择实体
type: entities
entities:
  - entity: sensor.zhwnlapi_yuan_shu_ju
    type: attribute
    name: 省份
    attribute: upper
  - entity: sensor.zhwnlapi_yuan_shu_ju
    type: attribute
    name: 城市
    attribute: city
  - entity: sensor.zhwnlapi_yuan_shu_ju
    type: attribute
    name: 更新时间
    attribute: up_time
  - type: divider
  - entity: sensor.zhwnlapi_ti_xing
    name: 概况
  - entity: sensor.zhwnlapi_yi_chang
    type: attribute
    name: 异常
    attribute: short_desc
  - entity: sensor.zhwnlapi_yi_chang
    type: attribute
    name: 异常
    attribute: desc
  - entity: sensor.zhwnlapi_yi_chang
    type: attribute
    name: 异常
    attribute: detail
  - type: divider
  - entity: sensor.zhwnlapi_yu_jing_2
    type: attribute
    name: 预警
    attribute: short_title
  - entity: sensor.zhwnlapi_yu_jing_2
    type: attribute
    name: 预警
    attribute: short_desc
  - entity: sensor.zhwnlapi_yu_jing_2
    type: attribute
    name: 预警
    attribute: details
  - entity: sensor.zhwnlapi_yu_jing_2
    type: attribute
    name: 预警
    attribute: desc

# 添加卡片选择markdown
type: markdown
content: |-
  {\\% set ids = [0,1,2,3,4,5,6,7,8,9,10,11,12,13,14] %}
  {\\% set forecase15 = [
  'date','uv_level','aqi','aqi_level','aqi_level_name','sunrise','sunset',
  'rainIntensity','high','low','humidity'
  ] %}
  |日期|紫外线强度|空气质量指数|空气质量级别|空气质量描述|日出|日落|降雨强度|最高温度|最低温度|湿度|
  |:----|:----|:----|:----|:----|:----|:----|:----|:----|:----|:----|
  {\\% for id in ids %}
    {\\% set sid = 'sensor.zhwnlapi_forecast15_' ~ id %}
    {\\%- for attr in forecase15 -%}
  |
    {\\%- endfor -%}
  |
  {\\% endfor %}
title: 15日天气预报


type: markdown
content: >-
  {\\% set ids = [0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23]
  %}

  {\\% set hourfc = [

  'time','wthr','aqi','aqi_level','aqi_level_name','type_desc','wd', 'wp',
  'shidu'

  ] %}

  |时间|温度|空气质量指数|空气质量级别|空气质量描述|天气|风向|风力|湿度|

  |---|---|---|---|---|---|---|---|---|

  {\\%- for id in ids %}
    {\\% set sid = 'sensor.zhwnlapi_hourfc_' ~ id %}
  |
    {\\%- for attr in hourfc -%}
  |
    {\\%- endfor -%}
  {\\% endfor%}
title: 24小时天气预报

通过实体的card推荐接入一些独立的信息

一些可以循环的信息使用markdown会比较好

同时这里也希望有大佬能指点下 为啥想用个table格式显示 没有成功

另辟蹊径的天气接入

参考

home-assistant 官网

论坛 官网

nmcweather集成

Q:594934249

—我是超小弟·一名不务专业的秃头运维—

博客:blog.chaoxiaodi.tech

github:github:chaoxiaodi

微信公众号:老骥不伏枥只是近黄昏