如何用Homeassistant展示孕期信息
背景
热爱折腾HA的人,怀孕的进度可能也想要能进行一些可视化。因此有了如下教程。 本期小教程教您怎么在Homeassistant中实现对以下信息的实时监控及展示
预产期
距离预产期天数
当前孕周及天数
孕期阶段及进程
孕妈孕期增重(可选,自行准备孕妇体重实体,这个要写的话就太长了)
教程前须知
请确保您了解Homeassistant的基本框架,本教程涉及Configuration.yaml-template sensor 添加。在开始本教程前请妥善完成备份
教程
Step1---创建Sensor
将以下yaml中的"2026-02-31"全部替换为孕妇的预产期,然后粘贴到您HA的Configuration.yaml中。
保存后重启HA
确认各实体数据是否正常:设置→设备与服务→实体

template: - sensor: # 预产期 - name: "预产期" unique_id: pregnancy_due_date state: "2026-02-31" icon: mdi:baby-face attributes: friendly_name: "宝宝预产期" # 距离预产期 - name: "距离预产期" unique_id: pregnancy_days_remaining state: "{{ ((as_timestamp('2026-02-31') - as_timestamp(today_at('00:00'))) / 86400) | int }}" unit_of_measurement: "天" icon: mdi:calendar-clock attributes: friendly_name: "距离预产期" # 当前孕周(修正+1天) - name: "当前孕周" unique_id: pregnancy_current_week state: "{{ ((as_timestamp(today_at('00:00')) - as_timestamp('2026-02-31') + 280*86400 + 86400) / 86400) | int // 7 }}周{{ ((as_timestamp(today_at('00:00')) - as_timestamp('2026-02-31') + 280*86400 + 86400) / 86400) | int % 7 }}天" icon: mdi:calendar-heart attributes: friendly_name: "当前孕周" weeks: "{{ ((as_timestamp(today_at('00:00')) - as_timestamp('2026-02-31') + 280*86400 + 86400) / 86400) | int // 7 }}" days_in_week: "{{ ((as_timestamp(today_at('00:00')) - as_timestamp('2026-02-31') + 280*86400 + 86400) / 86400) | int % 7 }}" total_days: "{{ ((as_timestamp(today_at('00:00')) - as_timestamp('2026-02-31') + 280*86400 + 86400) / 86400) | int }}" # 孕期进度(修正+1天) - name: "孕期进度" unique_id: pregnancy_progress state: "{{ ((((as_timestamp(today_at('00:00')) - as_timestamp('2026-02-31') + 280*86400 + 86400) / 86400) / 280 * 100) | round(1)) }}" unit_of_measurement: "%" icon: mdi:progress-clock attributes: friendly_name: "孕期进度" # 孕期阶段(修正+1天) - name: "孕期阶段" unique_id: pregnancy_trimester state: > {% set weeks = ((as_timestamp(today_at('00:00')) - as_timestamp('2026-02-31') + 280*86400 + 86400) / 86400) | int // 7 %} {% if weeks < 13 %}孕早期(第一阶段){% elif weeks < 28 %}孕中期(第二阶段){% elif weeks < 40 %}孕晚期(第三阶段){% else %}已过预产期{% endif %} icon: mdi:human-pregnant attributes: friendly_name: "孕期阶段"
Step2---卡片制作
在HA仪表盘中随便创建个卡片,在代码编辑器中替换为以下代码
孕期增重这一节为可选,如果您没有接入孕妇体重的话,可以删除(个人建议整起来,思路是小米体脂秤广播BLE→ESP32蓝牙转发→HA)
如果您的configuration跟我一样,应该不需要额外修改
type: vertical-stack
cards:
- type: custom:mushroom-title-card
title: 👶 宝宝倒计时
subtitle: 预产期:2026年4月29日
- type: horizontal-stack
cards:
- type: custom:mushroom-entity-card
entity: sensor.dang_qian_yun_zhou
icon: mdi:calendar-heart
primary_info: state
secondary_info: name
icon_color: pink
- type: custom:mushroom-entity-card
entity: sensor.ju_chi_yu_chan_qi
icon: mdi:calendar-clock
primary_info: state
secondary_info: name
icon_color: blue
- type: custom:mushroom-entity-card
entity: sensor.yun_qi_jin_du
icon: mdi:progress-clock
primary_info: state
secondary_info: name
icon_color: purple
- type: custom:mushroom-template-card
entity: sensor.lan_weight
secondary: 孕期增重
primary: |
{{ (states('sensor.lan_weight') | float - 42.5) | round(2) }} Kg
icon: mdi:scale-bathroom
icon_color: green
- type: custom:mushroom-entity-card
entity: sensor.yun_qi_jie_duan
icon: mdi:human-pregnant
primary_info: state
secondary_info: name
icon_color: orange
#如果您没有孕妇体重实体,可删除以下内容
- type: custom:mushroom-template-card
entity: sensor.lan_weight
secondary: 孕期增重
primary: |
{{ (states('sensor.lan_weight') | float - 42.5) | round(2) }} Kg
icon: mdi:scale-bathroom
icon_color: green
最终成品如下图

机动不灵
校验提示文案
机动不灵
校验提示文案