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

标题: 如何读取某个实体属性中的数值 [打印本页]

作者: roc227    时间: 2023-5-9 01:00
标题: 如何读取某个实体属性中的数值
请教各位大神,我想调用和风天气中的次日最高/最低温度,但这个温度在实体中是以日期中的形式出现,有什么办法能调用这个数值
比如2023-05-09中的temperature: 19和templow: 6
这个日期还是每天都在变化

weather.localweather实体的属性如下temperature: 8
temperature_unit: °C
humidity: 37
pressure: 980
pressure_unit: hPa
wind_bearing: 南风
wind_speed: 5
wind_speed_unit: km/h
visibility: 0
visibility_unit: km
precipitation_unit: mm
forecast:
  - datetime: '2023-05-09T00:53:22.559211'
    condition: sunny
    temperature: 19
    templow: 6
  - datetime: '2023-05-10T00:53:22.559211'
    condition: cloudy
    temperature: 18
    templow: 5


作者: gx19970920    时间: 2023-5-9 08:37
可以参考我写的这一个教程试一下
分享下将实体的属性独立成一个新实体的放
https://bbs.hassbian.com/thread-19884-1-1.html
(出处: 『瀚思彼岸』» 智能家居技术论坛)

作者: shayulei    时间: 2023-5-9 08:49
在模板的yaml文件里写,下面是我从天气里提取的。
  - sensor :
    - name: "Max temperature"
      unit_of_measurement: "°C"
      state: '{{state_attr("weather.sha_yu_lei_de_jia", "forecast")[0].temperature}}'

    - name: "Low temperature"
      unit_of_measurement: "°C"
      state: '{{state_attr("weather.sha_yu_lei_de_jia", "forecast")[0]. templow}}'
作者: shayulei    时间: 2023-5-9 08:51
在configuration.yaml里写上template: !include templates.yaml,然后就是在同级目录下生成一个叫templates.yaml的文件,在文件里编辑,我感觉这样写清楚一些。当然也可以写在configuration.yaml里
作者: bugensui    时间: 2023-5-9 11:17
shayulei 发表于 2023-5-9 08:51
在configuration.yaml里写上template: !include templates.yaml,然后就是在同级目录下生成一个叫templates ...

这样分开写的区别在哪里,有什么好处呢
作者: zheng1112    时间: 2023-5-9 11:35
本帖最后由 zheng1112 于 2023-5-9 11:37 编辑

这个题我会。
{{ states.weather.localweather.attributes.forecast[0].templow }}~{{ states.weather.localweather.attributes.forecast[0].temperature }}°C

[attach]47713[/attach]

作者: book123    时间: 2023-5-9 12:14
在esphome里面是这样:

# 室外空气质量-彩云天气
sensor:
  - platform: homeassistant
    id: outside_aqi_caiyun
    entity_id: weather.wo_de_jia_nas12vm
    attribute: aqi
    internal: true
作者: cjnt007    时间: 2023-5-9 13:44
zheng1112 发表于 2023-5-9 11:35
这个题我会。
{{ states.weather.localweather.attributes.forecast[0].templow }}~{{ states.weather.loca ...

6楼正解!
我是通过chatGPT查询也是同样的答案[attach]47716[/attach]

作者: xiaoxihu    时间: 2023-5-9 14:36
这个真的实用,正需要啊
作者: roc227    时间: 2023-5-10 00:07
shayulei 发表于 2023-5-9 08:49
在模板的yaml文件里写,下面是我从天气里提取的。
  - sensor :
    - name: "Max temperature"

感谢感谢!稍微动了点,也能用了
作者: roc227    时间: 2023-5-10 00:09
感谢楼上们的帮助,学会了[0]的用法,稍微改了改也能用了

  1. sensor:
  2.   - platform: template
  3.     sensors:
  4.       tempnows:
  5.         friendly_name: "当前温度"
  6.         unit_of_measurement: "°C"
  7.         value_template:  "{{state_attr('weather.localweather', 'temperature' )| round(0)}}"
  8.   - platform: template
  9.     sensors:
  10.       temphight:
  11.         friendly_name: "次日温度高"
  12.         unit_of_measurement: "°C"
  13.         value_template:  "{{state_attr('weather.localweather', 'forecast')[0].temperature| round(0)}}"
复制代码

作者: shayulei    时间: 2023-5-10 09:01
bugensui 发表于 2023-5-9 11:17
这样分开写的区别在哪里,有什么好处呢

防止configureation.yaml文件太大,找东西麻烦,这样写传感器就到传感器的文件里找,模板就到模板的文件里找。
作者: perseuszhang    时间: 2024-4-16 07:34
请教一下,这个“templow: 6” 想在ESPHome直接调用应该怎么写呢?




欢迎光临 『瀚思彼岸』» 智能家居技术论坛 (https://bbs.hassbian.com/) Powered by Discuz! X3.5