ESPHome 일/월 누적전력량 센서



ESPHome YAML
sensor:
  - platform: hlw8012
    power:
      name: neo_watt
      id: neo_watt
      unit_of_measurement: W
      accuracy_decimals: 2
# 누적전력량 센서
    energy:
      name: neo_energy
      id: neo_energy
      unit_of_measurement: kWh
      device_class: "energy"
      state_class: "total_increasing"
      accuracy_decimals: 2
      filters:
        - lambda: return x / 1000;

  # 일간 누적전력량과 월간 누적전력량 센서
  - platform: integration
    name: neo_daily_enery
    id: daily_energy
    sensor: "neo_watt"
    time_unit: h
    filters:
      - lambda: return x * (0.001);
    unit_of_measurement: kWh
    accuracy_decimals: 2
  - platform: integration
    name: neo_monthly_enery
    id: monthly_energy
    sensor: "neo_watt"
    time_unit: h
    filters:
      - lambda: return x * (0.001);
    unit_of_measurement: kWh
    accuracy_decimals: 2

time:
  - platform: sntp
    timezone: UTC-9
    servers:
      - time.bora.net
    # 위 일간 전력량과 월간 전력량 센서를 매일 자정과 매월 일일 자정에 리셋
    on_time:
      - cron: '0 0 0 * * *'
        then:
          - sensor.integration.reset: daily_energy
      - cron: '0 0 0 1 * *'
        then:
          - sensor.integration.reset: monthly_energy

또 다른 방법으로 HA에서 esphome의 누적에너지 센서를 이용해 일간 전력량과 월간 전력량 센서를 만들어도 됩니다.
센서 데이타를 정전등의 재부팅후에도 유지하려면 HA에서 전력량센서를 만드는 게 좋아 보입니다. ESPHome에서도 restore옵션으로 일정간격으로 저장할 수 있지만 eprom 특성상 그리 좋아보이진 않고 restore옵션의 디폴트 값도 false입니다.

HA/configuration/utility_meter:
neo_daily_energy:
  source: sensor.neo_energy
  cycle: daily
  offset: '00:00:00'
neo_monthly_energy:
  source: sensor.neo_energy
  cycle: monthly
  offset: '00:00:00'


** ESPHome  Energy센서는 정전이나 재부팅시 0으로 리셋되니 재부팅할 때 energy센서의 이전 값을 넘겨받아 재기록하는 템플릿센서를 추가해 그 템플릿 센서를 메인 energy센서로 사용하면 됩니다.
globals:
  - id: total_energy
    type: float
    restore_value: yes
    initial_value: '0.0'

sensor:
  - platform: hlw8012
    energy:
      name: "${device_name} Energy"
      id: energy
      unit_of_measurement: kWh
      device_class: "energy"
      state_class: "total_increasing"
      filters:
        - lambda: return x / 1000;
      on_value:
        then:
          - lambda: |-
              static float previous_energy_value = 0.0;
              float current_energy_value = id(energy).state;
              id(total_energy) += current_energy_value - previous_energy_value;
              previous_energy_value = current_energy_value;

  - platform: template
    name: "${device_name} Total Energy"
    unit_of_measurement: kWh
    state_class: "total_increasing"
    icon: "mdi:lightning-bolt"
    lambda: |-
      return id(total_energy);
    update_interval: 10s