summaryrefslogtreecommitdiffstats
path: root/lass/2configs/hass/lib.nix
diff options
context:
space:
mode:
authormakefu <github@syntax-fehler.de>2020-05-21 08:38:16 +0200
committermakefu <github@syntax-fehler.de>2020-05-21 08:38:16 +0200
commitd027d1d5316371280221870a9c05f2b35fb437ac (patch)
treee4c8e2a580d4d7d2d01f440ec837bed707db0c64 /lass/2configs/hass/lib.nix
parentab0906f7aab8a49186e64c4942a516686a132558 (diff)
parentcf5b35b2df428495f5bf4dd89e3cab190f069080 (diff)
Merge remote-tracking branch 'lass/master'
Diffstat (limited to 'lass/2configs/hass/lib.nix')
-rw-r--r--lass/2configs/hass/lib.nix196
1 files changed, 188 insertions, 8 deletions
diff --git a/lass/2configs/hass/lib.nix b/lass/2configs/hass/lib.nix
index c69f5e887..555cfae28 100644
--- a/lass/2configs/hass/lib.nix
+++ b/lass/2configs/hass/lib.nix
@@ -1,18 +1,196 @@
-{
+{ lib, ... }:
+rec {
lights = {
- bett = "light.0x0017880106ed3bd8_light";
- essen = "light.0x0017880108327622_light";
- arbeit = "light.0x0017880106ee2865_light";
- nass = "light.0x00178801082e9f2f_light";
+ bett = "0x0017880106ed3bd8";
+ essen = "0x0017880108327622";
+ arbeit = "0x0017880106ee2865";
+ nass = "0x00178801082e9f2f";
+ };
+
+ switches = {
+ dimmer = {
+ bett = "0x00178801086ac38c";
+ essen = "0x00178801086ad1fb";
+ nass = "0x00178801086ac373";
+ };
};
sensors = {
- bett = "0x00178801086ac38c";
+ movement = {
+ essen = "0x0017880106f772f2";
+ nass = "0x0017880106f77f30";
+ };
+ };
+
+ friendly_names =
+ lib.mapAttrs' (n: v: lib.nameValuePair "light.${v}_light" { friendly_name = "l.${n}"; }) lights //
+ lib.mapAttrs' (n: v: lib.nameValuePair "binary_sensor.${v}_update_available" { friendly_name = "s.${n}_up"; }) switches.dimmer //
+ lib.mapAttrs' (n: v: lib.nameValuePair "binary_sensor.${v}_update_available" { friendly_name = "i.${n}_up"; }) sensors.movement //
+ lib.mapAttrs' (n: v: lib.nameValuePair "binary_sensor.${v}_update_available" { friendly_name = "l.${n}_up"; }) lights //
+ lib.mapAttrs' (n: v: lib.nameValuePair "sensor.${v}_linkquality" { friendly_name = "s.${n}_link"; }) switches.dimmer //
+ lib.mapAttrs' (n: v: lib.nameValuePair "sensor.${v}_linkquality" { friendly_name = "i.${n}_link"; }) sensors.movement //
+ lib.mapAttrs' (n: v: lib.nameValuePair "sensor.${v}_linkquality" { friendly_name = "l.${n}_link"; }) lights //
+ lib.mapAttrs' (n: v: lib.nameValuePair "sensor.${v}_battery" { friendly_name = "s.${n}_bat"; }) switches.dimmer //
+ lib.mapAttrs' (n: v: lib.nameValuePair "sensor.${v}_battery" { friendly_name = "i.${n}_bat"; }) sensors.movement //
+ lib.mapAttrs' (n: v: lib.nameValuePair "sensor.${v}_action" { friendly_name = "s.${n}_act"; }) switches.dimmer //
+ lib.mapAttrs' (n: v: lib.nameValuePair "binary_sensor.${v}_occupancy" { friendly_name = "i.${n}_move"; }) sensors.movement //
+ lib.mapAttrs' (n: v: lib.nameValuePair "binary_sensor.${v}_occupancy" { friendly_name = "i.${n}_move"; }) sensors.movement //
+ lib.mapAttrs' (n: v: lib.nameValuePair "sensor.${v}_temperature" { friendly_name = "i.${n}_heat"; }) sensors.movement //
+ lib.mapAttrs' (n: v: lib.nameValuePair "sensor.${v}_temperature" { friendly_name = "i.${n}_heat"; }) sensors.movement //
+ lib.mapAttrs' (n: v: lib.nameValuePair "sensor.${v}_illuminance" { friendly_name = "i.${n}_lux"; }) sensors.movement //
+ lib.mapAttrs' (n: v: lib.nameValuePair "sensor.${v}_illuminance" { friendly_name = "i.${n}_lux"; }) sensors.movement //
+ {};
+
+ detect_movement = sensor: light: delay:
+ let
+ id = "${sensor}_${light}";
+ sensor_ = "binary_sensor.${sensor}_occupancy";
+ light_ = "light.${light}_light";
+ in {
+ input_boolean."${id}" = {
+ };
+ timer."${id}" = {
+ duration = delay;
+ };
+ automation = [
+ # {
+ # alias = "debug detect_movement";
+ # trigger = {
+ # platform = "state";
+ # entity_id = sensor_;
+ # };
+ # action = [
+ # {
+ # service = "system_log.write";
+ # data_template = {
+ # message = "XXXXXXXXXXXXXXXXXXXXXX {{ states('input_boolean.${sensor}_${light}_triggered') == 'on' }}";
+ # #message = "XXXXXXXXXXXXXXXXXXXXXX {{ state_attr('trigger.to_state.state', 'illuminance') }}";
+ # };
+ # }
+ # ];
+ # }
+ {
+ alias = "movement reset timer ${id}";
+ hide_entity = true;
+ trigger = {
+ platform = "state";
+ entity_id = sensor_;
+ from = "off";
+ to = "on";
+ };
+ action = [
+ {
+ service = "timer.cancel";
+ data_template.entity_id = "timer.${id}";
+ }
+ ];
+ }
+ {
+ alias = "movement on ${id}";
+ # hide_entity = true;
+ trigger = {
+ platform = "state";
+ entity_id = "binary_sensor.${sensor}_occupancy";
+ from = "off";
+ to = "on";
+ };
+ condition = {
+ condition = "and";
+ conditions = [
+ {
+ condition = "template";
+ value_template = "{{ trigger.to_state.attributes.illuminance < 13000 }}";
+ }
+ {
+ condition = "template";
+ value_template = "{{ states('${light_}') == 'off' }}";
+ }
+ ];
+ };
+ action = [
+ {
+ service = "light.turn_on";
+ data_template = {
+ entity_id = light_;
+ brightness = "100";
+ };
+ }
+ { delay = "0:00:02"; }
+ {
+ service = "input_boolean.turn_on";
+ data_template.entity_id = "input_boolean.${id}";
+ }
+ ];
+ }
+ {
+ alias = "movement off ${id}";
+ hide_entity = true;
+ trigger = {
+ platform = "state";
+ entity_id = sensor_;
+ from = "on";
+ to = "off";
+ };
+ condition = {
+ condition = "template";
+ value_template = "{{ states('input_boolean.${id}') == 'on' }}";
+ };
+ action = [
+ {
+ service = "timer.start";
+ entity_id = "timer.${id}";
+ }
+ ];
+ }
+ {
+ alias = "movement override ${id}";
+ hide_entity = true;
+ trigger = {
+ platform = "state";
+ entity_id = light_;
+ };
+ action = [
+ {
+ service = "input_boolean.turn_off";
+ data_template.entity_id = "input_boolean.${id}";
+ }
+ {
+ service = "system_log.write";
+ data_template = {
+ message = "XXXXXXXXXXXXXXXXXXXXXX {{ trigger }}";
+ };
+ }
+ ];
+ }
+ {
+ alias = "movement expired ${id}";
+ hide_entity = true;
+ trigger = {
+ platform = "event";
+ event_type = "timer.finished";
+ event_data.entity_id = "timer.${id}";
+ };
+ action = [
+ {
+ service = "light.turn_off";
+ data_template = {
+ entity_id = light_;
+ };
+ }
+ {
+ service = "input_boolean.turn_off";
+ data_template.entity_id = "input_boolean.${id}";
+ }
+ ];
+ }
+ ];
};
lightswitch = switch: light: {
automation = [
{
+ alias = "lightswitch ${switch} turn on light ${light}";
+ hide_entity = "true";
trigger = {
platform = "mqtt";
topic = "zigbee/${switch}";
@@ -47,13 +225,15 @@
{
service = "light.turn_on";
data_template = {
- entity_id = light;
+ entity_id = "light.${light}_light";
brightness = "{{ trigger.payload_json.brightness }}";
};
}
];
}
{
+ alias = "lightswitch ${switch} turn off light ${light}";
+ hide_entity = "true";
trigger = {
platform = "mqtt";
topic = "zigbee/${switch}";
@@ -74,7 +254,7 @@
action = {
service = "light.turn_off";
data_template = {
- entity_id = light;
+ entity_id = "light.${light}_light";
};
};
}