Files
nixos-servers/services/home-assistant/default.nix
Torjus Håkestad 9ed11b712f
All checks were successful
Run nix flake check / flake-check (push) Successful in 2m13s
home-assistant: fix Jinja2 battery template syntax
The template used | min(100) | max(0) which is invalid Jinja2 syntax.
These filters expect iterables (lists), not scalar arguments. This
caused TypeError warnings on every MQTT message and left battery
sensors unavailable.

Fixed by using proper list-based min/max:
  [[[value, 100] | min, 0] | max

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-02-09 16:12:59 +01:00

113 lines
3.4 KiB
Nix

{ pkgs, config, ... }:
{
homelab.monitoring.scrapeTargets = [{
job_name = "home-assistant";
port = 8123;
metrics_path = "/api/prometheus";
scrape_interval = "60s";
}];
# Enable the Home Assistant service
services.home-assistant = {
enable = true;
package = pkgs.unstable.home-assistant;
configWritable = true;
config = null;
extraPackages =
python3Packages: with pkgs.unstable.python313Packages; [
aiopyarr
aioshelly
bellows
gtts
ha-silabs-firmware-client
isal
paho-mqtt
prometheus-client
pykodi
python-roborock
radios
uiprotect
unifi-discovery
universal-silabs-flasher
vacuum-map-parser-base
vacuum-map-parser-roborock
zha
zha-quirks
zigpy-cc
zigpy-deconz
zigpy-xbee
zigpy-zigate
zigpy-znp
zlib-ng
];
customComponents = with pkgs.home-assistant-custom-components; [
];
};
# Enable the mosquitto MQTT broker
services.mosquitto = {
enable = true;
persistence = true;
listeners = [
{
acl = [ "pattern readwrite #" ];
omitPasswordAuth = true;
settings.allow_anonymous = true;
}
];
};
# Enable the zigbee2mqtt service
services.zigbee2mqtt = {
enable = true;
package = pkgs.unstable.zigbee2mqtt;
settings = {
homeassistant = {
legacy_entity_attributes = false;
legacy_triggers = false;
};
availability = true;
frontend = true;
permit_join = false;
serial.port = "/dev/ttyUSB0";
# Inline device configuration (replaces devices.yaml)
# This allows declarative management and homeassistant overrides
devices = {
# Temperature sensors with battery fix
# WSDCGQ12LM sensors report battery: 0 due to firmware quirk
# Override battery calculation using voltage (mV): (voltage - 2100) / 9
"0x54ef441000a547bd" = {
friendly_name = "0x54ef441000a547bd";
homeassistant.battery.value_template = "{{ [[(((value_json.voltage | float) - 2100) / 9) | round(0) | int, 100] | min, 0] | max }}";
};
"0x54ef441000a54d3c" = {
friendly_name = "0x54ef441000a54d3c";
homeassistant.battery.value_template = "{{ [[(((value_json.voltage | float) - 2100) / 9) | round(0) | int, 100] | min, 0] | max }}";
};
"0x54ef441000a564b6" = {
friendly_name = "temp_server";
homeassistant.battery.value_template = "{{ [[(((value_json.voltage | float) - 2100) / 9) | round(0) | int, 100] | min, 0] | max }}";
};
# Other sensors
"0x00124b0025495463".friendly_name = "0x00124b0025495463"; # SONOFF temp sensor (battery works)
"0x54ef4410009ac117".friendly_name = "0x54ef4410009ac117"; # Water leak sensor
# Buttons
"0x54ef441000a1f907".friendly_name = "btn_livingroom";
"0x54ef441000a1ee71".friendly_name = "btn_bedroom";
# Philips Hue lights
"0x001788010d1b599a" = {
friendly_name = "0x001788010d1b599a";
transition = 5;
};
"0x001788010d253b99".friendly_name = "0x001788010d253b99";
"0x001788010e371aa4".friendly_name = "0x001788010e371aa4";
"0x001788010dc5f003".friendly_name = "0x001788010dc5f003";
"0x001788010dc35d06".friendly_name = "0x001788010dc35d06";
};
};
};
}