From 2c9923264e318ad83ab9ceb4a13e47112dff0d4f Mon Sep 17 00:00:00 2001 From: makefu Date: Fri, 27 Aug 2021 22:45:16 +0200 Subject: ma ham: add keller button logic --- .../2configs/home/ham/automation/fenster_auf.nix | 15 ++++ .../home/ham/automation/keller_buttons.nix | 7 ++ .../2configs/home/ham/automation/light_buttons.nix | 86 ++-------------------- 3 files changed, 29 insertions(+), 79 deletions(-) create mode 100644 makefu/2configs/home/ham/automation/keller_buttons.nix (limited to 'makefu/2configs/home') diff --git a/makefu/2configs/home/ham/automation/fenster_auf.nix b/makefu/2configs/home/ham/automation/fenster_auf.nix index b24f6445..fa2052be 100644 --- a/makefu/2configs/home/ham/automation/fenster_auf.nix +++ b/makefu/2configs/home/ham/automation/fenster_auf.nix @@ -15,6 +15,12 @@ let for.minutes = min; } ]; + condition = [ + { condition = "state"; + entity_id = "input_boolean.ist_sommer"; + state = "off"; + } + ]; action = [ { @@ -61,10 +67,19 @@ let }; in { services.home-assistant.config = { + sensor = [ + { platform = "season"; type = "meteorological";} + ]; + input_boolean = { badezimmerfenster_lang_offen.name = "Badezimmer lange offen"; duschfenster_lang_offen.name = "Duschfenster lange offen"; + ist_sommer = { + name = "Es ist Sommer"; + initial = true; # TODO + }; }; + automation = [ (fenster_geschlossen_lang "Badezimmerfenster" "binary_sensor.badezimmer_fenster_contact") (fenster_geschlossen_lang "Duschfenster" "binary_sensor.dusche_fenster_contact") diff --git a/makefu/2configs/home/ham/automation/keller_buttons.nix b/makefu/2configs/home/ham/automation/keller_buttons.nix new file mode 100644 index 00000000..598dc7df --- /dev/null +++ b/makefu/2configs/home/ham/automation/keller_buttons.nix @@ -0,0 +1,7 @@ +let + inherit (../lib) btn_cycle_light; +in { + services.home-assistant.config.automation = [ + (btn_cycle_light "light.keller_osram" "keller_btn1" 128) + ]; +} diff --git a/makefu/2configs/home/ham/automation/light_buttons.nix b/makefu/2configs/home/ham/automation/light_buttons.nix index 89caf1a4..b3c9ecdb 100644 --- a/makefu/2configs/home/ham/automation/light_buttons.nix +++ b/makefu/2configs/home/ham/automation/light_buttons.nix @@ -1,80 +1,7 @@ let - btn_state = light: btn: halfbright: - let - maxbright = 255; - transition = 0.2; # seconds - in - # this function implements a simple state machine based on the state and brightness of the light (light must support brightness - { - alias = "Cycle through states of ${light} via button ${btn}"; - trigger = { - platform = "state"; - entity_id = "sensor.${btn}_click"; - to = "single"; - }; - action = { - choose = [ - { - # state 0: off to half - conditions = { - condition = "template"; - value_template = ''{{ states("${light}") == "off" }}''; - }; - sequence = [ - { - service = "light.turn_on"; - data = { - entity_id = light; - brightness = halfbright; - }; - } - ]; - } - { - # state 1: half to full - conditions = { - condition = "template"; - value_template = ''{{ states('${light}') == 'on' and ( ${toString (halfbright - 1)} <= state_attr("${light}","brightness") <= ${toString (halfbright + 1)})}}''; - }; - sequence = [ - { - service = "light.turn_on"; - data = { - entity_id = light; - brightness = maxbright; - }; - } - ]; - } - { - # state 2: full to off - conditions = { - condition = "template"; - # TODO: it seems like the devices respond with brightness-1 , maybe off-by-one somewhere? - value_template = ''{{ states("${light}") == "on" and state_attr("${light}","brightness") >= ${toString (maxbright - 1)}}}''; - }; - sequence = [ - { - service = "light.turn_off"; - data = { - entity_id = light; - }; - } - ]; - } - ]; - # default: on to off - # this works because state 0 checks for "state == off" - default = [{ - service = "light.turn_off"; - data = { - entity_id = light; - }; - }]; - }; - }; - turn_off_all = btn: + inherit (../lib) btn_cycle_light; + turn_off_all = btn: #lights: { alias = "Turn of all lights via ${btn} double click"; trigger = { @@ -84,14 +11,15 @@ let }; action = { service = "light.turn_off"; + #entity_id = lights; entity_id = "all"; }; }; in { services.home-assistant.config.automation = [ - # (btn_state "light.arbeitszimmerbeleuchtung" "arbeitszimmer_btn1") - (btn_state "light.schlafzimmer_komode_osram" "schlafzimmer_btn2" 128) - # (btn_state "light.wohnzimmerbeleuchtung" "wohnzimmer_btn3") - (turn_off_all "schlafzimmer_btn2") + # (btn_cycle_light "light.arbeitszimmerbeleuchtung" "arbeitszimmer_btn1") + (btn_cycle_light "light.schlafzimmer_komode_osram" "schlafzimmer_btn2" 128) + # (btn_cycle_light "light.wohnzimmerbeleuchtung" "wohnzimmer_btn3") + (turn_off_all "schlafzimmer_btn2" ) ]; } -- cgit v1.2.3 From 6fa5b38bed3f8e73d6f86a844ef775735a3cad29 Mon Sep 17 00:00:00 2001 From: makefu Date: Tue, 31 Aug 2021 15:14:37 +0200 Subject: ma ham: consolidate button management --- .../home/ham/automation/keller_buttons.nix | 7 -- .../home/ham/automation/ladestecker_timer.nix | 24 +++++++ .../2configs/home/ham/automation/light_buttons.nix | 4 +- makefu/2configs/home/ham/default.nix | 2 +- makefu/2configs/home/ham/lib/default.nix | 76 ++++++++++++++++++++++ 5 files changed, 104 insertions(+), 9 deletions(-) delete mode 100644 makefu/2configs/home/ham/automation/keller_buttons.nix create mode 100644 makefu/2configs/home/ham/automation/ladestecker_timer.nix (limited to 'makefu/2configs/home') diff --git a/makefu/2configs/home/ham/automation/keller_buttons.nix b/makefu/2configs/home/ham/automation/keller_buttons.nix deleted file mode 100644 index 598dc7df..00000000 --- a/makefu/2configs/home/ham/automation/keller_buttons.nix +++ /dev/null @@ -1,7 +0,0 @@ -let - inherit (../lib) btn_cycle_light; -in { - services.home-assistant.config.automation = [ - (btn_cycle_light "light.keller_osram" "keller_btn1" 128) - ]; -} diff --git a/makefu/2configs/home/ham/automation/ladestecker_timer.nix b/makefu/2configs/home/ham/automation/ladestecker_timer.nix new file mode 100644 index 00000000..8e877129 --- /dev/null +++ b/makefu/2configs/home/ham/automation/ladestecker_timer.nix @@ -0,0 +1,24 @@ +let + relay = "switch.terrasse_plug_relay"; + timeout = "300"; +in { + services.home-assistant.config.automation = [ + { alias = "Always turn off Charging station after ${toString (timeout)}m"; + trigger = [ + { + platform = "state"; + entity_id = relay; + to = "on"; + for.minutes = timeout; + } + ]; + action = + [ + { + service = "homeassistant.turn_off"; + entity_id = [ relay ]; + } + ]; + } + ]; +} diff --git a/makefu/2configs/home/ham/automation/light_buttons.nix b/makefu/2configs/home/ham/automation/light_buttons.nix index b3c9ecdb..62fc87bb 100644 --- a/makefu/2configs/home/ham/automation/light_buttons.nix +++ b/makefu/2configs/home/ham/automation/light_buttons.nix @@ -1,6 +1,6 @@ let - inherit (../lib) btn_cycle_light; + inherit (import ../lib) btn_cycle_light; turn_off_all = btn: #lights: { alias = "Turn of all lights via ${btn} double click"; @@ -19,6 +19,8 @@ in { services.home-assistant.config.automation = [ # (btn_cycle_light "light.arbeitszimmerbeleuchtung" "arbeitszimmer_btn1") (btn_cycle_light "light.schlafzimmer_komode_osram" "schlafzimmer_btn2" 128) + + (btn_cycle_light "light.keller_osram" "keller_btn1" 128) # (btn_cycle_light "light.wohnzimmerbeleuchtung" "wohnzimmer_btn3") (turn_off_all "schlafzimmer_btn2" ) ]; diff --git a/makefu/2configs/home/ham/default.nix b/makefu/2configs/home/ham/default.nix index 79f26a05..cb42f32a 100644 --- a/makefu/2configs/home/ham/default.nix +++ b/makefu/2configs/home/ham/default.nix @@ -1,5 +1,4 @@ { pkgs, lib, config, ... }: - # Ideas: ## wake-on-lan server ## @@ -33,6 +32,7 @@ in { ./automation/firetv_restart.nix ./automation/light_buttons.nix ./automation/wohnzimmer_rf_fernbedienung.nix + ./automation/ladestecker_timer.nix #./automation/giesskanne.nix ./automation/pflanzen_giessen_erinnerung.nix #./automation/urlaub.nix diff --git a/makefu/2configs/home/ham/lib/default.nix b/makefu/2configs/home/ham/lib/default.nix index 45c86138..75be5273 100644 --- a/makefu/2configs/home/ham/lib/default.nix +++ b/makefu/2configs/home/ham/lib/default.nix @@ -40,5 +40,81 @@ in entity = "firetv"; }; }; + zigbee.prefix = "/ham/zigbee"; + + btn_cycle_light = light: btn: halfbright: + let + maxbright = 255; + transition = 0.2; # seconds + in + # this function implements a simple state machine based on the state and brightness of the light (light must support brightness + { + alias = "Cycle through states of ${light} via button ${btn}"; + trigger = { + platform = "state"; + entity_id = "sensor.${btn}_click"; + to = "single"; + }; + action = { + choose = [ + { + # state 0: off to half + conditions = { + condition = "template"; + value_template = ''{{ states("${light}") == "off" }}''; + }; + sequence = [ + { + service = "light.turn_on"; + data = { + entity_id = light; + brightness = halfbright; + }; + } + ]; + } + { + # state 1: half to full + conditions = { + condition = "template"; + value_template = ''{{ states('${light}') == 'on' and ( ${toString (halfbright - 1)} <= state_attr("${light}","brightness") <= ${toString (halfbright + 1)})}}''; + }; + sequence = [ + { + service = "light.turn_on"; + data = { + entity_id = light; + brightness = maxbright; + }; + } + ]; + } + { + # state 2: full to off + conditions = { + condition = "template"; + # TODO: it seems like the devices respond with brightness-1 , maybe off-by-one somewhere? + value_template = ''{{ states("${light}") == "on" and state_attr("${light}","brightness") >= ${toString (maxbright - 1)}}}''; + }; + sequence = [ + { + service = "light.turn_off"; + data = { + entity_id = light; + }; + } + ]; + } + ]; + # default: on to off + # this works because state 0 checks for "state == off" + default = [{ + service = "light.turn_off"; + data = { + entity_id = light; + }; + }]; + }; + }; } -- cgit v1.2.3