2025/11/08

ESPHome에서 샤오미 체중계의 마지막 값 기록


샤오미 체중계(miscale2)가 esphome재부팅 후에도 마지막 기록값을 유지하도록 글로벌 변수에 저장.
재부팅후 마지막 저장된 값을 불러와 템플릿센서에 기록. 원래 센서는 HA에 등록되지 않도록 internal: true를 사용하고 같은 이름의 템플릿센서를 HA에 노출.

** 마지막 측정된 값을 기록하지 않으면 재부팅시 초기화되면 unknown값이 전송되고 재측정하기 전까지 유지되기 때문에 필요함.
** HACS에서 내려받은 Bodymiscale, Bodymiscale Card를 사용하면 샤오미 전용앱처럼 그래프를 그려 상태를 보여줌.


globals:
  - id: last_weight
    type: float
    restore_value: yes
    initial_value: '0.0'
  - id: last_impedance
    type: float
    restore_value: yes
    initial_value: '0.0'

sensor:
  - platform: template
    name: "MiScale Weight"
    lambda: |-
      return id(last_weight);
    unit_of_measurement: "kg"
    device_class: weight
    state_class: measurement

  - platform: template
    name: "MiScale Impedance"
    lambda: |-
      return id(last_impedance);
    unit_of_measurement: "Ω"
    state_class: measurement

  - platform: xiaomi_miscale
    mac_address: 'XX:XX:XX:XX:XX:XX'
    weight:
      name: "MiScale Weight"
      internal: true
      on_value:
        then:
          - lambda: |-
              id(last_weight) = x;
    impedance:
      name: "MiScale Impedance"
      internal: true
      on_value:
        then:
          - lambda: |-
              id(last_impedance) = x;