這篇要做出來的東西 :平面圖上每個房間的顏色反映它現在的溫度。20 度是藍的、25 度是綠的、30 度是紅的,而且是連續漸變,不是三段跳。
一眼掃過去就知道哪間熱哪間冷,不用一顆一顆點開看數字。
這篇用的是 HACS 上的 ha-floorplan (v1.2.0 以上)。下面會從一個最小的可跑檔案開始,做完之後你會有一張會隨溫度變色的平面圖,外加房間裡的數字。
步驟 1:準備一個最小的平面圖
在 Home Assistant 設定目錄底下建 www/floorplan/ 資料夾,放這兩個檔。
www/floorplan/home.svg :
<svg xmlns="http://www.w3.org/2000/svg" width="600" height="320" viewBox="0 0 600 320"> <rect id="room-living" x="0" y="0" width="360" height="320"/> <rect id="room-bedroom" x="360" y="0" width="240" height="320"/> <text class="room-name" x="14" y="30">客廳</text> <text class="room-name" x="374" y="30">臥室</text> <!-- 溫度數字,步驟 5 會用到。初始內容故意放 "--" --> <text id="temp-living" class="sensor-text" x="150" y="180">--</text> <text id="temp-bedroom" class="sensor-text" x="450" y="180">--</text> </svg> 複製代碼
www/floorplan/home.css :
svg { font-family: "Microsoft JhengHei", "PingFang TC", sans-serif; } #room-living, #room-bedroom { fill: #E0E0E0; stroke: #3A4250; stroke-width: 3; } .room-name { font-size: 18px; font-weight: bold; fill: #344052; pointer-events: none; } .sensor-text { font-size: 26px; font-weight: bold; fill: #2C3440; text-anchor: middle; pointer-events: none; } 複製代碼
SVG 的 id 一律用純英文、不要用點號 。很多舊教學教你把 id 直接寫成實體 ID(`sensor.living_room_temperature`),不要跟著做:CSS 選擇器碰到點號要跳脫成 `#sensor\.living_room_temperature`,很容易打錯,而且一個實體想同時驅動兩個圖形就做不到。ha-floorplan 官方文件自己就寫「強烈建議完全不要在 id 裡用點號」。
對應關係等一下在 YAML 裡寫。
步驟 2:加上卡片,先確認它出得來
儀表板 → 編輯 → 新增卡片 → 選 Floorplan Card → 切到程式碼編輯器,貼:
type: custom:floorplan-card title: 家裡 config: image: /local/floorplan/home.svg stylesheet: /local/floorplan/home.css log_level: info console_log_level: info rules: [] 複製代碼