diff --git a/configs/pepe/home-assistant.nix b/configs/pepe/home-assistant.nix index e2a21df..d0ade5b 100644 --- a/configs/pepe/home-assistant.nix +++ b/configs/pepe/home-assistant.nix @@ -47,11 +47,10 @@ in { "essential" ]; }; - input_boolean.bed_room_buttons.icon = "mdi:toggle-switch"; input_boolean.situation_toggle.icon = "mdi:toggle-switch"; - # room presents when I'm alone - input_boolean.alone.icon = "mdi:account"; + # enable presents_tracking + input_boolean.presents_tracking.icon = "mdi:account"; input_select.room_present = { icon = "mdi:map-marker"; options = [ @@ -97,9 +96,9 @@ in { to = "on"; }; condition = { - condition = "state"; - entity_id = "input_select.situation"; - state = situation; + condition = "template"; + value_template = + "{{ states( 'input_select.situation' ) == '${situation}' }}"; }; action = [ { @@ -118,31 +117,36 @@ in { { alias = "absents in ${presentsGroup} -> turn lights off ${turnOffGroup} in ${situation}"; - trigger = { + trigger = map (minutes: { platform = "state"; entity_id = "group.${presentsGroup}"; from = "on"; to = "off"; - for.seconds = 2 * 60; - }; + for.seconds = minutes * 60; + }) [ 2 5 10 15 20 25 30 35 40 45 50 55 ]; condition = { condition = "and"; conditions = [ - { - condition = "template"; - value_template = - "{{ states( 'input_select.room_present' ) != '${presentsGroup}' }}"; - } - { - condition = "template"; - value_template = - "{{ states( 'input_boolean.alone' ) == 'on' }}"; - } { condition = "template"; value_template = "{{ states( 'input_select.situation' ) == '${situation}' }}"; } + { + condition = "or"; + conditions = [ + { + condition = "template"; + value_template = + "{{ states( 'input_select.room_present' ) != '${presentsGroup}' }}"; + } + { + condition = "template"; + value_template = + "{{ states( 'input_boolean.presents_tracking' ) == 'off' }}"; + } + ]; + } ]; }; action = [ @@ -156,6 +160,32 @@ in { } ]; } + { + alias = + "finally absents in ${presentsGroup} -> turn lights off ${turnOffGroup} in ${situation}"; + trigger = [{ + platform = "state"; + entity_id = "group.${presentsGroup}"; + from = "on"; + to = "off"; + for.seconds = 60 * 60; + }]; + condition = { + 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: [ { @@ -179,6 +209,92 @@ in { ]) allRooms; in (flatMap roomPresents onOffArguments); + roomPresentSwitches = let + + allRoomPresentGroups = map (name: "${name}_present") allRooms; + + foo = + # group to determine presents + presentGroup: + let + otherRoomPresentGroups = + builtins.filter (name: name != presentGroup) allRoomPresentGroups; + in [ + { + alias = + "select ${presentGroup} if all other rooms are longer not present/active"; + trigger = { + platform = "state"; + entity_id = "group.${presentGroup}"; + from = "on"; + to = "off"; + for.seconds = (2 * 60) - 15; + }; + condition = { + condition = "and"; + conditions = flatMap (otherRoomPresent: { + condition = "state"; + entity_id = "group.${otherRoomPresent}"; + state = "off"; + for.seconds = 1 * 60; + }) otherRoomPresentGroups; + }; + action = [{ + service = "input_select.select_option"; + data = { + entity_id = "input_select.room_present"; + option = presentGroup; + }; + }]; + } + { + alias = + "select ${presentGroup} if no other room is present/active but ${presentGroup} is"; + trigger = { + platform = "time_pattern"; + minutes = "/1"; + }; + condition = { + condition = "and"; + conditions = [{ + condition = "state"; + entity_id = "group.${presentGroup}"; + state = "on"; + for.seconds = 1 * 60; + }] ++ (flatMap (otherRoomPresent: { + condition = "state"; + entity_id = "group.${otherRoomPresent}"; + state = "off"; + for.seconds = 1 * 60; + }) otherRoomPresentGroups); + }; + action = [{ + service = "input_select.select_option"; + data = { + entity_id = "input_select.room_present"; + option = presentGroup; + }; + }]; + } + { + alias = "select ${presentGroup} when entering the room"; + trigger = { + platform = "state"; + entity_id = "group.${presentGroup}"; + from = "off"; + to = "on"; + }; + action = [{ + service = "input_select.select_option"; + data = { + entity_id = "input_select.room_present"; + option = presentGroup; + }; + }]; + } + ]; + in flatMap foo allRoomPresentGroups; + # change light scene in rooms when changing situation # --------------------------------------------------- situationSwitches = let @@ -308,7 +424,7 @@ in { ]) allRooms; in flatMap situationSwitch allArguments; - in situationSwitches ++ roomSwitches ++ [ + in situationSwitches ++ roomSwitches ++ roomPresentSwitches ++ [ # control situation with buttons { @@ -427,6 +543,17 @@ in { ]; }; + all_room_presents = { + name = "Alle Anwesenheits Raum Sensoren"; + entities = [ + "group.floor_room_present" + "group.bed_room_present" + "group.living_room_present" + "group.kitchen_room_present" + "group.bath_room_present" + ]; + }; + # overview # -------- all_sensors = { @@ -440,7 +567,7 @@ in { entities = [ "input_select.situation" "input_select.room_present" - "input_boolean.alone" + "input_boolean.presents_tracking" ]; }; view_overview = { diff --git a/configs/pepe/home-assistant/zigbee2mqtt/buttons.nix b/configs/pepe/home-assistant/zigbee2mqtt/buttons.nix index c9d814b..1537691 100644 --- a/configs/pepe/home-assistant/zigbee2mqtt/buttons.nix +++ b/configs/pepe/home-assistant/zigbee2mqtt/buttons.nix @@ -11,20 +11,20 @@ let allDevices = { "button_1" = { id = "0x00158d0002b04f65"; - # states = { click = "input_boolean.living_room_button"; } # input state to hold (optional) (default "input_boolean.single_${name}") - groups = [ "living_room" "living_room_present" ]; + groups = [ "living_room" ]; + states.single = "input_boolean.presents_tracking"; states.double = "input_boolean.situation_toggle"; }; "button_2" = { id = "0x00158d0002b04f09"; - groups = [ "bed_room" "bed_room_present" ]; - states.single = "input_boolean.bed_room_buttons"; + groups = [ "bed_room" ]; + states.single = "input_boolean.presents_tracking"; states.double = "input_boolean.situation_toggle"; }; "button_3" = { id = "0x00158d0002b00e04"; - groups = [ "bed_room" "bed_room_present" ]; - states.single = "input_boolean.bed_room_buttons"; + groups = [ "bed_room" ]; + states.single = "input_boolean.presents_tracking"; states.double = "input_boolean.situation_toggle"; }; };