跳转到内容

模板传感器

template 传感器平台允许您使用 lambdas 创建一个具有模板化值的传感器。

# 示例配置条目
sensor:
- platform: template
name: "Template Sensor"
lambda: |-
if (id(some_binary_sensor).state) {
return 42.0;
} else {
return 0.0;
}
update_interval: 60s

lambda 可能的返回值:

  • return <FLOATING_POINT_NUMBER>; 传感器的新值。
  • return NAN; 如果状态应被视为无效以指示错误(高级用法)。
  • return {}; 如果您不想发布新状态(高级用法)。
  • lambda (可选, lambda): 在每个更新间隔评估以获取传感器新值的 lambda。

  • update_interval (可选, Time): 检查传感器的间隔。设置为 never 以禁用更新。默认为 60s。

  • Sensor 的所有其他选项。

您也可以通过 sensor.template.publish 动作在 YAML 文件的其他位置向模板传感器发布状态。

# 示例配置条目
sensor:
- platform: template
name: "Template Sensor"
id: template_sens
# 在某个触发器中
on_...:
- sensor.template.publish:
id: template_sens
state: 42.0
# 模板化
- sensor.template.publish:
id: template_sens
state: !lambda 'return 42.0;'

配置选项:

  • id (必选, ID): 模板传感器的 ID。
  • state (必选, float, 可模板化): 要发布的状态。

NOTE

此动作也可以在 lambda 中编写:

id(template_sens).publish_state(42.0);

以下是用于调试和跟踪蓝牙代理的一些实用传感器。

# 示例配置条目
sensor:
- platform: template
name: "Bluetooth Proxy Connections Limit"
id: bluetooth_proxy_connections_limit
icon: "mdi:bluetooth-settings"
update_interval: 30s
entity_category: "diagnostic"
lambda: |-
int limit = bluetooth_proxy::global_bluetooth_proxy->get_bluetooth_connections_limit();
ESP_LOGD("bluetooth_proxy_sensor", "Current connections limit => %d", limit);
return limit;
- platform: template
name: "Bluetooth Proxy Connections Free"
id: bluetooth_proxy_connections_free
icon: "mdi:bluetooth-settings"
update_interval: 30s
entity_category: "diagnostic"
lambda: |-
int free = bluetooth_proxy::global_bluetooth_proxy->get_bluetooth_connections_free();
ESP_LOGD("bluetooth_proxy_sensor", "Current connections free => %d", free);
return free;