summaryrefslogtreecommitdiffstats
path: root/esphome/config/printerswitch.yaml
blob: 9b31c94996682e8d84a24503a0b46729ab2560a2 (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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
substitutions:
  device_name: printerswitch
  friendly_name: "Printer Switch"
  current_resistor: 0.0007978220592127652
  voltage_divider: 4422.084168336673
  # max power is 3450W for 15A and 2300W for 10A
  max_power: "3450"

esphome:
  name: ${device_name}
  friendly_name: ${friendly_name}

esp8266:
  board: esp01_1m

# Enable logging
logger:
  # make RXD / TXD available
  baud_rate: 0

# Enable Home Assistant API
api:
  encryption:
    key: !secret api_encryption_key

# OTA
ota:
  - platform: esphome
    password: !secret ota_password

# WiFi Credentials
wifi:
  domain: .lan
  ssid: !secret wifi_ssid
  password: !secret wifi_password
  # see https://github.com/esphome/issues/issues/1532
  power_save_mode: HIGH

  # Enable fallback hotspot (captive portal) in case wifi connection fails
  ap:
    ssid: "${device_name} Hotspot"
    password: !secret wifi_ap_password

captive_portal:

binary_sensor:
  - platform: gpio
    name: "Button"
    device_class: power
    pin:
      number: GPIO1
      inverted: true
    on_press:
      - switch.toggle: relay
    internal: true

sensor:
  - platform: hlw8012
    model: BL0937
    sel_pin: GPIO12
    cf_pin: GPIO4
    cf1_pin: GPIO5
    current_resistor: ${current_resistor}
    voltage_divider: ${voltage_divider}
    voltage:
      id: voltage
      filters:
        - skip_initial: 2
    power:
      id: power
      internal: true
      filters:
        - skip_initial: 2
      on_value:
        then:
          # calculate current through P=U*I
          - lambda: id(current).publish_state(x / id(voltage).state);
          # idle power down. <5W
          - if:
              condition:
                for:
                  time: 5min
                  condition:
                    and:
                      - switch.is_on: relay
                      - lambda: return id(power).state < 5.0;
              then:
                - logger.log: "idle power down"
                - switch.turn_off: relay
      on_value_range:
        - above: ${max_power}
          then:
            - switch.turn_off: relay
    energy:
      id: energy
      internal: true
    update_interval: 1s
    initial_mode: "VOLTAGE"
    change_mode_every: never

  - platform: template
    id: current
    accuracy_decimals: 3
    update_interval: never
    internal: true

    # templates sensors for the ui. we only update the sensor on relay events or through interval
    # but only if switch is on. this way we don't push updates without changes to frontend
  - platform: template
    name: "Current"
    id: current_ui
    unit_of_measurement: "A"
    accuracy_decimals: 3
    icon: "mdi:flash-outline"
    update_interval: never

  - platform: template
    name: "Voltage"
    id: voltage_ui
    unit_of_measurement: "V"
    icon: "mdi:flash-outline"
    update_interval: never

  - platform: template
    name: "Power"
    id: power_ui
    unit_of_measurement: "W"
    icon: "mdi:flash-outline"
    update_interval: never

  - platform: template
    name: "Energy"
    id: energy_ui
    unit_of_measurement: "Wh"
    icon: "mdi:flash-outline"
    update_interval: never

switch:
  - platform: gpio
    name: "Relay"
    id: relay
    pin: GPIO14
    on_turn_on:
      - output.turn_on: led
      - lambda: id(current_ui).publish_state(id(current).state);
      - lambda: id(voltage_ui).publish_state(id(voltage).state);
      - lambda: id(power_ui).publish_state(id(power).state);
      - lambda: id(energy_ui).publish_state(id(energy).state);
    on_turn_off:
      - output.turn_off: led
      - lambda: id(current_ui).publish_state(0.0);
      - lambda: id(voltage_ui).publish_state(0.0);
      - lambda: id(power_ui).publish_state(0.0);
      - lambda: id(energy_ui).publish_state(0.0);

output:
  - platform: esp8266_pwm
    id: led
    pin: GPIO13

  - platform: gpio
    id: unused_gpio0
    pin:
      number: GPIO0
      mode: INPUT_PULLUP
  - platform: gpio
    id: unused_gpio2
    pin:
      number: GPIO2
      mode: INPUT_PULLUP
  - platform: gpio
    id: unused_gpio3
    pin:
      number: GPIO3
      mode: INPUT_PULLUP
  - platform: gpio
    id: unused_gpio15
    pin:
      number: GPIO15
      mode: INPUT_PULLUP
  - platform: gpio
    id: unused_gpio16
    pin:
      number: GPIO16
      mode: INPUT_PULLDOWN

interval:
  - interval: 10s
    then:
      - if:
          condition:
            switch.is_on: relay
          then:
            - lambda: id(current_ui).publish_state(id(current).state);
            - lambda: id(voltage_ui).publish_state(id(voltage).state);
            - lambda: id(power_ui).publish_state(id(power).state);
            - lambda: id(energy_ui).publish_state(id(energy).state);