pepe: add home-assistant button automation

This commit is contained in:
Ingolf Wagner 2020-04-12 21:42:11 +02:00
parent aad74d7393
commit 957bf38595
No known key found for this signature in database
GPG key ID: 76BF5F1928B9618B
4 changed files with 54 additions and 39 deletions

View file

@ -115,13 +115,13 @@ in {
control = "hidden"; control = "hidden";
name = "Not Used"; name = "Not Used";
view = false; view = false;
entities = [ ];
}; };
}; };
script.turn_all_off.sequence = [ ]; script.turn_all_off.sequence = [ ];
script.turn_all_lights_off.sequence = [ ];
script.turn_all_on.sequence = [ ];
}; };

View file

@ -70,7 +70,8 @@
}; };
}; };
group.tv.entities = [ "script.watch_tv" "script.stop_watch_tv" ]; group.tv.entities =
[ "script.watch_tv" "script.stop_watch_tv" "script.test_display" ];
}; };

View file

@ -102,19 +102,19 @@ in {
# ----------------- # -----------------
script.turn_all_off.sequence = [ script.turn_all_off.sequence = [
{ {
alias = "turn off sonoff"; alias = "turn off sonoff lights";
service = "switch.turn_off"; service = "switch.turn_off";
data.entity_id = "group.all_lights"; data.entity_id = "group.all_lights";
} }
{ {
alias = "turn off sonoff"; alias = "turn off tv";
service = "switch.turn_off"; service = "switch.turn_off";
data.entity_id = "group.tv"; data.entity_id = "group.tv";
} }
]; ];
script.turn_all_on.sequence = [{ script.turn_all_lights_off.sequence = [{
alias = "turn on all lights"; alias = "turn off sonoff lights";
service = "switch.turn_on"; service = "switch.turn_off";
data.entity_id = "group.all_lights"; data.entity_id = "group.all_lights";
}]; }];
@ -125,16 +125,15 @@ in {
flatSonoffSwitches = flatSonoffSwitches =
lib.mapAttrsToList (name: values: { name = name; } // values) lib.mapAttrsToList (name: values: { name = name; } // values)
sonoffSwitches; sonoffSwitches;
allRooms' =
lib.groupBy ({ room ? "unknown", ... }: room) flatSonoffSwitches;
allRooms = lib.mapAttrs (_: devices: {
entities = map ({ name, ... }: "switch.${name}") devices;
}) allRooms';
allLights' = lib.filter ({ type ? "unknown", ... }: type == "light") allLights' = lib.filter ({ type ? "unknown", ... }: type == "light")
flatSonoffSwitches; flatSonoffSwitches;
allLights = { allLights = {
all_lights.entities = map ({ name, ... }: "switch.${name}") allLights'; all_lights.entities = map ({ name, ... }: "switch.${name}") allLights';
}; };
allRooms' = lib.groupBy ({ room ? "unknown", ... }: room) allLights';
allRooms = lib.mapAttrs (_: devices: {
entities = map ({ name, ... }: "switch.${name}") devices;
}) allRooms';
in allRooms // allLights // { in allRooms // allLights // {
# this is a quick hack. it should not be the norm to define this stuff down here # this is a quick hack. it should not be the norm to define this stuff down here
tv = { entities = builtins.map toSwitch [ "pal05" ]; }; tv = { entities = builtins.map toSwitch [ "pal05" ]; };

View file

@ -262,8 +262,23 @@ in {
# todo : this belongs in the home-assistant.nix # todo : this belongs in the home-assistant.nix
automation = let automation = let
lights = map (button: { # hold, turn off the lights everywhere
alias = "Toggle all lights, on click"; all_lights_off = map (button: {
alias = "turn all lights off on holding a button";
trigger = {
platform = "mqtt";
topic = "zigbee2mqtt/${button}";
};
condition = {
condition = "template";
value_template = ''{{ "hold" == trigger.payload_json.action }}'';
};
action = { service = "script.turn_all_off"; };
}) [ "button_1" "button_2" "button_3" ];
# one click, turn on the light in the room
room_lights_on = map ({ button, room }: {
alias = "turn on lights in ${room}, on ${button} click";
trigger = { trigger = {
platform = "mqtt"; platform = "mqtt";
topic = "zigbee2mqtt/${button}"; topic = "zigbee2mqtt/${button}";
@ -273,32 +288,32 @@ in {
value_template = ''{{ "single" == trigger.payload_json.click }}''; value_template = ''{{ "single" == trigger.payload_json.click }}'';
}; };
action = { action = {
service = "switch.toggle"; service = "switch.turn_on";
entity_id = "group.all_lights"; entity_id = "group.${room}";
}; };
}) [ "button_1" "button_2" "button_3" ]; }) [
{
button = "button_1";
room = "living_room";
}
{
button = "button_2";
room = "bed_room";
}
{
button = "button_3";
room = "bed_room";
}
];
#mpd = map (button: { # todo : double click, turn on the light in this room and every where else turn it off
# alias = "Toggle mpd, on double click";
# trigger = {
# platform = "mqtt";
# topic = "zigbee2mqtt/${button}";
# };
# condition = {
# condition = "template";
# value_template = ''{{ "double" == trigger.payload_json.click }}'';
# };
# action = {
# service = "media_player.toggle";
# # todo use a group here
# entity_id = "media_player.mpd";
# };
#}) [ "button_1" "button_2" "button_3" ];
# kitchen light automation
# ------------------------
# https://www.home-assistant.io/integrations/binary_sensor.template/ # https://www.home-assistant.io/integrations/binary_sensor.template/
kitchen_lights = map (motion: [ kitchen_lights = map (motion: [
{ {
alias = "turn on lights, on motion"; alias = "turn on kitchen lights, on motion detected";
trigger = { trigger = {
platform = "state"; platform = "state";
entity_id = "binary_sensor.${motion}"; entity_id = "binary_sensor.${motion}";
@ -307,11 +322,11 @@ in {
}; };
action = { action = {
service = "switch.turn_on"; service = "switch.turn_on";
entity_id = "group.kitchen"; entity_id = "group.kitchen_room";
}; };
} }
{ {
alias = "turn off lights, on no motion"; alias = "turn off kitchen lights, on motion clear";
trigger = { trigger = {
platform = "state"; platform = "state";
entity_id = "binary_sensor.${motion}"; entity_id = "binary_sensor.${motion}";
@ -320,12 +335,12 @@ in {
}; };
action = { action = {
service = "switch.turn_off"; service = "switch.turn_off";
entity_id = "group.kitchen"; entity_id = "group.kitchen_room";
}; };
} }
]) [ "motion_sensor_1" ]; ]) [ "motion_sensor_1" ];
in lib.flatten (lights ++ kitchen_lights); in lib.flatten (kitchen_lights ++ all_lights_off ++ room_lights_on);
# click = double => music an aus # click = double => music an aus