ECHONETLite2MQTTを使ったECHONETLite機器のHome Assistantへの接続

前回の記事で、Home Assistantのインストール手順を紹介しました。

dededemio.hatenablog.jp

また、以下の記事で、エコキュート・エアコンに無線LANアダプターを接続し、Wi-Fi経由によるECHONET Lite接続が可能になりました。

dededemio.hatenablog.jp

dededemio.hatenablog.jp

今回は、Home AssistantへECHONET Lite対応機器を接続し、表示・操作する方法を紹介します。

ECHONET Lite対応機器の接続方法

ECHONETLite機器をHome Assistantへ接続するには、ECHONET Lite規格の通信に対応したソフトが必要です。 Home Assistantにはカスタムインテグレーションにechonetlite_homeassistantというものがあります。

github.com

導入方法は簡単で、設定→デバイスとサービス→統合を追加 から"ECHONETLite"を検索してインストールするか、上記のREADMEのOPEN HACS REPOSITORY ON MYというリンクをクリックしてインストールし、Home Assistantを再起動するだけ、です。

しかしながら、わたしの環境では以下のメッセージが出て、上記の方法ではインストールができませんでした。

マニュアルインストール方法も書いてあり、そちらではインストールはできたものの、どうしてもインテグレーションからECHONETLite機器を見つけることができず、うまく動きませんでした*1

そこで、以下の記事を参考に、ECHONETLite2MQTTという、ECHONETLite通信機器をMQTTとして接続するソフトを用い、Home Assistantと連携することにしました。

qiita.com

ECHONETLite2MQTTの導入

基本的には、ECHONETLite2MQTTのREADMEを見ながらインストールします。以下のGitHubサイトからADD ADD-ON REPOSITORY TO MYのリンクをクリックしてアドオンストアに追加のリポジトリを登録し、アドオンストアでECHONETLite2MQTTを検索してインストールできます。

github.com

アドオンストアにすぐに現れなければ、右上の「アップデートの確認」をクリックしてしばらく待つと出てきます。 しかし、私の環境ではこれでもうまくインストールできなかったため、Raspberry Piにdockerでインストールすることにしました。

以下にdockerでのインストール手順を示します。まず、dockerをインストールします。

sudo apt update
sudo apt install docker.io
sudo apt install docker-compose

インストール後、以下のコマンドでdockerの動作確認をします。

sudo docker run --rm hello-world
sudo docker ps

次に、ECHONETLite2MQTTのdockerイメージを作成します。これには1分くらいかかります。

git clone https://github.com/banban525/echonetlite2mqtt.git
cd echonetlite2mqtt
sudo docker build ./ -t echonetlite2mqtt

そして、Home AssistantのMQTTブローカーに接続するためのconfig.jsonを作成します。

{
  "port": 1883,
  "username": "mosquitto",
  "password": "your-password"
}

今回は、Home Assistantにmosquittoというユーザーを追加し、そのユーザーで接続することにします。

以下のコマンドでECHONETLite2MQTTを起動します。

sudo docker run -d --net=host -v /home/user/echonetlite2mqtt/config.json:/app/config/config.json -e MQTT_OPTION_FILE=/app/config/config.json -e MQTT_BROKER="mqtt://192.168.xx.xx:1883" echonetlite2mqtt

config.json相対パスでも指定できそうでしたが、私の環境では絶対パスでないと動かなかったので、絶対パスとしました。

さらに、ポートを開放します。3000はECHONET Lite2MQTTのWeb UIのポート、3610はECHONET Liteの通信ポートです。

sudo ufw allow 3000
sudo ufw allow 3610

これで、ECHONETLite2MQTTをインストールしたRaspberry Pihttp://192.168.xx.xx:3000にアクセスすると、以下のような画面が現れ、ECHONETLite2MQTTが正常に立ち上がっており、ECHONETLiteデバイスを発見できていることがわかります。

右上がMQTT Connectedとなっており、Home AssistantのMosquittoのログをみても接続されているようで、立ち上げには成功しているようした。

ECHONETLite2MQTTの操作

ECHONETLite2MQTTのWebUI画面から、発見されているデバイスをクリックすると、以下のような画面になります。 各プロパティがデバイス側が持っている信号で、setができるものは下向きの矢印をクリックするとパラメータ変更ができます。

上部のLogsをクリックすると、ECHONETLite2MQTTのログを見ることができます。ここには先ほど操作した内容や、requestなどの受信ログなどが記録されます。

Home AssistantへのMQTT接続のセットアップ

Home Assistantに、MQTTで受信したデータをエンティティとして追加するには、configuration.yamlに設定を記載する必要があります。

ここでは、エアコンとエコキュートの設定例を示します。

まずはエアコンです。エアコンはclimateと呼ぶ設定で、設定温度、現在温度、モード、風量を統一的に操作できるようになっていますので、この設定を記述します。一方、これに含まれないパラメータは、climateとは別で定義します。

ちょっと長いのですが、yaml例は以下の通りです。fe0000~の部分は任意のDevice IDに修正してください。

mqtt:
  climate:
    - name: "Air-conditioner_StudyRoom"
      unique_id: airconditioner_studyroom
      icon: mdi:air-conditioner

      modes:
        - "off"
        - "cool"
        - "heat"
        - "dry"
        - "auto"

      fan_modes:
        - "2"
        - "3"
        - "5"
        - "auto"
      fan_mode_state_topic: "echonetlite2mqtt/elapi/v2/devices/fe00000000000000000000000000000000/properties/airFlowLevel"
      fan_mode_command_topic: "echonetlite2mqtt/elapi/v2/devices/fe00000000000000000000000000000000/properties/airFlowLevel/set"

      mode_command_topic: "echonetlite2mqtt/elapi/v2/devices/fe00000000000000000000000000000000/properties/set"
      mode_command_template: >-
        {% if value == 'off' %}
          {"operationStatus":"false"}
        {% elif value == 'cool' %}
          {"operationStatus":"true","operationMode":"cooling"}
        {% elif value == 'heat' %}
          {"operationStatus":"true","operationMode":"heating"}
        {% elif value == 'dry' %}
          {"operationStatus":"true","operationMode":"dehumidification"}
        {% elif value == 'auto' %}
          {"operationStatus":"true","operationMode":"auto"}
        {% else %}
          {}
        {% endif %}

      mode_state_topic: "echonetlite2mqtt/elapi/v2/devices/fe00000000000000000000000000000000/properties"
      mode_state_template: >-
        {% if value_json.operationStatus == 'false' %}
          off
        {% elif value_json.operationMode == 'cooling' %}
          cool
        {% elif value_json.operationMode == 'heating' %}
          heat
        {% elif value_json.operationMode == 'dehumidification' %}
          dry
        {% elif value_json.operationMode == 'auto' %}
          auto
        {% else %}
          off
        {% endif %}

      temperature_state_topic: "echonetlite2mqtt/elapi/v2/devices/fe00000000000000000000000000000000/properties/targetTemperature"
      temperature_command_topic: "echonetlite2mqtt/elapi/v2/devices/fe00000000000000000000000000000000/properties/targetTemperature/set"

      current_temperature_topic: "echonetlite2mqtt/elapi/v2/devices/fe00000000000000000000000000000000/properties/roomTemperature"

      min_temp: 16
      max_temp: 30
      temp_step: 1

  sensor:
    - name: "AC_BedRoom_CumulativePower"
      unique_id: ac_bedroom_power_kwh
      state_topic: "echonetlite2mqtt/elapi/v2/devices/fe00000000000000000000000000000000/properties/consumedCumulativeElectricEnergy"
      unit_of_measurement: "kWh"
      device_class: energy
      state_class: total_increasing
      icon: mdi:flash
      
  select:
    - name: "AC_BedRoom_VerticalAirDirection"
      unique_id: ac_bedroom_airflow_direction_vertical
      command_topic: "echonetlite2mqtt/elapi/v2/devices/fe00000000000000000000000000000000/properties/airFlowDirectionVertical/set"
      state_topic: "echonetlite2mqtt/elapi/v2/devices/fe00000000000000000000000000000000/properties/airFlowDirectionVertical"
      options:
        - "uppermost"
        - "upperCenter"
        - "central"
        - "lowerCenter"
        - "lowermost"
      icon: mdi:arrow-up-down

name, unique idでエアコンの名前を付けます。またmodesとfan_modesで運転モードと風量を指定します。これは、エアコンによって選択肢が異なるので、ないものは消したり、あるものは増やしたりします。

さらに、最小・最大温度と温度設定幅を指定します。エアコンによってはリモコンからなら0.5℃で設定できたりしますが、ECHONET Liteでは温度設定幅は1℃になってしまうようで、ここでは1にします。

さらに、今回はclimate以外に、積算電力量と垂直方向風向の指定を追加します。ECHONETLite2MQTTの画面からプロパティ名(consumedCumulativeElectricEnergy、airFlowDirectionVertical)を見つけ、それをcommand_topicとstate_topicに指定します。電力量は単位とstate_classを指定しておきます。風向はプロパティの選択肢をoptionsとして指定すれば良いです。

これを、File Editorのconfiguration.yamlに追加し、開発者ツール→YAMLの再読み込みをすると、ダッシュボードにエアコン設定とセンサ・selectが現れますので、任意の部屋に割り付けたり、別のダッシュボードに追加するなりできるようになります。

つぎに、エコキュートの設定を作ります。エコキュートはclimateのようなまとまった設定はないので、表示・設定したいプロパティを1つずつ指定する必要があります。私は以下のプロパティを表示・設定するようにしました。

  • 残湯量: remainingWater
  • 動作状態: waterHeatingStatus, bathOperationStatusMonitor, hotWaterSupplyStatus
  • 積算消費電力の表示: consumedCumulativeElectricEnergy
  • 温水器湯温の表示: tankWaterTemperature
  • 給湯温度の表示と設定: targetSuppliedWaterTemperature
  • 自動沸き上げ: automaticWaterHeating
  • 自動湯はり: automaticBathOperation
mqtt:
  sensor:
    - name: "WaterHeater_RemainingWater"
      state_topic: "echonetlite2mqtt/elapi/v2/devices/fe00000000000000000000000000000000/properties/remainingWater"
      unit_of_measurement: "L"
      icon: mdi:water-boiler

    - name: "WaterHeater_HeatingStatus"
      state_topic: "echonetlite2mqtt/elapi/v2/devices/fe00000000000000000000000000000000/properties/waterHeatingStatus"

    - name: "WaterHeater_BathOperationStatus"
      state_topic: "echonetlite2mqtt/elapi/v2/devices/fe00000000000000000000000000000000/properties/bathOperationStatusMonitor"

    - name: "WaterHeater_SupplyStatus"
      state_topic: "echonetlite2mqtt/elapi/v2/devices/fe00000000000000000000000000000000/properties/hotWaterSupplyStatus"

    - name: "WaterHeater_ElectricEnergy"
      state_topic: "echonetlite2mqtt/elapi/v2/devices/fe00000000000000000000000000000000/properties/consumedCumulativeElectricEnergy"
      unit_of_measurement: "kWh"
      icon: mdi:flash

    - name: "WaterHeater_TankTemperature"
      state_topic: "echonetlite2mqtt/elapi/v2/devices/fe00000000000000000000000000000000/properties/tankWaterTemperature"
      unit_of_measurement: "°C"
      icon: mdi:thermometer

    - name: "WaterHeater_TargetTemperature"
      state_topic: "echonetlite2mqtt/elapi/v2/devices/fe00000000000000000000000000000000/properties/targetSuppliedWaterTemperature"
      unit_of_measurement: "°C"
      icon: mdi:thermostat

  number:
    - name: "WaterHeater_SetTargetTemperature"
      unique_id: waterheater_set_target_temp
      state_topic: "echonetlite2mqtt/elapi/v2/devices/fe00000000000000000000000000000000/properties/targetSuppliedWaterTemperature"
      command_topic: "echonetlite2mqtt/elapi/v2/devices/fe00000000000000000000000000000000/properties/targetSuppliedWaterTemperature/set"
      unit_of_measurement: "°C"
      min: 30
      max: 60
      step: 1
      icon: mdi:thermostat

  select:
    - name: "WaterHeater_AutoWaterHeatingMode"
      unique_id: waterheater_auto_heating_mode
      state_topic: "echonetlite2mqtt/elapi/v2/devices/fe00000000000000000000000000000000/properties/automaticWaterHeating"
      command_topic: "echonetlite2mqtt/elapi/v2/devices/fe00000000000000000000000000000000/properties/automaticWaterHeating/set"
      options:
        - "auto"
        - "manualNoHeating"
        - "manualHeating"
      icon: mdi:sync

  switch:
    - name: "WaterHeater_AutoBathOperation"
      unique_id: waterheater_auto_bath
      state_topic: "echonetlite2mqtt/elapi/v2/devices/fe00000000000000000000000000000000/properties/automaticBathOperation"
      command_topic: "echonetlite2mqtt/elapi/v2/devices/fe00000000000000000000000000000000/properties/automaticBathOperation/set"
      payload_on: "true"
      payload_off: "false"
      state_on: "true"
      state_off: "false"
      icon: mdi:shower

長いのですが、1つずつエアコンのsensorと同様に指定していきます。重要なのが、残湯量remainingWaterと、沸き上げ状態AutoWaterHeatingModeです。残湯量はunit_of_measurement: "L"としておきます。また沸き上げ状態は自動auto、満タン沸き上げmanualHeating、長期間停止manualNoHeatingの3つがあるようですので、それを記述しておきます。

こちらもyaml設定の読み込みをして信号名を変更すると、以下のように表示・操作できるようになります。

現在値のリクエス

エコキュートの設定をしたのですが、残湯量、タンク湯温、積算電力量がなぜかrequestをかけないと値更新されませんでした。三菱のエコキュート限定なのかもしれませんが、一定以上変化したり、なんらか動きがないと値が自動で更新されないようです。しかし、このあたりの値はHome Assistantに取り込み管理したいので、毎分データを取得するようにします。

いくつか方法があるのですが、ここではautomation.yamlに以下を記載し、1分毎にrequestのMQTTtopicを投げるオートメーションを追加することにしました。

minutesを/1とすることで、毎分実行してくれるようになり、表示データが1分毎に更新されるようになりました。

- alias: "Request RemainingWater Periodically"
  trigger:
    - platform: time_pattern
      minutes: "/1"
  action:
    - service: mqtt.publish
      data:
        topic: "echonetlite2mqtt/elapi/v2/devices/fe00000000000000000000000000000000/properties/remainingWater/request"
        payload: "get"

- alias: "Request CumulativeElectricEnergy Periodically"
  trigger:
    - platform: time_pattern
      minutes: "/1"
  action:
    - service: mqtt.publish
      data:
        topic: "echonetlite2mqtt/elapi/v2/devices/fe00000000000000000000000000000000/properties/consumedCumulativeElectricEnergy/request"
        payload: "get"

- alias: "Request TankWaterTemperature Periodically"
  trigger:
    - platform: time_pattern
      minutes: "/1"
  action:
    - service: mqtt.publish
      data:
        topic: "echonetlite2mqtt/elapi/v2/devices/fe00000000000000000000000000000000/properties/tankWaterTemperature/request"
        payload: "get"

まとめ

家庭のECHONET Lite対応機器を、ECHONETLite2MQTTを用いてHome Assistantで表示・操作できるようにしました。

つぎは、HomeAssistantでオートメーションによる制御を行ってみます。

*1:Home Assistant OSではなくSupervisedを用いたからか?