pepe: improved zigbee sensors and room logic

This commit is contained in:
Ingolf Wagner 2020-04-14 03:01:11 +02:00
parent 14ee540d4b
commit ab5ca9b142
No known key found for this signature in database
GPG key ID: 76BF5F1928B9618B
4 changed files with 215 additions and 148 deletions

View file

@ -33,54 +33,97 @@ in {
prometheus.namespace = "hass"; prometheus.namespace = "hass";
automation = [ automation = let
# todo when ich weis ich bin zuhause presents = room_group: present_group: [
#{ {
# alias = "Licht and wenn Dunkel"; alias = "presents -> turn on ${room_group} lights";
# trigger = { trigger = {
# platform = "state"; platform = "state";
# entity_id = [ "binary_sensor.night" ]; entity_id = "group.${present_group}";
# from = "off"; from = "off";
# to = "on"; to = "on";
# }; };
# action = [ action = {
# { service = "switch.turn_on";
# service = "switch.turn_on"; entity_id = "group.${room_group}";
# entity_id = "group.kitchen"; };
# } }
# { {
# service = "switch.turn_on"; alias = "absents -> turn off ${room_group} lights";
# entity_id = "group.living_room"; trigger = {
# } platform = "state";
# ]; entity_id = "group.${present_group}";
#} from = "on";
to = "off";
};
action = {
service = "switch.turn_off";
entity_id = "group.${room_group}";
};
}
];
in (presents "kitchen_room" "kitchen_room_present")
++ (presents "living_room" "living_room_present")
++ (presents "floor_room" "floor_room_present")
++ (presents "bed_room" "bed_room_present") ++ [
]; ];
group = { group = {
# rooms # rooms
# ----- # -----
floor_room = {
name = "Flur";
view = false;
};
floor_room_present = {
control = "hidden";
name = "Flur Anwesend";
view = false;
};
bed_room = { bed_room = {
name = "Schlafzimmer"; name = "Schlafzimmer";
view = false; view = false;
}; };
bed_room_present = {
control = "hidden";
name = "Schlafzimmer Anwesend";
view = false;
};
living_room = { living_room = {
name = "Wohnzimmer"; name = "Wohnzimmer";
view = false; view = false;
}; };
living_room_present = {
control = "hidden";
name = "Wohnzimmer Anwesend";
view = false;
};
kitchen_room = { kitchen_room = {
name = "Küche"; name = "Küche";
view = false; view = false;
}; };
kitchen_room_present = {
control = "hidden";
name = "Küche Anwesend";
view = false;
};
view_rooms = { view_rooms = {
name = "Räume"; name = "Räume";
view = true; view = true;
entities = [ entities = [
"group.all_lights" "group.all_lights"
"group.floor_room"
"group.floor_room_present"
"group.bed_room" "group.bed_room"
"group.bed_room_present"
"group.living_room" "group.living_room"
"group.living_room_present"
"group.kitchen_room" "group.kitchen_room"
"group.kitchen_room_present"
]; ];
}; };
@ -108,7 +151,7 @@ in {
view = false; view = false;
}; };
all_lights = { all_lights = {
name = "All Lights"; name = "Alle Lampen";
view = false; view = false;
}; };
unknown = { unknown = {

View file

@ -9,9 +9,9 @@ let
type = "light"; type = "light";
}; };
"pal02" = { "pal02" = {
label = "Schlafzimmer Lampe Bett"; label = "Flur Lampe";
icon = "mdi:lightbulb-on"; icon = "mdi:lightbulb-on";
room = "bed_room"; room = "floor_room";
type = "light"; type = "light";
}; };
"pal03" = { "pal03" = {

View file

@ -9,30 +9,63 @@ let
sensors = { sensors = {
buttons = { buttons = {
"button_1".id = "0x00158d0002b04f65"; "button_1" = {
"button_2".id = "0x00158d0002b04f09"; id = "0x00158d0002b04f65";
"button_3".id = "0x00158d0002b00e04"; room = "living_room_present";
};
"button_2" = {
id = "0x00158d0002b04f09";
room = "bed_room_present";
};
"button_3" = {
id = "0x00158d0002b00e04";
room = "bed_room_present";
};
}; };
temperature = { temperature = {
"temperature_sensor_1".id = "0x00158d0002d79220"; "temperature_sensor_1" = {
"temperature_sensor_2".id = "0x00158d0002d7913d"; id = "0x00158d0002d79220";
room = "living_room_present";
};
"temperature_sensor_2" = {
id = "0x00158d0002d7913d";
room = "living_room_present";
};
}; };
motion = { motion = {
"motion_sensor_1".id = "0x00158d0002fbd451"; "motion_sensor_1" = {
"motion_sensor_2".id = "0x00158d0002f9a6b8"; id = "0x00158d0002fbd451";
"motion_sensor_3".id = "0x00158d0002f04522"; room = "kitchen_room_present";
"motion_sensor_4".id = "0x00158d0002f9a558"; };
"motion_sensor_5".id = "0x00158d0002f9a56f"; "motion_sensor_2" = {
"motion_sensor_6".id = "0x00158d0002f9a5cb"; id = "0x00158d0002f9a6b8";
"motion_sensor_7".id = "0x00158d0002f9a6aa"; room = "kitchen_room_present";
"motion_sensor_8".id = "0x00158d0002f04637"; };
"motion_sensor_3" = {
id = "0x00158d0002f04522";
room = "living_room_present";
};
"motion_sensor_4" = {
id = "0x00158d0002f9a558";
room = "living_room_present";
};
"motion_sensor_5" = {
id = "0x00158d0002f9a56f";
room = "floor_room_present";
};
"motion_sensor_6" = {
id = "0x00158d0002f9a5cb";
room = "floor_room_present";
};
"motion_sensor_7" = { id = "0x00158d0002f9a6aa"; };
"motion_sensor_8" = { id = "0x00158d0002f04637"; };
}; };
door = { door = {
"door_sensor_1".id = "0x00158d000312dc52"; "door_sensor_1" = { id = "0x00158d000312dc52"; };
"door_sensor_2".id = "0x00158d000316d5bf"; "door_sensor_2" = { id = "0x00158d000316d5bf"; };
"door_sensor_3".id = "0x00158d0002f9516f"; "door_sensor_3" = { id = "0x00158d0002f9516f"; };
"door_sensor_4".id = "0x00158d00031383b9"; "door_sensor_4" = { id = "0x00158d00031383b9"; };
"door_sensor_5".id = "0x00158d0003120d3e"; "door_sensor_5" = { id = "0x00158d0003120d3e"; };
}; };
}; };
@ -83,6 +116,25 @@ in {
services.homeAssistantConfig = { services.homeAssistantConfig = {
# define input_boolean
# --------------------
# which get toggled by the buttons
input_boolean = builtins.listToAttrs (lib.flatten (lib.mapAttrsToList (name:
{ ... }: [
{
name = "single_${name}";
value = { icon = "mdi:radiobox-blank"; };
}
{
name = "double_${name}";
value = { icon = "mdi:radiobox-blank"; };
}
{
name = "hold_${name}";
value = { icon = "mdi:radiobox-blank"; };
}
]) sensors.buttons));
# define sensors # define sensors
# -------------- # --------------
sensor = let sensor = let
@ -203,7 +255,12 @@ in {
name = name; name = name;
value = { value = {
control = "hidden"; control = "hidden";
entities = [ "sensor.${name}" ] ++ (information name); entities = [
"sensor.${name}"
"input_boolean.single_${name}"
"input_boolean.double_${name}"
"input_boolean.hold_${name}"
] ++ (information name);
}; };
}) (sensors.buttons); }) (sensors.buttons);
@ -248,8 +305,34 @@ in {
}; };
}; };
in views // sensorButtons // sensorMotions // sensorTemperature # todo : make this more generic
// sensorDoors // { # append to *_room_present group
flatMotionSensors =
lib.mapAttrsToList (name: values: { name = name; } // values)
sensors.motion;
motionAllRooms' =
lib.groupBy ({ room ? "unknown", ... }: room) flatMotionSensors;
motionAllRooms = lib.mapAttrs
(_: devices: map ({ name, ... }: "binary_sensor.${name}") devices)
motionAllRooms';
# todo : make this more generic
# append to *_room_present group
flatButtonSensors =
lib.mapAttrsToList (name: values: { name = name; } // values)
sensors.buttons;
buttonAllRooms' =
lib.groupBy ({ room ? "unknown", ... }: room) flatButtonSensors;
buttonAllRooms = lib.mapAttrs (_: devices:
map ({ name, ... }: "input_boolean.single_${name}") devices)
buttonAllRooms';
presentsAllRooms =
lib.mapAttrs (_: entities: { entities = lib.flatten entities; })
(lib.zipAttrs [ buttonAllRooms motionAllRooms ]);
in views // presentsAllRooms // sensorButtons // sensorMotions
// sensorTemperature // sensorDoors // {
all_sensors.entities = all_sensors.entities =
(lib.mapAttrsToList (name: { ... }: "sensor.${name}") (lib.mapAttrsToList (name: { ... }: "sensor.${name}")
(sensors.buttons // sensors.temperature)) (sensors.buttons // sensors.temperature))
@ -259,26 +342,11 @@ in {
# create automation # create automation
# ----------------- # -----------------
# todo : this belongs in the home-assistant.nix
automation = let automation = let
# hold, turn off the lights everywhere # single click
all_lights_off = map (button: { toggle_single_button_input = map (button: {
alias = "turn all lights off on holding a button"; alias = "toggle hold ${button}";
trigger = {
platform = "mqtt";
topic = "zigbee2mqtt/${button}";
};
condition = {
condition = "template";
value_template = ''{{ "hold" == trigger.payload_json.action }}'';
};
action = { service = "script.turn_all_off"; };
}) [ "button_1" "button_2" "button_3" ];
# one click, turn on the light in the room
room_lights_on = map ({ button, room }: {
alias = "turn on lights in ${room}, on ${button} click";
trigger = { trigger = {
platform = "mqtt"; platform = "mqtt";
topic = "zigbee2mqtt/${button}"; topic = "zigbee2mqtt/${button}";
@ -288,27 +356,14 @@ in {
value_template = ''{{ "single" == trigger.payload_json.click}}''; value_template = ''{{ "single" == trigger.payload_json.click}}'';
}; };
action = { action = {
service = "switch.turn_on"; service = "input_boolean.toggle";
entity_id = "group.${room}"; data.entity_id = "input_boolean.single_${button}";
}; };
}) [ }) (builtins.attrNames sensors.buttons);
{
button = "button_1";
room = "living_room";
}
{
button = "button_2";
room = "bed_room";
}
{
button = "button_3";
room = "bed_room";
}
];
# double click, turn off the light in the room # double click
room_lights_off = map ({ button, room }: { toggle_double_button_input = map (button: {
alias = "turn on lights in ${room}, on ${button} click"; alias = "toggle hold ${button}";
trigger = { trigger = {
platform = "mqtt"; platform = "mqtt";
topic = "zigbee2mqtt/${button}"; topic = "zigbee2mqtt/${button}";
@ -318,62 +373,30 @@ in {
value_template = ''{{ "double" == trigger.payload_json.click}}''; value_template = ''{{ "double" == trigger.payload_json.click}}'';
}; };
action = { action = {
service = "switch.turn_off"; service = "input_boolean.toggle";
entity_id = "group.${room}"; data.entity_id = "input_boolean.double_${button}";
}; };
}) [ }) (builtins.attrNames sensors.buttons);
{
button = "button_1";
room = "living_room";
}
{
button = "button_2";
room = "bed_room";
}
{
button = "button_3";
room = "bed_room";
}
];
# kitchen light automation # hold
# ------------------------ toggle_hold_button_input = map (button: {
# https://www.home-assistant.io/integrations/binary_sensor.template/ alias = "toggle hold ${button}";
kitchen_lights = map (motion: [
{
alias = "turn on kitchen lights, on motion detected";
trigger = { trigger = {
platform = "state"; platform = "mqtt";
entity_id = "binary_sensor.${motion}"; topic = "zigbee2mqtt/${button}";
from = "off"; };
to = "on"; condition = {
condition = "template";
value_template = ''{{ "hold" == trigger.payload_json.action }}'';
}; };
action = { action = {
service = "switch.turn_on"; service = "input_boolean.toggle";
entity_id = "group.kitchen_room"; data.entity_id = "input_boolean.hold_${button}";
}; };
} }) (builtins.attrNames sensors.buttons);
{
alias = "turn off kitchen lights, on motion clear";
trigger = {
platform = "state";
entity_id = "binary_sensor.${motion}";
from = "on";
to = "off";
};
action = {
service = "switch.turn_off";
entity_id = "group.kitchen_room";
};
}
]) [ "motion_sensor_1" ];
in lib.flatten in lib.flatten (toggle_single_button_input ++ toggle_double_button_input
(kitchen_lights ++ all_lights_off ++ room_lights_on ++ room_lights_off); ++ toggle_hold_button_input);
# click = double => music an aus
# click = hold => film an aus
}; };

View file

@ -53,13 +53,14 @@ in {
''; '';
}; };
entities = mkOption { entities = mkOption {
default = [ ];
type = with types; listOf str; type = with types; listOf str;
description = '' description = ''
(list)(Required)Array or comma delimited string, list of entities to group. (list)(Required)Array or comma delimited string, list of entities to group.
''; '';
}; };
all = mkOption { all = mkOption {
default = true; default = false;
type = with types; bool; type = with types; bool;
description = '' description = ''
(boolean)(Optional)Set this to true if the group state should only turn on if all grouped entities are on. (boolean)(Optional)Set this to true if the group state should only turn on if all grouped entities are on.