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,172 +67,210 @@ 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" ];
roomPresents = { # presents <-> absents : light settings
# group of the room that should be turned on # -------------------------------------
roomGroup, roomSwitches = let
# group of the room that should be turned off roomPresents = {
roomOffGroup ? roomGroup, # group of the room that should be turned on
# group of the room that that indicates presents in the room roomGroup,
presentsGroup, # group of the room that should be turned off
# global situation turnOffGroup ? roomGroup,
situation, # group of the room that that indicates presents in the room
# brightness for dimable lights presentsGroup,
brightness ? 255 }: [ # global situation
{ situation,
alias = "presents -> turn on ${roomGroup} lights in ${situation}"; # brightness for dimable lights
trigger = { brightness ? 255 }: [
platform = "state"; {
entity_id = "group.${presentsGroup}"; alias =
from = "off"; "presents in ${presentsGroup} -> turn lights on in ${roomGroup} for ${situation}";
to = "on"; trigger = {
}; platform = "state";
condition = { entity_id = "group.${presentsGroup}";
condition = "state"; from = "off";
entity_id = "input_select.situation"; to = "on";
state = situation; };
}; condition = {
action = [ condition = "state";
{ entity_id = "input_select.situation";
service = "switch.turn_on"; state = situation;
data.entity_id = "group.${roomGroup}"; };
} action = [
{
service = "light.turn_on";
data = {
entity_id = "group.${roomGroup}";
brightness = brightness;
};
}
];
}
{
alias =
"absents -> turn off ${roomOffGroup} lights in ${situation}";
trigger = {
platform = "state";
entity_id = "group.${presentsGroup}";
from = "on";
to = "off";
for.seconds = 2 * 60;
};
condition = {
condition = "and";
conditions = [
{ {
condition = "template"; service = "switch.turn_on";
value_template = data.entity_id = "group.${roomGroup}";
"{{ states( 'input_select.room_present' ) != '${presentsGroup}' }}";
} }
{ {
condition = "template"; service = "light.turn_on";
value_template = data = {
"{{ states( 'input_boolean.alone' ) == 'on' }}"; entity_id = "group.${roomGroup}";
} brightness = brightness;
{ };
condition = "template";
value_template =
"{{ states( 'input_select.situation' ) == '${situation}' }}";
} }
]; ];
}
}; {
action = [ alias =
{ "absents in ${presentsGroup} -> turn lights off ${turnOffGroup} in ${situation}";
service = "switch.turn_off"; trigger = {
data.entity_id = "group.${roomOffGroup}"; platform = "state";
} entity_id = "group.${presentsGroup}";
{ from = "on";
service = "light.turn_off"; to = "off";
data.entity_id = "group.${roomOffGroup}"; for.seconds = 2 * 60;
} };
]; condition = {
} condition = "and";
]; conditions = [
{
situationSwitch = { fromRoomGroup, toRoomGroup, presentsGroup condition = "template";
, fromSituation, toSituation, value_template =
# brightness for dimable lights "{{ states( 'input_select.room_present' ) != '${presentsGroup}' }}";
brightness ? 255 }: [ }
{
condition = "template";
value_template =
"{{ states( 'input_boolean.alone' ) == 'on' }}";
}
{
condition = "template";
value_template =
"{{ states( 'input_select.situation' ) == '${situation}' }}";
}
];
};
action = [
{
service = "switch.turn_off";
data.entity_id = "group.${turnOffGroup}";
}
{
service = "light.turn_off";
data.entity_id = "group.${turnOffGroup}";
}
];
}
];
onOffArguments = flatMap (name: [
{ {
alias = roomGroup = "${name}_lights";
"${fromSituation} -> ${toSituation} for ${fromRoomGroup} -> ${toRoomGroup} on"; presentsGroup = "${name}_present";
trigger = { situation = "dark";
platform = "state";
entity_id = "input_select.situation";
from = fromSituation;
to = toSituation;
};
condition = {
condition = "state";
entity_id = "group.${presentsGroup}";
state = "on";
};
action = [
{
service = "switch.turn_off";
data.entity_id = "group.${fromRoomGroup}";
}
{
service = "light.turn_off";
data.entity_id = "group.${fromRoomGroup}";
}
{
service = "switch.turn_on";
data.entity_id = "group.${toRoomGroup}";
}
{
service = "light.turn_on";
data = {
entity_id = "group.${toRoomGroup}";
brightness = brightness;
};
}
];
} }
{ {
alias = roomGroup = "${name}_essential";
"${fromSituation} -> ${toSituation} for ${fromRoomGroup} -> ${toRoomGroup} off"; turnOffGroup = "${name}_lights";
trigger = { presentsGroup = "${name}_present";
platform = "state"; situation = "essential";
entity_id = "input_select.situation"; brightness = 30;
from = fromSituation;
to = toSituation;
};
condition = {
condition = "state";
entity_id = "group.${presentsGroup}";
state = "off";
};
action = [
{
service = "switch.turn_off";
data.entity_id = "group.${fromRoomGroup}";
}
{
service = "light.turn_off";
data.entity_id = "group.${fromRoomGroup}";
}
];
} }
]; ]) allRooms;
in (flatMap roomPresents onOffArguments);
in lib.flatten (map roomPresents (lib.flatten (map (name: [ # change light scene in rooms when changing situation
{ # ---------------------------------------------------
roomGroup = "${name}_lights"; situationSwitches = let
presentsGroup = "${name}_present"; situationSwitch = {
situation = "dark"; # old group to turn off
} fromRoomGroup,
{ # new group to turn on
roomGroup = "${name}_essential"; toRoomGroup,
roomOffGroup = "${name}_lights"; # group to determine presents
presentsGroup = "${name}_present"; presentsGroup,
situation = "essential"; # old situation
brightness = 30; fromSituation,
} # new situation
]) allRooms))) ++ [ toSituation,
# brightness for dimable lights
brightness ? 255 }: [
{
alias =
"${fromSituation} -> ${toSituation} for ${fromRoomGroup} -> ${toRoomGroup} on";
trigger = {
platform = "state";
entity_id = "input_select.situation";
from = fromSituation;
to = toSituation;
};
condition = {
condition = "state";
entity_id = "group.${presentsGroup}";
state = "on";
};
action = [
{
service = "switch.turn_off";
data.entity_id = "group.${fromRoomGroup}";
}
{
service = "light.turn_off";
data.entity_id = "group.${fromRoomGroup}";
}
{
service = "switch.turn_on";
data.entity_id = "group.${toRoomGroup}";
}
{
service = "light.turn_on";
data = {
entity_id = "group.${toRoomGroup}";
brightness = brightness;
};
}
];
}
{
alias =
"${fromSituation} -> ${toSituation} for ${fromRoomGroup} -> ${toRoomGroup} off";
trigger = {
platform = "state";
entity_id = "input_select.situation";
from = fromSituation;
to = toSituation;
};
condition = {
condition = "state";
entity_id = "group.${presentsGroup}";
state = "off";
};
action = [
{
service = "switch.turn_off";
data.entity_id = "group.${fromRoomGroup}";
}
{
service = "light.turn_off";
data.entity_id = "group.${fromRoomGroup}";
}
];
}
];
allArguments = flatMap (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;
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";