前言
前两周更新了一个和风天气的插件,很多朋友来私信问天气相关的内容在HomeAssistant里都可以做点啥?这里我借着我更新插件时候的想法给大家分享一下,天气相关的自动化,抛砖引玉,欢迎交流
前置要求
1.HomeAssistant (这个都没有的话您可以关网页了)
2.和风天气插件
github地址:https://github.com/c1pher-cn/heweather
之前的专栏介绍:网页链接
(优先看github,有些bug修了可能来不及更新文章,另外其他插件也是大同小异,可供参考)
3.tts
我这里用的我家的homepodmini,edge_tts 每家的设备不一样,选择适合你的
4.消息推送
是我这里使用的是iosApp内置的推送功能,推送给我和我老婆两个人的手机
我做了哪些天气自动化:
天气、灾害预警
预警中主要使用和风天气插件的这个两个实体
sensor.heweather_rain_warn (模板配置)
sensor.heweather_disaster_warn(插件自带)
在2023年11月更新heweather之后,天气预警需要手工在template里配置,配置方式详见网页链接
新的传感器在写代码的时候就考虑了自动化的实现,所以我们只判断传感器状态是否为on即可,on即说明对应传感器有异常天气,然后在tts或者通知里将属性中的status打出来即可,已经提前设置好。
alias: 一小时天气预警
trigger:
- platform: state
entity_id:
- sensor.heweather_rain_warn
from: "off"
to: "on"
action:
- service: notify.mobile_app_iphone_xr_2
data:
title: 天气预警
message: "注意注意! {{state_attr('sensor.heweather_rain_warn', 'states')}} "
- service: tts.edge_tts_say
data_template:
entity_id: media_player.ke_ting
message: "注意注意! {{state_attr('sensor.heweather_rain_warn', 'states')}} "
initial_state: true
alias: 自然灾害预警
trigger:
- platform: state
entity_id:
- sensor.heweather_disaster_warn
from: "off"
to: "on"
action:
- service: tts.edge_tts_say
data_template:
entity_id: media_player.ke_ting
message: "注意注意!!! {{state_attr('sensor.heweather_disaster_warn','states') }} "
initial_state: true
起床天气播报
天气播报这里我做了一个脚本来实现的,因为播报这个动作是很多情况都可能需要的,比如起床需要播报,想出家门或者家里其他人也想触发的情况,避免重复,我写了一个播报脚本,然后需要触发播报的时候只要触发脚本即可。
起床触发->天气播报
按钮触发->天气播报
天气播报脚本
alias: 客厅广播
sequence:
- service: tts.edge_tts_say
data_template:
entity_id: media_player.ke_ting
message: >
{% set timenow = now().time().strftime("%H") | int %}
{% if timenow > 8 and timenow < 12 %}
主人,早上好
{% elif timenow >= 12 and timenow < 14 %}
主人,中午好
{% elif timenow >= 14 and timenow < 18 %}
主人,下午好
{% elif timenow >= 18 and timenow < 23 %}
主人,晚上好
{% else %}
你好。
{% endif %}
现在天气{{states('sensor.heweather_text')}},
空气质量等级为{{states('sensor.heweather_category')}}
主要污染物为{{states('sensor.heweather_primary')}},
室外湿度{{states('sensor.heweather_humidity')}}%,
室外温度{{states('sensor.heweather_temperature')}}℃,
室外体感温度{{states('sensor.heweather_feelslike')}}℃,
室内湿度是{{states('sensor.4c65a8dbf864_humidity')}}%,
室内温度是{{states('sensor.qdhkl_ac_0106_temperature')}}℃,
{% if states('sensor.heweather_precip') | float > 0 %}
所在地区的降雨量为{{states('sensor.heweather_precip')}}毫米
{% else %}
{% endif %}
{{state_attr('sensor.heweather_rain_warn', 'states')}}
mode: single
如何定义起床?这里我是在客厅装了个人体传感器,把每天早晨第一次有人经过时定义为起床,在这个时候播报,后面再有人经过不再进行播报。所以我这里需要一个计数器,每天定时清零,然后早晨有人经过从0到1时触发起床自动化,启动播报脚本。
计数器:
counter:
livingroom_counter:
name: humancnt
icon: mdi:account-plus
计数器清零的自动化,这里不要问我为什么配置3点清零,因为我们家都是夜行动物
- alias: 计数器清零
initial_state: true
trigger:
platform: time
at: 03:00:00
action:
service: counter.reset
target:
entity_id: counter.livingroom_counter
计数器增加的自动化,人体传感器从off到on计数器就+1
- alias: 有人经过计数器增加
initial_state: true
trigger:
platform: state
entity_id: binary_sensor.motion_sensor_158d000121c2c4
from: 'off'
to: 'on'
action:
service: counter.increment
target:
entity_id: counter.livingroom_counter
上面说清楚了,起床就自动化就很简单了,计数器从0到1的时候就是有人起床啦,触发播报脚本即可
- alias: 起床播报
initial_state: true
trigger:
platform: state
entity_id: counter.livingroom_counter
to: '1'
action:
service: script.toggle
target:
entity_id: script.send_getup_broadcast
你学会了吗?对你有帮助的话,来波点赞关注吧,有其他想法欢迎留言区交流讨论