96 lines
3.2 KiB
Nix
96 lines
3.2 KiB
Nix
|
let
|
||
|
helper = import ./home-assistant-automation-helpers.nix;
|
||
|
|
||
|
entityLightSleepingHoursNotBefore = "light_sleeping_hours_not_before";
|
||
|
entityLightSleepingHoursNotAfter = "light_sleeping_hours_not_after";
|
||
|
entityLightDarkHoursNotBefore = "light_dark_hours_not_before";
|
||
|
entityLightDarkHoursNotAfter = "light_dark_hours_not_after";
|
||
|
|
||
|
conditionalAutomation = slug: triggers: conditions: actions: {
|
||
|
alias = "Light: ${slug}";
|
||
|
id = "light_automation_${slug}";
|
||
|
trigger = triggers;
|
||
|
condition = conditions;
|
||
|
action = actions;
|
||
|
};
|
||
|
automation = slug: triggers: actions: (conditionalAutomation slug triggers [] actions);
|
||
|
|
||
|
lightOnOnMovement = room: opts: (conditionalAutomation
|
||
|
"${room}_movement_turn_on"
|
||
|
(map (e: helper.trigger.stateTo e "on") opts.triggers)
|
||
|
opts.conditions
|
||
|
(map (e: helper.action.turnOn e) opts.entities)
|
||
|
);
|
||
|
|
||
|
# switch off light when trigger entity switches to off and all given trigger
|
||
|
# entities are not in state "on" (prevents hassle with unavailable state)
|
||
|
lightOffAfterMovement = room: opts: (conditionalAutomation
|
||
|
"${room}_movement_turn_off"
|
||
|
(map (e: helper.trigger.stateTo e "off") opts.triggers)
|
||
|
(map (e: helper.condition.stateNot e "on") opts.triggers)
|
||
|
(map (e: helper.action.turnOff e) opts.entities)
|
||
|
);
|
||
|
|
||
|
outsideSleepingHoursCondition = [{
|
||
|
condition = "not";
|
||
|
conditions = [{
|
||
|
condition = "time";
|
||
|
after = "input_datetime.${entityLightSleepingHoursNotBefore}";
|
||
|
before = "input_datetime.${entityLightSleepingHoursNotAfter}";
|
||
|
}];
|
||
|
}];
|
||
|
withinDarkHoursCondition = [{
|
||
|
condition = "time";
|
||
|
after = "input_datetime.${entityLightDarkHoursNotBefore}";
|
||
|
before = "input_datetime.${entityLightDarkHoursNotAfter}";
|
||
|
}];
|
||
|
|
||
|
roomEntitiesMap = {
|
||
|
# should switch on when its dark outside but not when someone sleeps
|
||
|
flur = {
|
||
|
triggers = ["binary_sensor.pir_flur_1_occupancy" "binary_sensor.pir_flur_2_occupancy"];
|
||
|
entities = ["light.flur_deckenlicht"];
|
||
|
conditions = [] ++ withinDarkHoursCondition ++ outsideSleepingHoursCondition;
|
||
|
};
|
||
|
#kueche = {
|
||
|
# triggers = [];
|
||
|
# entites = [];
|
||
|
# conditions = [] ++ withinDarkHoursCondition;
|
||
|
#};
|
||
|
# should switch on every time
|
||
|
vorratsraum = {
|
||
|
triggers = ["binary_sensor.pir_vorratsraum_occupancy"];
|
||
|
entities = ["light.vorratsraum_deckenlicht"];
|
||
|
conditions = [];
|
||
|
};
|
||
|
};
|
||
|
in
|
||
|
{
|
||
|
input_datetime = {
|
||
|
"${entityLightDarkHoursNotBefore}" = {
|
||
|
name = "TIME Light: Dark hours start not before";
|
||
|
has_date = false;
|
||
|
has_time = true;
|
||
|
};
|
||
|
"${entityLightDarkHoursNotAfter}" = {
|
||
|
name = "TIME Light: Dark hours start not after";
|
||
|
has_date = false;
|
||
|
has_time = true;
|
||
|
};
|
||
|
"${entityLightSleepingHoursNotBefore}" = {
|
||
|
name = "TIME Light: Sleeping hours start not before";
|
||
|
has_date = false;
|
||
|
has_time = true;
|
||
|
};
|
||
|
"${entityLightSleepingHoursNotAfter}" = {
|
||
|
name = "TIME Light: Sleeping hours start not after";
|
||
|
has_date = false;
|
||
|
has_time = true;
|
||
|
};
|
||
|
};
|
||
|
|
||
|
automation = [] ++
|
||
|
(builtins.attrValues (builtins.mapAttrs (r: o: lightOnOnMovement r o) roomEntitiesMap)) ++
|
||
|
(builtins.attrValues (builtins.mapAttrs (r: o: lightOffAfterMovement r o) roomEntitiesMap));
|
||
|
}
|