本帖最后由 hungheo 于 2025-8-29 21:04 编辑
BBGPPR 发表于 2025-8-29 19:13
老大,有个问题我一直没想明白,不知道可否略微指点。
关于sensor.devices_count_light_all这个传感器, ...
首先我是用nodered+mqtt创建的,你也可以用homeassistant自带的
configration.yaml里面创建sensor,或者设置-->设备与服务-->辅助元素-->template-->传感器模板
两种方式都可以,统计的代码是jinja2写的
这里给你一个插座的示例(很早以前用过的)
{% set outlet_entities = [
'switch.b460edf16109_outlet',
'switch.649e31805c7c_outlet',
'switch.28d1272ade67_outlet',
'switch.28d1272a27a9_outlet',
'switch.649e316975f3_outlet'] %}
{% set total_outlet = outlet_entities | length %}
{% set non_off_outlet = states | selectattr('entity_id', 'in', outlet_entities) | selectattr('state', 'eq', 'on') | list %}
{% set non_off_count = non_off_outlet | length %}
{% if non_off_count > 0 %}
{% if non_off_count == total_outlet %}
全开
{% else %}
{{ non_off_count }}
{% endif %}
{% else %}
全关
{% endif %}
其次,你所说的时间统计,这个是homeassistant里面sensor的属性决定的,通过history来记录,储存在config目录下home-assistant_v2.db这个文件里(如果是默认的数据库的话)
如果想调用就得调用history的内容,查询某天的开关的时间点然后再计算时长(homeassistant里面没试过,nodered可以很轻松的实现)
也有简便的方法:辅助元素-->历史统计
或者configration.yaml配置(这是示例,自行修改):
参考链接在这里:https://www.home-assistant.io/integrations/history_stats/
# Example configuration.yaml entry
sensor:
- platform: history_stats
name: Lamp ON today
entity_id: light.my_lamp
state: "on"
type: time
start: "{{ today_at() }}"
end: "{{ now() }}"
最后,我分享的UI上面的状态最后更新时间,是UI插件自带的,mushroom、button-card、bubble-card这些都有,它们只是查询实体的属性里面的最后更新时间,计算下与当前时间的时间差,然后在显示出来,并不能统计时长
|