From f2a913e8624d051ff15451ef0980fa7965b0e0a2 Mon Sep 17 00:00:00 2001 From: Ingolf Wagner Date: Sun, 3 May 2020 18:22:28 +0200 Subject: [PATCH] home-assistant: cleanup and motion sensor movement --- configs/pepe/home-assistant.nix | 362 ++++++++++-------- .../home-assistant/zigbee2mqtt/motion.nix | 2 +- 2 files changed, 193 insertions(+), 171 deletions(-) diff --git a/configs/pepe/home-assistant.nix b/configs/pepe/home-assistant.nix index 7548692..ec3f2ed 100644 --- a/configs/pepe/home-assistant.nix +++ b/configs/pepe/home-assistant.nix @@ -67,172 +67,210 @@ in { automation = let + # a help function that flattens even 2 times + flatMap = function: list: lib.flatten (map function (lib.flatten list)); + allRooms = [ "living_room" "floor_room" "bath_room" "bed_room" "kitchen_room" ]; - roomPresents = { - # group of the room that should be turned on - roomGroup, - # group of the room that should be turned off - roomOffGroup ? roomGroup, - # group of the room that that indicates presents in the room - presentsGroup, - # global situation - situation, - # brightness for dimable lights - brightness ? 255 }: [ - { - alias = "presents -> turn on ${roomGroup} lights in ${situation}"; - trigger = { - platform = "state"; - entity_id = "group.${presentsGroup}"; - from = "off"; - to = "on"; - }; - condition = { - condition = "state"; - entity_id = "input_select.situation"; - state = situation; - }; - action = [ - { - service = "switch.turn_on"; - data.entity_id = "group.${roomGroup}"; - } - { - 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 = [ + # presents <-> absents : light settings + # ------------------------------------- + roomSwitches = let + roomPresents = { + # group of the room that should be turned on + roomGroup, + # group of the room that should be turned off + turnOffGroup ? roomGroup, + # group of the room that that indicates presents in the room + presentsGroup, + # global situation + situation, + # brightness for dimable lights + brightness ? 255 }: [ + { + alias = + "presents in ${presentsGroup} -> turn lights on in ${roomGroup} for ${situation}"; + trigger = { + platform = "state"; + entity_id = "group.${presentsGroup}"; + from = "off"; + to = "on"; + }; + condition = { + condition = "state"; + entity_id = "input_select.situation"; + state = situation; + }; + action = [ { - condition = "template"; - value_template = - "{{ states( 'input_select.room_present' ) != '${presentsGroup}' }}"; + service = "switch.turn_on"; + data.entity_id = "group.${roomGroup}"; } { - condition = "template"; - value_template = - "{{ states( 'input_boolean.alone' ) == 'on' }}"; - } - { - condition = "template"; - value_template = - "{{ states( 'input_select.situation' ) == '${situation}' }}"; + service = "light.turn_on"; + data = { + entity_id = "group.${roomGroup}"; + brightness = brightness; + }; } ]; - - }; - action = [ - { - service = "switch.turn_off"; - data.entity_id = "group.${roomOffGroup}"; - } - { - service = "light.turn_off"; - data.entity_id = "group.${roomOffGroup}"; - } - ]; - } - ]; - - situationSwitch = { fromRoomGroup, toRoomGroup, presentsGroup - , fromSituation, toSituation, - # brightness for dimable lights - brightness ? 255 }: [ + } + { + alias = + "absents in ${presentsGroup} -> turn lights off ${turnOffGroup} in ${situation}"; + trigger = { + platform = "state"; + entity_id = "group.${presentsGroup}"; + from = "on"; + to = "off"; + for.seconds = 2 * 60; + }; + 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}' }}"; + } + ]; + }; + action = [ + { + service = "switch.turn_off"; + data.entity_id = "group.${turnOffGroup}"; + } + { + service = "light.turn_off"; + data.entity_id = "group.${turnOffGroup}"; + } + ]; + } + ]; + onOffArguments = flatMap (name: [ { - 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; - }; - } - ]; + roomGroup = "${name}_lights"; + presentsGroup = "${name}_present"; + situation = "dark"; } { - 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}"; - } - ]; + roomGroup = "${name}_essential"; + turnOffGroup = "${name}_lights"; + presentsGroup = "${name}_present"; + situation = "essential"; + brightness = 30; } - ]; + ]) allRooms; + in (flatMap roomPresents onOffArguments); - in lib.flatten (map roomPresents (lib.flatten (map (name: [ - { - roomGroup = "${name}_lights"; - presentsGroup = "${name}_present"; - situation = "dark"; - } - { - roomGroup = "${name}_essential"; - roomOffGroup = "${name}_lights"; - presentsGroup = "${name}_present"; - situation = "essential"; - brightness = 30; - } - ]) allRooms))) ++ [ + # change light scene in rooms when changing situation + # --------------------------------------------------- + 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 ? 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 { @@ -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 create_room = { name, description }: { diff --git a/configs/pepe/home-assistant/zigbee2mqtt/motion.nix b/configs/pepe/home-assistant/zigbee2mqtt/motion.nix index f519849..0bf67b7 100644 --- a/configs/pepe/home-assistant/zigbee2mqtt/motion.nix +++ b/configs/pepe/home-assistant/zigbee2mqtt/motion.nix @@ -10,7 +10,7 @@ let }; "motion_sensor_2" = { id = "0x00158d0002f9a6b8"; - groups = [ "kitchen_room" "kitchen_room_present" ]; + groups = [ "bed_room" "bed_room_present" ]; }; "motion_sensor_3" = { id = "0x00158d0002f04522";