nixos-config/configs/pepe/home-assistant.nix

498 lines
13 KiB
Nix
Raw Normal View History

2020-04-08 16:43:09 +02:00
{ pkgs, config, lib, ... }:
let unstablePkgs = import <nixpkgs-unstable> { };
in {
imports = [
#./home-assistant/mpd.nix
#./home-assistant/timer.nix
2020-06-01 04:12:15 +02:00
./home-assistant/light-control.nix
2020-04-17 20:59:12 +02:00
./home-assistant/chaospott.nix
2020-04-10 11:36:58 +02:00
./home-assistant/kodi.nix
2020-04-12 13:36:15 +02:00
./home-assistant/mqtt.nix
./home-assistant/sonoff.nix
2020-04-17 20:59:12 +02:00
./home-assistant/stocks.nix
2020-04-12 13:36:15 +02:00
./home-assistant/weather.nix
./home-assistant/workday.nix
2020-04-10 13:50:20 +02:00
./home-assistant/zigbee2mqtt.nix
2020-04-08 16:43:09 +02:00
];
services.homeAssistantConfig = {
# turn on to edit GUI
lovelace = { };
2020-04-08 16:43:09 +02:00
homeassistant = {
name = "Home";
2020-04-08 16:43:09 +02:00
latitude = 51.444847;
longitude = 6.967006;
elevation = 116;
unit_system = "metric";
2020-04-08 16:43:09 +02:00
time_zone = config.time.timeZone;
2020-04-08 16:43:09 +02:00
auth_providers = [{
type = "trusted_networks";
trusted_networks = [
config.module.cluster.services.tinc."private".networkSubnet
# lan network
"10.1.0.0/24"
];
2020-04-08 16:43:09 +02:00
}];
2020-04-08 16:43:09 +02:00
};
# needed, don't know why
default_config = { };
config = { };
# to grab metrics via prometheus
2020-04-08 16:43:09 +02:00
prometheus.namespace = "hass";
2020-04-16 02:58:44 +02:00
# manual state
# ------------
# all scenes
input_select.scene = {
2020-04-16 01:26:02 +02:00
icon = "mdi:brightness-auto";
2020-06-11 12:28:29 +02:00
options = [ "default" "night" "outside" "cooking" ];
2020-04-16 01:26:02 +02:00
};
# scenes controlled by buttons
input_select.scene_button = {
icon = "mdi:brightness-auto";
options = [ "default" "night" ];
};
2020-04-16 01:26:02 +02:00
input_boolean.situation_toggle.icon = "mdi:toggle-switch";
input_boolean.printer_toggle.icon = "mdi:toggle-switch";
2020-04-14 17:29:00 +02:00
input_boolean.windows_up.icon = "mdi:toggle-switch";
# heater scenes
input_select.heater_state = {
#icon = "mdi:brightness-auto";
options = [ "off" "on1" "on2" ];
};
2020-06-01 04:12:15 +02:00
automation = [
2020-04-17 20:59:12 +02:00
{
alias = "Hold Button -> Toggle Printer";
trigger = {
platform = "state";
entity_id = "input_boolean.printer_toggle";
};
action = {
service = "switch.toggle";
data.entity_id = "switch.pal02";
};
}
2020-04-16 01:26:02 +02:00
{
2020-06-11 12:28:29 +02:00
alias = "set light-control scene";
2020-04-16 01:26:02 +02:00
trigger = {
platform = "state";
entity_id = "input_select.scene";
2020-04-16 01:26:02 +02:00
};
action = {
2020-06-01 04:12:15 +02:00
service = "mqtt.publish";
data_template = {
2020-06-01 04:12:15 +02:00
topic = "control/lights/set";
payload_template =
''{"scene":"{{ states('input_select.scene')}}"}'';
2020-04-16 01:26:02 +02:00
};
};
}
# control situation with buttons
2020-04-16 01:26:02 +02:00
{
2020-06-11 12:28:29 +02:00
alias = "Button -> Change Scene";
2020-04-16 01:26:02 +02:00
trigger = {
platform = "state";
entity_id = "input_boolean.situation_toggle";
};
2020-06-11 12:28:29 +02:00
condition = {
2020-06-11 16:53:29 +02:00
condition = "template";
value_template =
''{{ states.input_select.scene.state != "outside" }}'';
2020-06-11 12:28:29 +02:00
};
2020-04-16 01:26:02 +02:00
action = {
service = "input_select.select_next";
data.entity_id = "input_select.scene_button";
};
}
2020-06-11 12:28:29 +02:00
{
alias = "Button -> Default Scene";
trigger = {
platform = "state";
entity_id = "input_boolean.situation_toggle";
};
condition = {
2020-06-11 16:53:29 +02:00
condition = "template";
value_template =
''{{ states.input_select.scene.state == "outside" }}'';
2020-06-11 12:28:29 +02:00
};
2020-06-11 16:53:29 +02:00
action = [
{
service = "input_select.select_option";
data = {
entity_id = "input_select.scene_button";
option = "default";
};
}
{
service = "input_select.select_option";
data = {
entity_id = "input_select.scene";
option = "default";
};
}
];
2020-06-11 12:28:29 +02:00
}
{
alias = "Button Scene -> Scene";
trigger = {
platform = "state";
entity_id = "input_select.scene_button";
};
action = {
service = "input_select.select_option";
data_template = {
entity_id = "input_select.scene";
option = "{{ states('input_select.scene_button') }}";
2020-04-16 01:26:02 +02:00
};
};
2020-04-16 00:22:29 +02:00
}
{
alias = "Set Scene To Outside when opening door";
trigger = [
{
platform = "state";
2020-06-01 04:12:15 +02:00
# todo : groups are not working right now
2020-10-21 22:57:48 +02:00
entity_id = "binary_sensor.door_sensor_4";
from = "off";
to = "on";
}
{
platform = "state";
2020-06-01 04:12:15 +02:00
# todo : groups are not working right now
2020-10-21 22:57:48 +02:00
entity_id = "binary_sensor.door_sensor_4";
from = "on";
to = "off";
}
];
action = {
service = "input_select.select_option";
2020-06-01 04:12:15 +02:00
data = {
entity_id = "input_select.scene";
option = "outside";
2020-06-01 04:12:15 +02:00
};
};
}
2020-10-28 23:56:10 +01:00
# window roles
{
alias = "windows state = down in the evening";
2020-10-28 23:56:10 +01:00
trigger = [{
platform = "sun";
event = "sunset";
2020-10-30 23:39:40 +01:00
offset = "+00:01:00"; # 10 min after sunset
2020-10-28 23:56:10 +01:00
}];
action = [{
2020-11-05 09:45:08 +01:00
service = "input_boolean.turn_off";
data.entity_id = "input_boolean.windows_up";
}];
2020-10-28 23:56:10 +01:00
}
{
alias = "windows state = up in the morning";
2020-10-28 23:56:10 +01:00
trigger = [{
2020-10-30 23:39:40 +01:00
platform = "time";
at = "09:00:00";
2020-10-28 23:56:10 +01:00
}];
action = [{
2020-11-05 09:45:08 +01:00
service = "input_boolean.turn_on";
data.entity_id = "input_boolean.windows_up";
}];
}
{
alias = "enforce window_up state";
trigger = [
2020-10-30 23:39:40 +01:00
{
platform = "time_pattern";
minutes = "/5"; # every 5 minutes
2020-10-30 23:39:40 +01:00
}
{
platform = "state";
entity_id = "input_boolean.windows_up";
2020-10-30 23:39:40 +01:00
}
];
action = [{
service = "script.turn_on";
data_template.entity_id = ''
{% if is_state('input_boolean.windows_up','on') -%}
script.fyrtur_up
{%- else -%}
script.fyrtur_down
{%- endif %}
'';
}];
2020-10-28 23:56:10 +01:00
}
2020-10-30 23:39:40 +01:00
# heater
2020-10-28 23:56:10 +01:00
{
alias = "heater state = on1 in the morning";
2020-10-28 23:56:10 +01:00
trigger = [{
2020-10-30 23:39:40 +01:00
platform = "time";
at = "09:00:00";
2020-10-28 23:56:10 +01:00
}];
action = [{
service = "input_select.select_option";
data = {
entity_id = "input_select.heater_state";
option = "on1";
};
}];
2020-10-28 23:56:10 +01:00
}
2020-10-30 00:36:00 +01:00
{
alias = "heater state = on2 in the early evening";
2020-10-30 00:36:00 +01:00
trigger = [{
platform = "time";
2020-10-30 23:39:40 +01:00
at = "19:45:00";
2020-10-30 00:36:00 +01:00
}];
action = [{
service = "input_select.select_option";
data = {
entity_id = "input_select.heater_state";
option = "on2";
};
}];
2020-10-30 00:36:00 +01:00
}
{
alias = "heater state = off in the evening";
2020-10-30 00:36:00 +01:00
trigger = [{
platform = "time";
at = "23:30:00";
2020-10-30 00:36:00 +01:00
}];
action = [{
service = "input_select.select_option";
data = {
entity_id = "input_select.heater_state";
option = "off";
};
}];
}
{
alias = "enforce heater state";
trigger = [
2020-10-30 00:36:00 +01:00
{
platform = "time_pattern";
minutes = "/5"; # every 5 minutes
2020-10-30 00:36:00 +01:00
}
{
platform = "state";
entity_id = "input_select.heater_state";
2020-10-30 00:36:00 +01:00
}
];
action = [{
service = "script.turn_on";
data_template.entity_id =
"script.heater_{{ states('input_select.heater_state') }}";
}];
2020-10-30 00:36:00 +01:00
}
];
2020-04-08 16:43:09 +02:00
2020-04-14 11:38:40 +02:00
group = let
create_room = { name, description }: {
2020-04-14 11:38:40 +02:00
"${name}" = {
2020-04-16 00:22:29 +02:00
name = "${description}";
2020-04-15 18:08:27 +02:00
entities = [ ];
2020-04-14 11:38:40 +02:00
};
};
create_rooms = rooms:
lib.foldr (a: b: a // b) { } (map create_room rooms);
2020-04-12 21:05:55 +02:00
# rooms
# -----
2020-04-14 11:38:40 +02:00
in (create_rooms [
{
name = "floor_room";
description = "Flur";
}
{
name = "bed_room";
description = "Schlafzimmer";
}
{
name = "living_room";
description = "Wohnzimmer";
}
{
name = "kitchen_room";
description = "Küche";
}
2020-04-15 18:08:27 +02:00
{
name = "bath_room";
description = "Klo";
}
2020-04-14 11:38:40 +02:00
]) // {
2020-04-12 21:05:55 +02:00
# overview
# --------
all_sensors = { name = "Alle Sensoren"; };
2020-04-12 21:05:55 +02:00
today = {
name = "Today";
entities = [ "input_select.scene" ];
2020-04-12 19:57:49 +02:00
};
2020-04-12 21:05:55 +02:00
# other stuff
# -----------
tv = { name = "TV"; };
all_lights = { name = "Alle Lampen"; };
2020-04-12 21:05:55 +02:00
unknown = {
name = "Not Used";
entities = [ ];
2020-04-08 16:43:09 +02:00
};
};
script = let
# delay in seconds
delay = 2;
heater_on = heater: temperatur: {
service = "mqtt.publish";
data_template = {
topic = "zigbee2mqtt/${heater}/set"; # office
payload_template = ''
{"system_mode":"auto","current_heating_setpoint":${
toString temperatur
}}'';
};
};
heater_off = heater: {
service = "mqtt.publish";
data_template = {
topic = "zigbee2mqtt/${heater}/set"; # office
payload_template = ''{"system_mode":"off"}'';
};
};
hot = 28;
cold = 19;
fyrtur_command = device: position: {
service = "mqtt.publish";
data_template = {
topic = "zigbee2mqtt/${device}/set";
payload_template = ''{"position":"${toString position}"}'';
};
};
in {
heater_off = {
sequence = [
(heater_off "heater1") # office
{ delay = delay; }
(heater_off "heater2") # office
{ delay = delay; }
(heater_off "heater3") # bed room
{ delay = delay; }
(heater_off "heater4") # storage room
];
};
heater_on1 = {
sequence = [
(heater_on "heater1" hot) # office
{ delay = delay; }
(heater_on "heater2" hot) # office
{ delay = delay; }
(heater_on "heater3" cold) # bed room
{ delay = delay; }
(heater_on "heater4" cold) # storage room
];
};
heater_on2 = {
sequence = [
(heater_on "heater1" hot) # office
{ delay = delay; }
(heater_on "heater2" hot) # office
{ delay = delay; }
(heater_on "heater3" hot) # bed room
{ delay = delay; }
(heater_on "heater4" cold) # storage room
];
};
fyrtur_up = {
sequence = [
(fyrtur_command "fyrtur1" 90)
{ delay = delay; }
(fyrtur_command "fyrtur2" 90)
{ delay = delay; }
2020-11-05 09:45:08 +01:00
(fyrtur_command "fyrtur3" 100)
{ delay = delay; }
2020-11-05 09:45:08 +01:00
(fyrtur_command "fyrtur4" 100)
];
};
fyrtur_down = {
sequence = [
2020-11-05 09:45:08 +01:00
(fyrtur_command "fyrtur1" 16)
{ delay = delay; }
2020-11-05 09:45:08 +01:00
(fyrtur_command "fyrtur2" 16)
{ delay = delay; }
2020-11-05 09:45:08 +01:00
(fyrtur_command "fyrtur3" 22)
{ delay = delay; }
2020-11-05 09:45:08 +01:00
(fyrtur_command "fyrtur4" 22)
];
};
};
2020-04-08 16:43:09 +02:00
};
services.home-assistant = {
enable = true;
#package = unstablePkgs.home-assistant.override {
# python3 = unstablePkgs.python37;
# extraPackages = python: [
# # todo : check which is still needed
# python.netdisco
# python.xmltodict
# python.mpd2
# # for mqtt
# python.hbmqtt
# python.paho-mqtt
# # needed for platform workday
# #(python.buildPythonPackage rec {
# # pname = "holidays";
# # version = "0.9.10";
# # src = python.fetchPypi {
# # inherit pname version;
# # sha256 =
# # "9f06d143eb708e8732230260636938f2f57114e94defd8fa2082408e0d422d6f";
# # };
# # doCheck = false;
# # buildInputs = [ pkgs.dateutils ];
# # propagatedBuildInputs = [ python."python-dateutil" python."six" ];
# # meta = with pkgs.stdenv.lib; {
# # homepage = "https://github.com/dr-prodigy/python-holidays";
# # license = licenses.mit;
# # description = "Generate and work with holidays in Python";
# # maintainers = with maintainers; [ mrVanDalo ];
# # };
# #})
# ];
#};
2020-04-08 16:43:09 +02:00
};
2020-04-09 01:19:00 +02:00
# host nginx setup
services.nginx = {
enable = true;
recommendedProxySettings = true;
virtualHosts = {
"iot.pepe.private" = {
2020-04-09 16:51:39 +02:00
serverAliases = [ "hass.pepe.private" "home.pepe.private" ];
locations."/" = { proxyPass = "http://pepe.private:8123"; };
2020-04-09 01:19:00 +02:00
};
};
};
2020-04-08 16:43:09 +02:00
}