mcpField
H
ha-automation

ha-automation

github.com/nodnarbnitramその他1689 回インストール27 週間前に更新
リポジトリ

ha-automation について

Home Assistantでオートメーション、スクリプト、ブループリント、Jinja2テンプレートを作成するためのスキルです。設定例に加え、よくある誤りの防止とトラブルシューティングを扱います。

主な機能

  1. 01トリガーと条件のリファレンス
  2. 02アクションと実行モードの例
  3. 03Jinja2テンプレートのガイド
  4. 04ブループリント入力とセレクターのガイド
  5. 05一般的なエラーの対処法

活用シーン

  1. 01YAMLオートメーションの作成
  2. 02パラメーター付きブループリントの作成
  3. 03テンプレートやトリガーのデバッグ

インストール

コマンドをコピーしてターミナルで実行します。

$npx skills add https://github.com/nodnarbnitram/claude-code-extensions --skill ha-automation

インストール後、Claude Code で/ha-automationを入力して読み込まれたか確認します。

同梱ファイルREADME.md

詳細

Home Assistant 自動化エキスパート

Home Assistant の自動化、スクリプト、ブループリント、Jinja2 テンプレートを自信を持って使いこなしましょう。

始める前に

このスキルでは、次のようなよくある自動化のミスを防ぎます。

  1. トリガー構文の誤り - 非推奨の形式や無効なエンティティ ID を使用する
  2. 条件演算子の不足 - and/or/not の接続詞を忘れる
  3. Jinja2 テンプレートのエラー - 未定義の変数や誤ったフィルターの使用
  4. ブループリントのパラメーター化の問題 - !input の置換漏れやセレクターの不整合

クイックリファレンス:自動化の構造

基本的な自動化の形式
automation: !include automations.yaml
単一の自動化ファイル
- alias: "My Automation"
  description: "What this automation does"
  trigger: ...
  condition: ...
  action: ...
  mode: single

トリガーのクイックリファレンス

状態トリガー
trigger:
  platform: state
  entity_id: light.bedroom
  from: "off"
  to: "on"
  for:
    minutes: 5
数値状態トリガー
trigger:
  platform: numeric_state
  entity_id: sensor.temperature
  above: 25
  below: 30
時刻トリガー
trigger:
  platform: time
  at: "10:30:00"
時刻パターントリガー
trigger:
  platform: time_pattern
  hours: "*/2"  # Every 2 hours
  minutes: 0
デバイストリガー
trigger:
  platform: device
  device_id: abc123...
  domain: light
  type: turned_on
イベントトリガー
trigger:
  platform: event
  event_type: call_service
  event_data:
    domain: light
MQTT トリガー
trigger:
  platform: mqtt
  topic: home/front_door/status
  payload: "opened"
Webhook トリガー
trigger:
  platform: webhook
  webhook_id: my_webhook_id
太陽トリガー
trigger:
  platform: sun
  event: sunrise
  offset: "-00:30:00"  # 30 min before sunrise
ゾーントリガー
trigger:
  platform: zone
  entity_id: device_tracker.john
  zone: zone.home
  event: enter
テンプレートトリガー
trigger:
  platform: template
  value_template: "{{ state_attr('light.bedroom', 'brightness') > 200 }}"

条件のクイックリファレンス

状態条件
condition:
  - condition: state
    entity_id: light.bedroom
    state: "on"
数値状態条件
condition:
  - condition: numeric_state
    entity_id: sensor.temperature
    above: 20
    below: 25
時刻条件
condition:
  - condition: time
    after: "08:00:00"
    before: "20:00:00"
    weekday:
      - mon
      - tue
      - wed
太陽条件
condition:
  - condition: sun
    after: sunrise
    before: sunset
テンプレート条件
condition:
  - condition: template
    value_template: "{{ states('light.bedroom') == 'on' }}"
条件の組み合わせ
condition:
  - condition: and
    conditions:
      - condition: state
        entity_id: light.bedroom
        state: "on"
      - condition: numeric_state
        entity_id: sensor.temperature
        above: 20
condition:
  - condition: or
    conditions:
      - condition: state
        entity_id: binary_sensor.motion
        state: "on"
      - condition: state
        entity_id: light.bedroom
        state: "on"

アクションのクイックリファレンス

サービス呼び出しアクション
action:
  - service: light.turn_on
    target:
      entity_id: light.bedroom
    data:
      brightness: 255
      color_temp: 366
サービス呼び出し(複数のターゲット)
action:
  - service: light.turn_on
    target:
      entity_id:
        - light.bedroom
        - light.living_room
      area_id: downstairs
Choose(If/Then/Else)
action:
  - choose:
      - conditions:
          - condition: state
            entity_id: light.bedroom
            state: "on"
        sequence:
          - service: light.turn_off
            target:
              entity_id: light.bedroom
      - conditions:
          - condition: state
            entity_id: light.bedroom
            state: "off"
        sequence:
          - service: light.turn_on
            target:
              entity_id: light.bedroom
    default:
      - service: notification.send
        data:
          message: "Unable to determine light state"
Repeat(ループ)
action:
  - repeat:
      count: 3
      sequence:
        - service: light.toggle
          target:
            entity_id: light.bedroom
        - delay:
            seconds: 1
遅延
action:
  - delay:
      minutes: 5
トリガーを待機
action:
  - wait_for_trigger:
      platform: state
      entity_id: binary_sensor.motion
      to: "off"
    timeout:
      minutes: 30
  - service: light.turn_off
    target:
      entity_id: light.bedroom
並列アクション
action:
  - parallel:
      - service: light.turn_off
        target:
          entity_id: light.bedroom
      - service: switch.turn_off
        target:
          entity_id: switch.fan

自動化モード

mode: single  # Default - cancel previous run
mode: restart  # Restart on retrigger
mode: queued  # Queue up to 10 retriggered runs
mode: parallel  # Allow multiple simultaneous runs
mode: queued  # with max: 10  # Limit queued runs

Jinja2 テンプレートのチートシート

よく使う変数
  • states('entity.id') - エンティティの状態を取得
  • state_attr('entity.id', 'attribute') - エンティティの属性を取得
  • now() - 現在の日時
  • as_timestamp('2024-01-01') - タイムスタンプに変換
  • trigger - トリガーオブジェクト(トリガーテンプレート内の場合)
フィルター
{{ value | float(0) }}  # Convert to float with default
{{ value | int(0) }}  # Convert to int with default
{{ value | round(2) }}  # Round to 2 decimal places
{{ value | timestamp_custom("%Y-%m-%d") }}  # Format timestamp
{{ value | replace("old", "new") }}  # Replace string
{{ value | lower }}  # Lowercase
{{ value | upper }}  # Uppercase
{{ value | length }}  # Get length
{{ list | list }}  # Convert to list
条件分岐
{% if states('light.bedroom') == 'on' %}
  Light is on
{% elif states('light.bedroom') == 'off' %}
  Light is off
{% else %}
  Unknown
{% endif %}
ループ
{% for entity in states.light %}
  {{ entity.name }}: {{ entity.state }}
{% endfor %}
数学演算
{{ 5 + 3 }}  # Addition
{{ 10 - 4 }}  # Subtraction
{{ 3 * 4 }}  # Multiplication
{{ 20 / 4 }}  # Division
{{ 17 % 5 }}  # Modulo (remainder)
文字列操作
{{ "hello " + "world" }}  # Concatenation
{{ value is string }}  # Type checking
{{ value is number }}
{{ value is defined }}  # Check if variable exists
{{ value | default("fallback") }}  # Provide default
テンプレートのデバッグ

Home Assistant の UI で「開発者ツール」>「テンプレート」を使うと、テンプレートをリアルタイムでテストできます。

ブループリント作成の基本

ブループリントの構造
blueprint:
  name: "Friendly Name"
  description: "What this blueprint does"
  domain: automation
  source_url: "https://github.com/user/repo/path/to/blueprint.yaml"

  input:
    my_light:
      name: "Light to control"
      description: "The light entity to control"
      selector:
        entity:
          domain: light

    brightness_level:
      name: "Brightness"
      description: "Brightness percentage (0-100)"
      selector:
        number:
          min: 0
          max: 100
          unit_of_measurement: "%"

    duration:
      name: "Duration"
      description: "How long to stay on"
      selector:
        number:
          min: 1
          max: 60
          unit_of_measurement: "minutes"

    my_bool:
      name: "Enable feature"
      selector:
        boolean:

trigger: ...
condition: ...
action:
  - service: light.turn_on
    target:
      entity_id: !input my_light
    data:
      brightness: !input brightness_level
ブループリントのセレクター
# Entity selector
selector:
  entity:
    domain: light

# Device selector
selector:
  device:
    integration: zwave
    manufacturer: Philips

# Area selector
selector:
  area:

# Number selector
selector:
  number:
    min: 0
    max: 100
    step: 5
    unit_of_measurement: "%"

# Text selector
selector:
  text:
    multiline: true

# Boolean selector
selector:
  boolean:

# Select (dropdown)
selector:
  select:
    options:
      - label: "Option 1"
        value: "value1"
      - label: "Option 2"
        value: "value2"

よくあるミスを避ける

❌ しないこと:古いトリガー形式を使う
trigger:
  - platform: state
    entity_id: light.bedroom
✅ すること:ネストされた構造を使う
trigger:
  - platform: state
    entity_id: light.bedroom
    to: "on"
❌ しないこと:トリガーと条件のロジックを混在させる
trigger:
  - platform: state
    entity_id: light.bedroom
    to: "on"
condition:
  - condition: state
    entity_id: light.bedroom
    to: "on"  # Redundant!
✅ すること:状態変化の検出にはトリガーを、状態の確認には条件を使う
trigger:
  - platform: state
    entity_id: light.bedroom
condition:
  - condition: numeric_state
    entity_id: sensor.temperature
    above: 20  # Additional check
❌ しないこと:サービス呼び出しで entity_id を指定し忘れる
action:
  - service: light.turn_on
    data:
      brightness: 255
✅ すること:ターゲットエンティティを指定する
action:
  - service: light.turn_on
    target:
      entity_id: light.bedroom
    data:
      brightness: 255

ベストプラクティス

  1. わかりやすいエイリアス名を使う - 自動化の目的を明確にします
  2. 説明を追加する - 自動化が存在する理由を説明します
  3. 開発者ツールでテストする - デプロイ前にテンプレートを検証します
  4. 複雑なロジックには choose を使う - 複数の自動化を使うより読みやすくなります
  5. 部屋や機能ごとに整理する - !include を使って大きなファイルを分割します
  6. 適切なモードを設定する - 用途に応じて single/restart/queued を選びます
  7. for: 条件を追加する - 短時間の状態変化による誤ったトリガーを防ぎます
  8. 柔軟性を高めるためにテンプレートを使う - ブループリントでパラメーターを渡せるようにします

トラブルシューティング

テンプレートに「Error」と表示される
  • 「開発者ツール」>「テンプレート」でテンプレートを確認します
  • エンティティ ID が存在することを確認します:homeassistant.entity_ids
  • オプションの属性には | default("value") フィルターを使います
自動化がトリガーされない
  • エンティティ ID と状態値を確認します(大文字と小文字は区別されます)
  • YAML バリデーターを使って trigger.yaml の構文を確認します
  • 「設定」>「開発者ツール」>「ログ」で自動化のログを確認します
ブループリントの入力が機能しない
  • !input variable_name の構文を確認します(YAML 1.2 タグ)
  • セレクターのタイプが入力タイプと一致していることを確認します
  • ブループリントが正しいフォルダーにあることを確認します:config/blueprints/automation/
Jinja2 の構文エラー
  • 未定義の変数がないか確認し、| default() を使います
  • フィルターの構文を確認します:value | filter_name(arg)
  • エスケープせずに引用符の中で引用符を使わないでください

公式リソース

コメント