summaryrefslogtreecommitdiffstats
path: root/krebs/2configs/shack/powerraw.nix
diff options
context:
space:
mode:
authortv <tv@krebsco.de>2019-09-11 10:34:02 +0200
committertv <tv@krebsco.de>2019-09-11 10:34:02 +0200
commit0182f1bd64973e93d4cf4c30b6005708b7e09240 (patch)
treef5a318fee1572b9b35f9f321d4ac707bc7935792 /krebs/2configs/shack/powerraw.nix
parente388d02623b98bad5db52b29ea1ef1f494fddae8 (diff)
parent5d24345ff430df38263c113041070a900c23131e (diff)
Merge remote-tracking branch 'prism/master'
Diffstat (limited to 'krebs/2configs/shack/powerraw.nix')
-rw-r--r--krebs/2configs/shack/powerraw.nix72
1 files changed, 72 insertions, 0 deletions
diff --git a/krebs/2configs/shack/powerraw.nix b/krebs/2configs/shack/powerraw.nix
new file mode 100644
index 000000000..af3f129c1
--- /dev/null
+++ b/krebs/2configs/shack/powerraw.nix
@@ -0,0 +1,72 @@
+{ config, lib, pkgs, ... }:
+# Replacement for powerraw.shack pollin box
+# Requires usb-serial device on host
+# Requires mqtt available at mqtt.shack
+# Requires hostname powerraw.shack
+let
+ influx-url = "http://influx.shack:8086";
+ pkg = pkgs.python3.pkgs.callPackage (
+ pkgs.fetchgit {
+ url = "https://git.shackspace.de/rz/powermeter.git";
+ rev = "96609f0d632e0732afa768ddd7b3f8841ca37c1b";
+ sha256 = "sha256:0wfpm3ik5r081qv2crmpjwylgg2v8ximq347qh0fzq1rwv0dqbnn";
+ }) {};
+in {
+ # receive response from light.shack / standby.shack
+ networking.firewall.allowedUDPPorts = [ 11111 ];
+ users.users.powermeter.extraGroups = [ "dialout" ];
+
+ systemd.services.powermeter-serial2mqtt = {
+ description = "powerraw Serial -> mqtt";
+ wantedBy = [ "multi-user.target" ];
+ serviceConfig = {
+ User = "powermeter";
+ ExecStart = "${pkg}/bin/powermeter-serial2mqtt";
+ Restart = "always";
+ PrivateTmp = true;
+ };
+ };
+
+ systemd.services.powermeter-mqtt2socket = {
+ description = "powerraw mqtt -> raw socket 11111";
+ wantedBy = [ "multi-user.target" ];
+ serviceConfig = {
+ User = "powermeter";
+ ExecStart = "${pkg}/bin/powermeter-mqtt2socket";
+ Restart = "always";
+ PrivateTmp = true;
+ };
+ };
+
+ services.telegraf = {
+ enable = true;
+ extraConfig = {
+ agent.debug = false;
+ outputs = {
+ influxdb = [{
+ urls = [ influx-url ];
+ database = "telegraf";
+ }];
+ };
+ };
+ };
+
+ services.telegraf.extraConfig.inputs.mqtt_consumer = let
+ genTopic = name: topic: tags: {
+ servers = [ "tcp://mqtt.shack:1883" ];
+ qos = 0;
+ connection_timeout = "30s";
+ topics = [ topic ];
+ inherit tags;
+ persistent_session = false;
+ name_override = name;
+ data_format = "value";
+ data_type = "float";
+ };
+ sensor = "total";
+ types = [ "Voltage" "Current" "Power" ];
+ phases = [ 1 2 3 ];
+ in
+ [ (genTopic "Power consumed" "/power/${sensor}/consumed" { inherit sensor; }) ] ++
+ (lib.flatten (map (type: (map (phase: (genTopic "Power" "/power/${sensor}/L${toString phase}/${type}" { inherit sensor phase type; }) ) phases)) types));
+}