substitutions:
  name: "geigercounter"
  friendly_name: "Geigerzähler"
  
esphome:
  name: ${name}
  friendly_name: ${friendly_name}
  name_add_mac_suffix: false
  project:
    name: sensor.radiation
    version: "1.0"
  min_version: 2022.11.0

esp32:
  board: nodemcu-32s
  framework:
    type: arduino

wifi:
  ssid: "Voltage-legacy"
  password: !secret voltage_legacy_psk
  use_address: ${name}.home  
  power_save_mode: high
  fast_connect: on

  # Enable fallback hotspot (captive portal) in case wifi connection fails
  ap:
    ssid: "Geigercounter Fallback Hotspot"
    password: !secret fallback_psk

captive_portal:

# Enable logging
logger:

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

ota:
  platform: esphome
  password: !secret ota

spi:
  clk_pin: 18
  mosi_pin: 21

display:
  - platform: max7219
    cs_pin: 16
    num_chips: 1
    lambda: |-
      if(id(sun_state).has_state()) {
        // dim 7 segment's LED brighness at nighttime
        if (id(sun_state).state == "below_horizon") {
          it.set_intensity(2);
        } else {
          it.set_intensity(12);
        }
      }
      it.print("        ");
      it.printf("%2.2f uSh", id(dose_meter).state);

# Here we calc and include to the firmware a power and doze values of ionizing radiation as sensor outputs
sensor:
  - platform: pulse_counter
    pin: 19
    unit_of_measurement: "µSv/h"
    name: "Radioaktive Strahlungsleistung"
    icon: "mdi:radioactive"
    count_mode:
      rising_edge: DISABLE
      falling_edge: INCREMENT
    # Hardware counter alows only 13us debounce, so we set it OFF:
    use_pcnt: False
    # When hw counter is OFF then we may set our filter time to SBM20 190 us Deadtime value or any other (also in microseconds):
    internal_filter: 190us
    update_interval: 60s
    accuracy_decimals: 3
    id: dose_meter
    filters:
      - sliding_window_moving_average: # 5-minutes moving average (MA5) here
          window_size: 5
          send_every: 1
      - multiply: 0.0057 # SBM20 tube conversion factor of pulses into µSv/Hour
  - platform: integration
    name: "Gesamte Strahlungsdosis"
    unit_of_measurement: "µSv"
    sensor: dose_meter # link entity id to the pulse_counter values above
    icon: "mdi:radioactive"
    accuracy_decimals: 5
    time_unit: min # integrate values every next minute
    filters:
      # obtained dose. Converting from mkSv/hour into mkSv/minute: [mkSv/h / 60] OR [mkSv/h * 0.0166666667]. 
      # if my_dose_meter in CPM, then [0.0054 / 60 minutes] = 0.00009; so CPM * 0.00009 = dose every next minute, mkSv.
      - multiply: 0.0166666667
text_sensor:
  - platform: homeassistant
    entity_id: sun.sun
    id: sun_state
