HAOS集成和风天气
室内搞完了,室外就别搞传感器了,直接走api拉数据。
系统默认集成的OpenWeather和AccuWeather都不太靠谱,前者填了api key后报无效,后者是数据离实际差距大+免费api次数不够用,最后还得自己搞国内的天气服务API。
注册了高德、和风两家,后者虽然没有前者的品牌,但毕竟数据种类更齐全,用来练手是不错的。
效果如下:
至于怎么安装,其实什么插件都不用装,直接File Editor插件编写 /config/configuration.yaml ,保存无误后,重启服务即可。
【注】重启服务,只需要“配置”->“系统”->“重新启动”。这个过程很快,30s搞定。不需要重启整个HAOS的vm或者物理机,那个时间要长很多。
要写什么内容呢?
【注】为便于管理,以下配置已提交到github。
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 |
# 文件中原始存在内容要保持,莫要删除 # 此处新增配置,应加到原始内容之下 # 经纬度可以从这里查:https://lbs.amap.com/tools/picker # 和风天气api申请:https://dev.qweather.com/ weather: - platform: template name: "HeFeng Weather Station" condition_template: "{{ states('sensor.hf_condition') }}" temperature_template: "{{ states('sensor.hf_temp') | int }}" temperature_unit: "°C" humidity_template: "{{ states('sensor.hf_humidity') | int }}" precipitation_unit: "mm" visibility_template: "{{ states('sensor.hf_visibility') | int }}" visibility_unit: "km" pressure_template: "{{ states('sensor.hf_pressure') | int }}" pressure_unit: "hPa" wind_speed_template: "{{ states('sensor.hf_wind_speed') | int }}" wind_speed_unit: "km/h" wind_bearing_template: "{{ states('sensor.hf_wind_bearing') | int }}" ozone_template: "{{ states('sensor.hf_aqi_o3') | int }}" forecast_template: >- {% set icon_cond_mapper = {'100': 'sunny', '101': 'partlycloudy', '102': 'partlycloudy', '103': 'partlycloudy', '104': 'cloudy', '150': 'clear-night', '151': 'partlycloudy', '152': 'partlycloudy', '153': 'partlycloudy', '154': 'cloudy', '300': 'rainy', '301': 'rainy', '302': 'lightning-rainy', '303': 'lightning-rainy', '304': 'hail', '305': 'rainy', '306': 'rainy', '307': 'extreme-rain', '308': 'extreme-rain', '309': 'rainy', '310': 'extreme-rain', '311': 'extreme-rain', '312': 'extreme-rain', '313': 'rainy', '314': 'rainy', '315': 'rainy', '316': 'extreme-rain', '317': 'extreme-rain', '318': 'extreme-rain', '350': 'rainy', '351': 'extreme-rain', '399': 'rainy', '400': 'snowy', '401': 'snowy', '402': 'heavy-snow', '403': 'heavy-snow', '404': 'snowy-rainy', '405': 'snowy-rainy', '406': 'snowy-rainy', '407': 'rainy', '408': 'rainy', '409': 'rainy', '410': 'heavy-snow', '456': 'snowy-rainy', '457': 'rainy', '499': 'rainy', '500': 'fog', '501': 'fog', '502': 'fog', '503': 'exceptional', '504': 'exceptional', '507': 'exceptional', '508': 'exceptional', '509': 'fog', '510': 'fog', '511': 'fog', '512': 'fog', '513': 'fog', '514': 'fog', '515': 'fog', '900': 'exceptional', '901': 'exceptional', '999': 'exceptional'} %} {% set fc_data = state_attr('sensor.hf_forecast', 'daily') %} {% macro fc_mapper() %} {%- for fc in fc_data -%} {%- if not loop.first %};{% endif %} {{ dict(datetime=fc['fxDate'], condition=icon_cond_mapper[ fc['iconDay'] ] if fc['iconDay'] in icon_cond_mapper else 'Unknown', native_temperature=fc['tempMax'] | int, native_templow=fc['tempMin'] | int, native_precipitation=fc['precip'] | float, native_pressure=fc['pressure'] | int, native_wind_speed=fc['windSpeedDay'] | int, wind_bearing=fc['wind360Day'] | int ) | tojson }} {%- endfor %} {% endmacro %} {{ fc_mapper().strip().split(';') | map('from_json') | list }} sensor: - platform: rest name: HF Weather # 实时天气信息接口 json_attributes: - now scan_interval: 900 value_template: "{{ value_json['now']['text'].title() }}" icon: "mdi:white-balance-sunny" resource: https://devapi.qweather.com/v7/weather/now?location=116.51,39.80&key=4f952e81b806476a8e9ee3885c530734 - platform: rest name: HF Aqi # 实时空气质量信息接口 json_attributes: - now scan_interval: 1200 value_template: "{{ value_json['now']['category'].title() }}" icon: "mdi:air-filter" resource: https://devapi.qweather.com/v7/air/now?location=116.51,39.80&key=4f952e81b806476a8e9ee3885c530734 - platform: rest name: HF Forecast # 三天预报接口 json_attributes: - daily scan_interval: 7200 value_template: "{{ value_json['code'].title() }}" # icon: "mdi:air-filter" resource: https://devapi.qweather.com/v7/weather/3d?location=116.51,39.80&key=4f952e81b806476a8e9ee3885c530734 - platform: template sensors: hf_condition: friendly_name: "Outside condition" value_template: >- {% set mapper = {'100': 'sunny', '101': 'partlycloudy', '102': 'partlycloudy', '103': 'partlycloudy', '104': 'cloudy', '150': 'clear-night', '151': 'partlycloudy', '152': 'partlycloudy', '153': 'partlycloudy', '154': 'cloudy', '300': 'rainy', '301': 'rainy', '302': 'lightning-rainy', '303': 'lightning-rainy', '304': 'hail', '305': 'rainy', '306': 'rainy', '307': 'extreme-rain', '308': 'extreme-rain', '309': 'rainy', '310': 'extreme-rain', '311': 'extreme-rain', '312': 'extreme-rain', '313': 'rainy', '314': 'rainy', '315': 'rainy', '316': 'extreme-rain', '317': 'extreme-rain', '318': 'extreme-rain', '350': 'rainy', '351': 'extreme-rain', '399': 'rainy', '400': 'snowy', '401': 'snowy', '402': 'heavy-snow', '403': 'heavy-snow', '404': 'snowy-rainy', '405': 'snowy-rainy', '406': 'snowy-rainy', '407': 'rainy', '408': 'rainy', '409': 'rainy', '410': 'heavy-snow', '456': 'snowy-rainy', '457': 'rainy', '499': 'rainy', '500': 'fog', '501': 'fog', '502': 'fog', '503': 'exceptional', '504': 'exceptional', '507': 'exceptional', '508': 'exceptional', '509': 'fog', '510': 'fog', '511': 'fog', '512': 'fog', '513': 'fog', '514': 'fog', '515': 'fog', '900': 'exceptional', '901': 'exceptional', '999': 'exceptional'} %} {% set icon = state_attr('sensor.hf_weather', 'now')['icon'] %} {{ mapper[icon] if icon in mapper else 'Unknown' }} hf_temp: friendly_name: "Outside temp" # 温度 value_template: "{{ state_attr('sensor.hf_weather', 'now')['temp'] | int }}" device_class: temperature unit_of_measurement: "°C" hf_temp_feels: friendly_name: "Outside temp feels" # 体感温度 value_template: "{{ state_attr('sensor.hf_weather', 'now')['feelsLike'] | int }}" device_class: temperature unit_of_measurement: "°C" hf_dew_point: friendly_name: "Outside dew point" # 露点 value_template: "{{ state_attr('sensor.hf_weather', 'now')['dew'] | int }}" icon_template: mdi:thermometer-water unit_of_measurement: "°C" hf_pressure: friendly_name: "Outside pressure" # 气压 value_template: "{{ state_attr('sensor.hf_weather', 'now')['pressure'] | int }}" device_class: pressure unit_of_measurement: "hPa" hf_humidity: friendly_name: "Outside humidity" # 湿度 value_template: "{{ state_attr('sensor.hf_weather', 'now')['humidity'] | int }}" device_class: humidity unit_of_measurement: "%" hf_precipitation: friendly_name: "Outside precipitation" # 降水 value_template: "{{ state_attr('sensor.hf_weather', 'now')['precip'] | float }}" icon_template: mdi:weather-rainy unit_of_measurement: "mm" hf_wind_speed: friendly_name: "Outside wind speed" # 风速 value_template: "{{ state_attr('sensor.hf_weather', 'now')['windSpeed'] | int }}" icon_template: mdi:weather-windy unit_of_measurement: "Km/h" hf_wind_scale: friendly_name: "Outside wind scale" # 风力等级 value_template: "{{ state_attr('sensor.hf_weather', 'now')['windScale'] | int }}" icon_template: mdi:weather-windy unit_of_measurement: "BfL" hf_wind_direction: friendly_name: "Outside wind direction" # 风向 value_template: "{{ state_attr('sensor.hf_weather', 'now')['windDir'] }}" icon_template: mdi:windsock hf_wind_bearing: friendly_name: "Outside wind bearing" #风向角 value_template: "{{ state_attr('sensor.hf_weather', 'now')['wind360'] }}" icon_template: mdi:windsock hf_visibility: friendly_name: "Outside visibility" # 能见度 value_template: "{{ state_attr('sensor.hf_weather', 'now')['vis'] | int }}" unit_of_measurement: "Km" icon_template: mdi:eye hf_cloud_cover: friendly_name: "Outside cloud cover" # 云量 value_template: "{{ state_attr('sensor.hf_weather', 'now')['cloud'] | int }}" icon_template: mdi:cloud unit_of_measurement: "%" hf_aqi_index: friendly_name: "Outside aqi index" # 空气质量指数 value_template: "{{ state_attr('sensor.hf_aqi', 'now')['aqi'] | int }}" device_class: aqi hf_aqi_level: friendly_name: "Outside aqi level" # 空气质量等级 value_template: "{{ state_attr('sensor.hf_aqi', 'now')['level'] | int }}" device_class: aqi unit_of_measurement: "Lvl" hf_aqi_primary: friendly_name: "Outside aqi primary" # 主要污染物 value_template: "{{ state_attr('sensor.hf_aqi', 'now')['primary'] }}" icon_template: mdi:weather-dust hf_aqi_pm10: friendly_name: "Outside aqi pm10" # pm10含量 value_template: "{{ state_attr('sensor.hf_aqi', 'now')['pm10'] | int }}" # icon_template: mdi:weather-dust device_class: pm10 unit_of_measurement: "ug/m³" hf_aqi_pm2p5: friendly_name: "Outside aqi pm2.5" # pm2.5含量 value_template: "{{ state_attr('sensor.hf_aqi', 'now')['pm2p5'] | int }}" # icon_template: mdi:weather-dust device_class: pm25 unit_of_measurement: "ug/m³" hf_aqi_o3: friendly_name: "Outside aqi o3" # o3含量 value_template: "{{ state_attr('sensor.hf_aqi', 'now')['o3'] | int }}" # icon_template: mdi:weather-dust device_class: ozone unit_of_measurement: "ppm" |
重启后,在“配置”->“设备与服务”->“实体注册表”里,可以根据关键字检索到所有经rest请求得到的传感器。
【注】如果重启后什么都看不到,同样使用File Editor观察 /config/home-assistant.log 即可查阅报错信息,从而调整 /config/configuration.yaml 的配置,以解决问题。
然后如何加入首页dashboard就不详述了。
如此这般,手机首屏就能室内外气象完整对比了,我先测试几天看效果如何。
文章的脚注信息由WordPress的wp-posturl插件自动生成