home-assistant: cleanup and motion sensor movement

This commit is contained in:
Ingolf Wagner 2020-05-03 18:22:28 +02:00
parent 3608d2a0d9
commit f2a913e862
No known key found for this signature in database
GPG key ID: 76BF5F1928B9618B
2 changed files with 193 additions and 171 deletions

View file

@ -67,14 +67,20 @@ in {
automation = let automation = let
# a help function that flattens even 2 times
flatMap = function: list: lib.flatten (map function (lib.flatten list));
allRooms = allRooms =
[ "living_room" "floor_room" "bath_room" "bed_room" "kitchen_room" ]; [ "living_room" "floor_room" "bath_room" "bed_room" "kitchen_room" ];
# presents <-> absents : light settings
# -------------------------------------
roomSwitches = let
roomPresents = { roomPresents = {
# group of the room that should be turned on # group of the room that should be turned on
roomGroup, roomGroup,
# group of the room that should be turned off # group of the room that should be turned off
roomOffGroup ? roomGroup, turnOffGroup ? roomGroup,
# group of the room that that indicates presents in the room # group of the room that that indicates presents in the room
presentsGroup, presentsGroup,
# global situation # global situation
@ -82,7 +88,8 @@ in {
# brightness for dimable lights # brightness for dimable lights
brightness ? 255 }: [ brightness ? 255 }: [
{ {
alias = "presents -> turn on ${roomGroup} lights in ${situation}"; alias =
"presents in ${presentsGroup} -> turn lights on in ${roomGroup} for ${situation}";
trigger = { trigger = {
platform = "state"; platform = "state";
entity_id = "group.${presentsGroup}"; entity_id = "group.${presentsGroup}";
@ -110,7 +117,7 @@ in {
} }
{ {
alias = alias =
"absents -> turn off ${roomOffGroup} lights in ${situation}"; "absents in ${presentsGroup} -> turn lights off ${turnOffGroup} in ${situation}";
trigger = { trigger = {
platform = "state"; platform = "state";
entity_id = "group.${presentsGroup}"; entity_id = "group.${presentsGroup}";
@ -137,23 +144,49 @@ in {
"{{ states( 'input_select.situation' ) == '${situation}' }}"; "{{ states( 'input_select.situation' ) == '${situation}' }}";
} }
]; ];
}; };
action = [ action = [
{ {
service = "switch.turn_off"; service = "switch.turn_off";
data.entity_id = "group.${roomOffGroup}"; data.entity_id = "group.${turnOffGroup}";
} }
{ {
service = "light.turn_off"; service = "light.turn_off";
data.entity_id = "group.${roomOffGroup}"; data.entity_id = "group.${turnOffGroup}";
} }
]; ];
} }
]; ];
onOffArguments = flatMap (name: [
{
roomGroup = "${name}_lights";
presentsGroup = "${name}_present";
situation = "dark";
}
{
roomGroup = "${name}_essential";
turnOffGroup = "${name}_lights";
presentsGroup = "${name}_present";
situation = "essential";
brightness = 30;
}
]) allRooms;
in (flatMap roomPresents onOffArguments);
situationSwitch = { fromRoomGroup, toRoomGroup, presentsGroup # change light scene in rooms when changing situation
, fromSituation, toSituation, # ---------------------------------------------------
situationSwitches = let
situationSwitch = {
# old group to turn off
fromRoomGroup,
# new group to turn on
toRoomGroup,
# group to determine presents
presentsGroup,
# old situation
fromSituation,
# new situation
toSituation,
# brightness for dimable lights # brightness for dimable lights
brightness ? 255 }: [ brightness ? 255 }: [
{ {
@ -218,21 +251,26 @@ in {
]; ];
} }
]; ];
allArguments = flatMap (name: [
in lib.flatten (map roomPresents (lib.flatten (map (name: [
{ {
roomGroup = "${name}_lights";
presentsGroup = "${name}_present"; presentsGroup = "${name}_present";
situation = "dark"; fromSituation = "dark";
} toSituation = "essential";
{ fromRoomGroup = "${name}_lights";
roomGroup = "${name}_essential"; toRoomGroup = "${name}_essential";
roomOffGroup = "${name}_lights";
presentsGroup = "${name}_present";
situation = "essential";
brightness = 30; brightness = 30;
} }
]) allRooms))) ++ [ {
presentsGroup = "${name}_present";
fromSituation = "essential";
toSituation = "dark";
fromRoomGroup = "${name}_essential";
toRoomGroup = "${name}_lights";
}
]) allRooms;
in flatMap situationSwitch allArguments;
in situationSwitches ++ roomSwitches ++ [
# control situation with buttons # control situation with buttons
{ {
@ -267,23 +305,7 @@ in {
}; };
}; };
} }
] ++ (lib.flatten (map situationSwitch (lib.flatten (map (name: [ ];
{
presentsGroup = "${name}_present";
fromSituation = "dark";
toSituation = "essential";
fromRoomGroup = "${name}_lights";
toRoomGroup = "${name}_essential";
brightness = 30;
}
{
presentsGroup = "${name}_present";
fromSituation = "essential";
toSituation = "dark";
fromRoomGroup = "${name}_essential";
toRoomGroup = "${name}_lights";
}
]) allRooms))));
group = let group = let
create_room = { name, description }: { create_room = { name, description }: {

View file

@ -10,7 +10,7 @@ let
}; };
"motion_sensor_2" = { "motion_sensor_2" = {
id = "0x00158d0002f9a6b8"; id = "0x00158d0002f9a6b8";
groups = [ "kitchen_room" "kitchen_room_present" ]; groups = [ "bed_room" "bed_room_present" ];
}; };
"motion_sensor_3" = { "motion_sensor_3" = {
id = "0x00158d0002f04522"; id = "0x00158d0002f04522";