summaryrefslogtreecommitdiffstats
path: root/makefu/2configs/home/ham/sensor/pollen.nix
blob: d95c199bfb13496b144a6f39702f3f5b168d4a68 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
{ pkgs, lib, ... }:
with lib;
let
  region = "112";
  types = [
      "Erle"
      "Beifuss"
      "Ambrosia"
      "Birke"
      "Esche"
      "Hasel"
      "Graeser"
      "Roggen"
    ];
    gen_tomorrow_sensor = type: {
      name = "dwd_pollenbelastung_${toLower type}_tomorrow";
      value = {
       icon_template = "mdi:grass";
       friendly_name = "${type} Morgen";
       value_template = "{{ state_attr('sensor.dwd_pollenbelastung_${toLower type}', 'tomorrow') }}";
      };
    };
    gen_sensor = type: {
      name = "dwd_pollenbelastung_${toLower type}";
      value = {
        icon_template = "mdi:tree-outline";
        friendly_name = type;
        value_template = ''
         {% set dwd_state = state_attr('sensor.dwd_pollenbelastung_stuttgart', '${type}')['today'] %}
         {% if dwd_state == "3" %}6{% elif dwd_state == "2-3"%}5{% elif dwd_state == "2"%}4{% elif dwd_state == "1-2"%}3{% elif dwd_state == "1"%}2{% elif dwd_state == "0-1"%}1{% else %}0{% endif %}
        '';
        attribute_templates.today = ''
           {% set dwd_state = state_attr('sensor.dwd_pollenbelastung_stuttgart', '${type}')['today'] %}
           {% if dwd_state == "3" %}6{% elif dwd_state == "2-3"%}5{% elif dwd_state == "2"%}4{% elif dwd_state == "1-2"%}3{% elif dwd_state == "1"%}2{% elif dwd_state == "0-1"%}1{% else %}0{% endif %}
        '';
        attribute_templates.tomorrow = ''
           {% set dwd_state = state_attr('sensor.dwd_pollenbelastung_stuttgart', '${type}')['tomorrow'] %}
           {% if dwd_state == "3" %}6{% elif dwd_state == "2-3"%}5{% elif dwd_state == "2"%}4{% elif dwd_state == "1-2"%}3{% elif dwd_state == "1"%}2{% elif dwd_state == "0-1"%}1{% else %}0{% endif %}
        '';
        # -1 == unknown
        #attribute_templates.dayafter = ''
        #   {% set dwd_state = state_attr('sensor.dwd_pollenbelastung', '${type}')['dayafter_to'] %}
        #   {% if dwd_state == "3" %}6{% elif dwd_state == "2-3"%}5{% elif dwd_state == "2"%}4{% elif dwd_state == "1-2"%}3{% elif dwd_state == "1"%}2{% elif dwd_state == "0-1"%}1{% elif dwd_state == "-1"%}-1{% else %}0{% endif %}
        #'';
      };
    };
in
  {
  services.home-assistant.config.sensor = [
  { 
    platform = "rest";
    scan_interval = 3600;
    name = "DWD Pollenbelastung Stuttgart";
    resource = "https://opendata.dwd.de/climate_environment/health/alerts/s31fg.json";
    json_attributes_path = "$..content[?(@.partregion_id==${region})].Pollen";
    json_attributes =  types;
    value_template = "{{ value_json.last_update }}";
  }
  {
    platform = "template";
    sensors = (listToAttrs (map gen_sensor types)) //
              (listToAttrs (map gen_tomorrow_sensor types)) ;
  }
  ];
}