nixos-config/configs/pepe/home-assistant/zigbee2mqtt/motion.nix

122 lines
3.3 KiB
Nix
Raw Normal View History

2020-04-15 21:47:21 +02:00
{ pkgs, lib, ... }:
let
# https://www.zigbee2mqtt.io/devices/RTCGQ01LM.html
allDevices = {
"motion_sensor_1" = {
id = "0x00158d0002fbd451";
2020-04-16 00:22:29 +02:00
groups = [ "kitchen_room" "kitchen_room_present" ];
2020-04-15 21:47:21 +02:00
};
"motion_sensor_2" = {
id = "0x00158d0002f9a6b8";
groups = [ "bed_room" "bed_room_present" ];
2020-04-15 21:47:21 +02:00
};
"motion_sensor_3" = {
id = "0x00158d0002f04522";
2020-04-16 00:22:29 +02:00
groups = [ "living_room" "living_room_present" ];
2020-04-15 21:47:21 +02:00
};
"motion_sensor_4" = {
id = "0x00158d0002f9a558";
2020-04-16 00:22:29 +02:00
groups = [ "living_room" "living_room_present" ];
2020-04-15 21:47:21 +02:00
};
"motion_sensor_5" = {
id = "0x00158d0002f9a56f";
2020-04-16 00:22:29 +02:00
groups = [ "bath_room" "bath_room_present" ];
2020-04-15 21:47:21 +02:00
};
"motion_sensor_6" = {
id = "0x00158d0002f9a5cb";
2020-04-16 00:22:29 +02:00
groups = [ "floor_room" "floor_room_present" ];
2020-04-15 21:47:21 +02:00
};
"motion_sensor_7" = {
id = "0x00158d0002f9a6aa";
2020-04-16 00:22:29 +02:00
groups = [ "bed_room" "bed_room_present" ];
2020-04-15 21:47:21 +02:00
};
"motion_sensor_8" = {
id = "0x00158d0002f04637";
2020-04-16 00:22:29 +02:00
groups = [ "bath_room" "bath_room_present" ];
2020-04-15 21:47:21 +02:00
};
};
in {
services.zigbee2mqtt.devices = lib.mapAttrs' (name:
2020-05-01 03:36:51 +02:00
{ id, timeout ? 65, ... }: {
2020-04-15 21:47:21 +02:00
name = id;
value = {
retain = false;
friendly_name = name;
2020-04-16 00:10:12 +02:00
# should not be set below 60 seconds
occupancy_timeout = timeout;
2020-04-15 21:47:21 +02:00
};
}) allDevices;
services.homeAssistantConfig = {
# define meta information sensors
sensor = lib.flatten (lib.mapAttrsToList (name:
{ ... }: [
{
name = "battery_${name}";
platform = "mqtt";
state_topic = "zigbee2mqtt/${name}";
availability_topic = "zigbee2mqtt/bridge/state";
unit_of_measurement = "%";
icon = "mdi:battery-10";
value_template = "{{ value_json.battery }}";
}
{
name = "link_${name}";
platform = "mqtt";
state_topic = "zigbee2mqtt/${name}";
availability_topic = "zigbee2mqtt/bridge/state";
icon = "mdi:signal";
unit_of_measurement = "lqi";
value_template = "{{ value_json.linkquality }}";
}
]) allDevices);
binary_sensor = lib.mapAttrsToList (name:
{ ... }: {
name = name;
platform = "mqtt";
device_class = "motion";
state_topic = "zigbee2mqtt/${name}";
availability_topic = "zigbee2mqtt/bridge/state";
payload_on = true;
payload_off = false;
value_template = "{{ value_json.occupancy }}";
}) allDevices;
# create groups
# -------------
group = let
# to have nice panels for every device
sensorGroups = lib.mapAttrs (name:
{ ... }: {
control = "hidden";
entities = [
"binary_sensor.${name}"
"sensor.battery_${name}"
"sensor.link_${name}"
];
}) allDevices;
# sort lights into given groups.
sortedInGroups = let
groupEntries = lib.zipAttrs (lib.flatten (lib.mapAttrsToList (name:
{ groups ? [ ], ... }:
map (groupName: { "${groupName}" = "binary_sensor.${name}"; }) groups)
allDevices));
in lib.mapAttrs (name: entities: { inherit entities; }) groupEntries;
in sortedInGroups // sensorGroups // {
all_sensors.entities =
lib.mapAttrsToList (name: { ... }: "binary_sensor.${name}") allDevices;
};
};
}