『瀚思彼岸』» 智能家居技术论坛

 找回密码
 立即注册
查看: 24506|回复: 12

[进阶教程] 花粉过敏者看过来-在HA中显示当前过敏风险指数

[复制链接]

3

主题

148

帖子

3574

积分

论坛元老

Rank: 8Rank: 8

积分
3574
金钱
3421
HASS币
30
发表于 2021-3-30 14:13:11 | 显示全部楼层 |阅读模式
本帖最后由 zmg 于 2021-3-30 14:18 编辑

每年到了春暖花开,万物复苏的大好时节,对于花粉症患者(包括我本人)来说,意味着一年一度难受的日子又到来了。

花粉过敏没有特效药可治,唯有出门前做好个人防护最重要。于是我给家里的HA增加了过敏指数显示,随时了解当前的过敏指数,如果指数较高,还可通过TTS在出门前主动提醒。

过敏指数

又叫过敏风险指数,英文Allergy Risk Index,缩写ARI,是考虑气象因素并结合环境要素对人体的影响,从天气角度出发为广大公众提供是否易发生过敏的服务提示。

该指数等级划分为五级,级数越高,表示的气象条件,引发过敏的可能性越大。

  • 1级 极不易发
  • 2级 不易发
  • 3级 较易发
  • 4级 易发
  • 5级 极易发



数据来源

数据源来自和风天气,需要注册一个免费用户并获取个人api key。过敏指数属于天气生活指数之一,接口文档在这里:https://dev.qweather.com/docs/api/indices/

调用格式:https://devapi.qweather.com/v7/indices/1d?location=经度,纬度&key=api_key&type=7

举例,获取北京市当前的全部生活指数,可以用浏览器访问如下地址:

https://devapi.qweather.com/v7/indices/1d?location=116.41,39.92&key=xxxxxxxxxxxxxx&type=0

如果只想读取过敏风险指数,把链接中的type=0改成type=7即可。

返回的数据是这样的:

{"code":"200","updateTime":"2021-03-30T10:36+08:00","fxLink":"http://hfx.link/1tjo2","daily":[{"date":"2021-03-30","type":"7","name":"过敏指数","level":"4","category":"易发","text":"天气条件易诱发过敏,易过敏人群应减少外出,外出宜穿长衣长裤并佩戴好眼镜和口罩,外出归来时及时清洁手和口鼻。"}],"refer":{"sources":["Weather China"],"license":["no commercial use"]}}



格式化一下,看起来就清晰了。

Screenshot 2021-03-30 141453.png


HA获取当前过敏指数


下面要做的,就是用HA中的rest组件,每隔一段时间去访问一次上述链接,获得返回数据并解析后,把需要的数值赋给自定义sensor即可。


sensor:
  - platform: rest
    name: ari # Allergy Risk Index
    resource: https://devapi.qweather.com/v7/indices/1d?location=116.41,39.92&key=xxxxxxxxxxxxxxxx&type=7
    value_template: '{{ value_json["daily"][0]["level"].title() }}'
    scan_interval: 3600 #一小时更新一次
    json_attributes:
      - daily


可以顺便再定义几个template sensor,TTS用起来方便。

  - platform: template
    sensors:
      ari_index:
        icon_template: 'mdi:face-mask'
        friendly_name: 过敏指数
        value_template: '{{ states.sensor.ari.attributes.daily[0]["level"] }}'
      ari_category:
        friendly_name: 过敏风险
        value_template: '{{ states.sensor.ari.attributes.daily[0]["category"] }}'
      ari_text:
        friendly_name: 过敏风险详情
        value_template: '{{ states.sensor.ari.attributes.daily[0]["text"] }}'


前端显示


为了节省显示空间,我把过敏指数ari和空气质量指数aqi在一张卡片中显示,反正都是和空气质量相关的数据。这要用到HACS里面的Dual gauge card,在HACS的Frontend标签中搜索安装即可。然后定义卡片(外圈是aqi,内圈是ari):


type: 'custom:dual-gauge-card'
title: air
min: 1
max: 300
shadeInner: true
outer:
  entity: sensor.aqi
  label: AQI
  colors:
    - color: var(--label-badge-green)
      value: 1
    - color: var(--label-badge-yellow)
      value: 100
    - color: var(--label-badge-red)
      value: 200
    - color: var(--label-badge-black)
      value: 300
inner:
  entity: sensor.ari
  label: ARI
  min: 1
  max: 5
  colors:
    - color: var(--label-badge-green)
      value: 1
    - color: var(--label-badge-yellow)
      value: 3
    - color: var(--label-badge-red)
      value: 5


显示效果:


Screenshot 2021-03-30 112238.png


点击卡片中的ARI,会显示出详情:


Screenshot 2021-03-30 112311.png


今天天气还算不错,前两天沙尘暴的时候,看上去特别吓人。

IMG_21-03-30_125544191_3.jpg


TTS就简单了,ari_category、ari_text两个sensor内容都是文字信息,TTS可直接使用,此处略过。





评分

参与人数 2金钱 +40 HASS币 +20 收起 理由
+ 20 + 20 实用的技术才是最美的技术!
neroxps + 20 真正的技术改变生活

查看全部评分

回复

使用道具 举报

40

主题

3057

帖子

1万

积分

超级版主

Nero

Rank: 8Rank: 8

积分
11135
金钱
8028
HASS币
182
发表于 2021-3-30 14:33:53 | 显示全部楼层
真正的技术改变生活,楼主的场景完美提现了现代技术对生活水平的提升
Nero
回复

使用道具 举报

11

主题

313

帖子

1324

积分

金牌会员

Rank: 6Rank: 6

积分
1324
金钱
1011
HASS币
10
发表于 2021-3-30 16:14:06 | 显示全部楼层
本帖最后由 firelion 于 2021-3-30 18:31 编辑

Dual gauge card添加后为啥在添加卡片那里搜索不到啊,已经重启几次ha了
回复

使用道具 举报

11

主题

313

帖子

1324

积分

金牌会员

Rank: 6Rank: 6

积分
1324
金钱
1011
HASS币
10
发表于 2021-3-30 17:51:50 | 显示全部楼层
嘿嘿 空气质量指数也是同样的搞法么?
回复

使用道具 举报

3

主题

148

帖子

3574

积分

论坛元老

Rank: 8Rank: 8

积分
3574
金钱
3421
HASS币
30
 楼主| 发表于 2021-3-30 19:25:31 | 显示全部楼层
firelion 发表于 2021-3-30 17:51
嘿嘿 空气质量指数也是同样的搞法么?

AQI我也是同样搞法,用第三方插件也行。
回复

使用道具 举报

3

主题

148

帖子

3574

积分

论坛元老

Rank: 8Rank: 8

积分
3574
金钱
3421
HASS币
30
 楼主| 发表于 2021-3-30 19:26:47 | 显示全部楼层
firelion 发表于 2021-3-30 16:14
Dual gauge card添加后为啥在添加卡片那里搜索不到啊,已经重启几次ha了

好像是找不到,不过直接输入代码就行了。
回复

使用道具 举报

11

主题

313

帖子

1324

积分

金牌会员

Rank: 6Rank: 6

积分
1324
金钱
1011
HASS币
10
发表于 2021-3-30 21:34:37 | 显示全部楼层
试着把所有指数都分离出来了,也做点贡献吧,第一次贴代码,不知道怎么排版,凑合看吧。指数太多了,图标也懒得改了,自己可以修改下。


sensor:
  - platform: rest
    name: Weather Life Index  #天气生活指数
    resource: https://devapi.qweather.com/v7/indices/1d?location=经度,纬度&key=和风天气的key&type=0
    value_template: '点击查看详情'
    scan_interval: 3600       #更新频率:一小时更新一次,单位:秒
    json_attributes:
      - daily

#分离出各个指数
  - platform: template
    sensors:
      makeup_index:
        friendly_name: 化妆指数
        value_template: '{{ states.sensor.weather_life_index.attributes.daily[0]["level"] }}'
      makeup_category:
        friendly_name: 化妆建议
        value_template: '{{ states.sensor.weather_life_index.attributes.daily[0]["category"] }}'
      makeup_text:
        friendly_name: 化妆建议详情
        value_template: '{{ states.sensor.weather_life_index.attributes.daily[0]["text"] }}'
      uv_index:
        friendly_name: 紫外线指数
        value_template: '{{ states.sensor.weather_life_index.attributes.daily[1]["level"] }}'
      uv_category:
        friendly_name: 紫外线强度
        value_template: '{{ states.sensor.weather_life_index.attributes.daily[1]["category"] }}'
      uv_text:
        friendly_name: 防晒建议
        value_template: '{{ states.sensor.weather_life_index.attributes.daily[1]["text"] }}'
      ac_index:
        friendly_name: 空调开启指数
        value_template: '{{ states.sensor.weather_life_index.attributes.daily[2]["level"] }}'
      ac_category:
        friendly_name: 空调开启建议
        value_template: '{{ states.sensor.weather_life_index.attributes.daily[2]["category"] }}'
      ac_text:
        friendly_name: 空调开启建议详情
        value_template: '{{ states.sensor.weather_life_index.attributes.daily[2]["text"] }}'
      travel_index:
        friendly_name: 旅游指数
        value_template: '{{ states.sensor.weather_life_index.attributes.daily[3]["level"] }}'
      travel_category:
        friendly_name: 出游建议
        value_template: '{{ states.sensor.weather_life_index.attributes.daily[3]["category"] }}'
      travel_text:
        friendly_name: 出游建议详情
        value_template: '{{ states.sensor.weather_life_index.attributes.daily[3]["text"] }}'
      sunglasses_index:
        friendly_name: 太阳镜指数
        value_template: '{{ states.sensor.weather_life_index.attributes.daily[4]["level"] }}'
      sunglasses_category:
        friendly_name: 太阳镜建议
        value_template: '{{ states.sensor.weather_life_index.attributes.daily[4]["category"] }}'
      sunglasses_text:
        friendly_name: 太阳镜建议详情
        value_template: '{{ states.sensor.weather_life_index.attributes.daily[4]["text"] }}'
      ari_index:
        icon_template: 'mdi:face-mask'
        friendly_name: 过敏指数
        value_template: '{{ states.sensor.weather_life_index.attributes.daily[5]["level"] }}'
      ari_category:
        friendly_name: 过敏风险
        value_template: '{{ states.sensor.weather_life_index.attributes.daily[5]["category"] }}'
      ari_text:
        friendly_name: 过敏风险详情
        value_template: '{{ states.sensor.weather_life_index.attributes.daily[5]["text"] }}'
      aircure_index:
        friendly_name: 晾晒指数
        value_template: '{{ states.sensor.weather_life_index.attributes.daily[6]["level"] }}'
      aircure_category:
        friendly_name: 晾晒建议
        value_template: '{{ states.sensor.weather_life_index.attributes.daily[6]["category"] }}'
      aircure_text:
        friendly_name: 晾晒建议详情
        value_template: '{{ states.sensor.weather_life_index.attributes.daily[6]["text"] }}'
      traffic_index:
        friendly_name: 交通指数
        value_template: '{{ states.sensor.weather_life_index.attributes.daily[7]["level"] }}'
      traffic_category:
        friendly_name: 交通天气状况
        value_template: '{{ states.sensor.weather_life_index.attributes.daily[7]["category"] }}'
      traffic_text:
        friendly_name: 交通建议
        value_template: '{{ states.sensor.weather_life_index.attributes.daily[7]["text"] }}'
      comfort_index:
        friendly_name: 天气舒适度指数
        value_template: '{{ states.sensor.weather_life_index.attributes.daily[8]["level"] }}'
      comfort_category:
        friendly_name: 天气舒适度
        value_template: '{{ states.sensor.weather_life_index.attributes.daily[8]["category"] }}'
      comfort_text:
        friendly_name: 天气舒适度详情
        value_template: '{{ states.sensor.weather_life_index.attributes.daily[8]["text"] }}'
      airpollution_index:
        friendly_name: 空气污染扩散条件指数
        value_template: '{{ states.sensor.weather_life_index.attributes.daily[9]["level"] }}'
      airpollution_category:
        friendly_name: 空气污染扩散条件
        value_template: '{{ states.sensor.weather_life_index.attributes.daily[9]["category"] }}'
      airpollution_text:
        friendly_name: 空气污染扩散条件指数详情
        value_template: '{{ states.sensor.weather_life_index.attributes.daily[9]["text"] }}'
      dressing_index:
        friendly_name: 穿衣指数
        value_template: '{{ states.sensor.weather_life_index.attributes.daily[10]["level"] }}'
      dressing_category:
        friendly_name: 穿衣建议
        value_template: '{{ states.sensor.weather_life_index.attributes.daily[10]["category"] }}'
      dressing_text:
        friendly_name: 穿衣建议详情
        value_template: '{{ states.sensor.weather_life_index.attributes.daily[10]["text"] }}'
      spf_index:
        friendly_name: 防晒指数
        value_template: '{{ states.sensor.weather_life_index.attributes.daily[11]["level"] }}'
      spf_text:
        friendly_name: 防晒建议
        value_template: '{{ states.sensor.weather_life_index.attributes.daily[11]["text"] }}'
      flu_index:
        friendly_name: 感冒指数
        value_template: '{{ states.sensor.weather_life_index.attributes.daily[12]["level"] }}'
      flu_category:
        friendly_name: 感冒风险
        value_template: '{{ states.sensor.weather_life_index.attributes.daily[12]["category"] }}'
      flu_text:
        friendly_name: 感冒风险详情
        value_template: '{{ states.sensor.weather_life_index.attributes.daily[12]["text"] }}'
      carwash_index:
        friendly_name: 洗车指数
        value_template: '{{ states.sensor.weather_life_index.attributes.daily[13]["level"] }}'
      carwash_category:
        friendly_name: 洗车建议
        value_template: '{{ states.sensor.weather_life_index.attributes.daily[13]["category"] }}'
      carwash_text:
        friendly_name: 洗车建议详情
        value_template: '{{ states.sensor.weather_life_index.attributes.daily[13]["text"] }}'
      fishing_index:
        friendly_name: 钓鱼指数
        value_template: '{{ states.sensor.weather_life_index.attributes.daily[14]["level"] }}'
      fishing_category:
        friendly_name: 钓鱼建议
        value_template: '{{ states.sensor.weather_life_index.attributes.daily[14]["category"] }}'
      fishing_text:
        friendly_name: 钓鱼建议详情
        value_template: '{{ states.sensor.weather_life_index.attributes.daily[14]["text"] }}'
      sport_index:
        friendly_name: 运动指数
        value_template: '{{ states.sensor.weather_life_index.attributes.daily[15]["level"] }}'
      sport_category:
        friendly_name: 运动建议
        value_template: '{{ states.sensor.weather_life_index.attributes.daily[15]["category"] }}'
      sport_text:
        friendly_name: 运动建议详情
        value_template: '{{ states.sensor.weather_life_index.attributes.daily[15]["text"] }}'

QQ截图20210330213246.jpg QQ截图20210330213043.jpg

回复

使用道具 举报

1

主题

189

帖子

1919

积分

金牌会员

Rank: 6Rank: 6

积分
1919
金钱
1730
HASS币
0
发表于 2021-3-31 01:51:09 | 显示全部楼层
真心不错啊!受教!
回复

使用道具 举报

16

主题

222

帖子

1701

积分

金牌会员

Rank: 6Rank: 6

积分
1701
金钱
1479
HASS币
0
发表于 2021-3-31 03:15:20 | 显示全部楼层
firelion 发表于 2021-3-30 21:34
试着把所有指数都分离出来了,也做点贡献吧,第一次贴代码,不知道怎么排版,凑合看吧。指数太多了,图标也 ...

刚想说,居然全被你搞出来了,已经够用了,有没有公交车的API啊?
回复

使用道具 举报

0

主题

45

帖子

349

积分

中级会员

Rank: 3Rank: 3

积分
349
金钱
303
HASS币
0
发表于 2021-3-31 09:42:35 | 显示全部楼层
大佬!学习了
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

Archiver|手机版|小黑屋|Hassbian

GMT+8, 2024-4-24 09:43 , Processed in 0.107158 second(s), 35 queries .

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表