Files
esphome-yambms/packages/yambms/yambms_service.yaml
T
Sleeper85 3e596ea82a Reworked the update and telemetry services
- update and telemetry are now gated independently by substitution;
  var_service and its Update service switch are removed
- the uptime text sensor is published by its source instead of polling
  it, which fixes the 24855d readings that reached the telemetry, and
  the raw seconds are now sent instead of the formatted string
- the MAC address is replaced by a salted SHA-256, derived identically
  server side so no pairing window is needed
2026-11-08 23:08:33 +01:00

164 lines
7.3 KiB
YAML

# Updated : 2026.08.31
# Version : 1.4.0
# GitHub : https://github.com/Sleeper85/esphome-yambms
# YamBMS ( Yet another multi-BMS Merging Solution )
# This YAML is free software: you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation, either version 3
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
# See the GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/gpl.html>.
esphome:
on_boot:
- priority: 600
then:
- lambda: id(yambms${yambms_id}_running_version).publish_state("${yambms_version}");
- lambda: id(yambms${yambms_id}_last_version).publish_state("${yambms_version}");
# Derived node identifier sent to the telemetry service instead of the raw
# MAC address. get_mac_address() returns the canonical lowercase form with
# no separator, independent of whatever the board package exposes as its
# MAC sensor — a user-written board file must not be able to change it.
- lambda: |-
std::string in = "yambms_node_id:" + get_mac_address();
sha256::SHA256 hasher;
hasher.init();
hasher.add(in);
hasher.calculate();
char hex[65];
hasher.get_hex(hex);
id(yambms${yambms_id}_node_id_hash).publish_state(std::string(hex, 32));
text_sensor:
# +--------------------------------------+
# | YamBMS version |
# +--------------------------------------+
- platform: template
name: "Running version"
id: yambms${yambms_id}_running_version
device_id: yambms_yambms${yambms_id}
icon: mdi:cogs
entity_category: diagnostic
update_interval: never
- platform: template
name: "Last version"
id: yambms${yambms_id}_last_version
device_id: yambms_yambms${yambms_id}
icon: mdi:new-box
entity_category: diagnostic
update_interval: never
# +--------------------------------------+
# | Node identifier |
# +--------------------------------------+
# Exposed so the user can quote it for support: it is the only handle left
# once the MAC address is no longer sent. Published by on_boot above.
- platform: template
name: "Node ID hash"
id: yambms${yambms_id}_node_id_hash
device_id: ${board_id}
icon: mdi:fingerprint
entity_category: diagnostic
update_interval: never
# https://esphome.io/components/http_request.html
http_request:
verify_ssl: false
# Must stay under the ESP32 task WDT budget: a 5s WDT minus the 1s feed
# interval leaves 4s. The ESPHome default of 4.5s exceeds it and panics the
# loop task whenever a connect() is silently dropped by a firewall.
timeout: 3s
# DNS resolution runs before the socket and is NOT covered by `timeout`. A
# filtered port 53 blocks the loop task for a fixed 7s, which panics the WDT.
# See Design_decisions.md.
watchdog_timeout: 60s
interval:
- interval: 6h # 60min
# The scheduler fires an interval within 5 s of setup, not after a full
# period. Without this delay the first request lands during boot, and
# whether it runs depends on a race with the WiFi association.
startup_delay: 5min
then:
if:
condition:
and:
- lambda : return ${yambms_update_service} || ${yambms_telemetry_service};
- lambda : return id(my_network).is_connected(); # only proves the device has an IP, not that the internet is reachable.
- lambda : return id(yambms${yambms_id}_var_service_failures) < 3;
then:
- globals.set:
id: yambms${yambms_id}_var_service_round_failed
value: 'false'
# +--------------------------------------+
# | Update service (GET) |
# +--------------------------------------+
- if:
condition:
lambda: return ${yambms_update_service};
then:
- http_request.get:
url: http://script.opentel.be/yambms.get.php
capture_response: true
# on_error fires only when no connection could be made (DNS or
# connect failure) — the cases that freeze the loop. An HTTP error
# status still fires on_response, which rightly resets the counter.
on_error:
then:
- lambda: |-
id(yambms${yambms_id}_var_service_failures) += 1;
id(yambms${yambms_id}_var_service_round_failed) = true;
on_response:
then:
- lambda: |-
id(yambms${yambms_id}_var_service_failures) = 0;
json::parse_json(body, [](JsonObject root) -> bool {
// Explicitly cast to std::string to resolve ambiguity
id(yambms${yambms_id}_last_version).publish_state(root["version"].as<std::string>());
return true;
});
# +--------------------------------------+
# | Telemetry service (POST) |
# +--------------------------------------+
# Guarded by an `if` rather than nested in the GET's on_response: nesting
# would stack one blocking request inside another on a loop task whose
# stack is 8192 B by default (see LOOP_TASK_STACK.md).
# Skipped when a request already failed this round, to avoid a second 7s
# freeze for nothing. The two services are otherwise independent.
- if:
condition:
lambda: return ${yambms_telemetry_service} && !id(yambms${yambms_id}_var_service_round_failed);
then:
- http_request.post:
url: http://script.opentel.be/yambms.post.php
request_headers:
Content-Type: application/json
on_error:
then:
- lambda: |-
id(yambms${yambms_id}_var_service_failures) += 1;
id(yambms${yambms_id}_var_service_round_failed) = true;
on_response:
then:
- lambda: id(yambms${yambms_id}_var_service_failures) = 0;
json: |-
root["node_id_hash"] = id(yambms${yambms_id}_node_id_hash).state;
root["version"] = "${yambms_version}";
root["board_chip"] = "${board_chip}";
root["board_name"] = "${board_name}";
root["bms_model"] = id(yambms${yambms_id}_var_bms_models);
root["bms_protocol"] = id(yambms${yambms_id}_var_bms_protocols);
root["bms_count"] = id(yambms${yambms_id}_var_bms_counter);
root["comm_bus"] = id(yambms${yambms_id}_var_communication_bus);
root["comm_protocol"] = id(yambms${yambms_id}_var_communication_protocol);
root["uptime"] = id(esp32_uptime_sec).state;
root["date_time_now"] = id(esp32_date_time_now).state;