Skip to content

Examples and Automations

This document provides practical examples of using the Kubernetes integration in Home Assistant automations and dashboards. Entity IDs below use production as the example cluster name — substitute the cluster name you chose when adding the integration (see Entity Naming).

Automation Examples

Time-Based Scaling

Stop Multiple Deployments at Night

automation:
  - alias: "Stop multiple deployments at night"
    trigger:
      platform: time
      at: "22:00:00"
    action:
      - service: kubernetes.stop_workload
        data:
          workload_names:
            - switch.production_default_development_api
            - switch.production_default_staging_api
            - switch.production_default_monitoring
          namespace: "default"

Start Multiple Deployments in the Morning

automation:
  - alias: "Start multiple deployments in the morning"
    trigger:
      platform: time
      at: "07:00:00"
    action:
      - service: kubernetes.start_workload
        data:
          workload_names:
            - switch.production_default_web_app
            - switch.production_default_api_server
            - switch.production_default_cache_service
          replicas: 3
          namespace: "default"

Scale Down During Off-Hours

automation:
  - alias: "Scale down for off-hours"
    trigger:
      platform: time
      at: "18:00:00"
    condition:
      condition: time
      weekday:
        - mon
        - tue
        - wed
        - thu
        - fri
    action:
      - service: kubernetes.scale_workload
        data:
          workload_names:
            - switch.production_default_web_frontend
            - switch.production_default_api_backend
          replicas: 1
          namespace: "default"

Set an Exact Replica Count

automation:
  - alias: "Scale web app up for business hours"
    trigger:
      platform: time
      at: "08:00:00"
    action:
      - service: number.set_value
        target:
          entity_id: number.production_default_web_app_replicas
        data:
          value: 3

Resource-Based Automations

Scale Up on High Load

automation:
  - alias: "Scale up on high CPU usage"
    trigger:
      platform: numeric_state
      entity_id: sensor.server_cpu_usage
      above: 80
      for:
        minutes: 5
    action:
      - service: kubernetes.scale_workload
        data:
          workload_name: switch.production_default_web_app
          namespace: "default"
          replicas: 5

Emergency Cluster Shutdown

automation:
  - alias: "Emergency cluster shutdown"
    trigger:
      platform: state
      entity_id: binary_sensor.ups_power_failure
      to: "on"
    action:
      - service: kubernetes.stop_workload
        data:
          workload_names:
            - switch.production_default_non_critical_app
            - switch.production_default_development_services
          namespace: "default"
      - delay: "00:02:00"
      - service: kubernetes.scale_workload
        data:
          workload_names:
            - switch.production_default_critical_app
          replicas: 1
          namespace: "default"

StatefulSet Examples

Scale Database StatefulSets

automation:
  - alias: "Scale database StatefulSets for maintenance"
    trigger:
      platform: time
      at: "02:00:00"
    condition:
      condition: time
      weekday:
        - sun
    action:
      - service: kubernetes.scale_workload
        data:
          workload_names:
            - switch.production_database_database_primary
            - switch.production_database_database_replica
          replicas: 1
          namespace: "database"

Stop Development Databases

automation:
  - alias: "Stop development databases over weekend"
    trigger:
      platform: time
      at: "19:00:00"
    condition:
      condition: time
      weekday:
        - fri
    action:
      - service: kubernetes.stop_workload
        data:
          workload_names:
            - switch.production_development_dev_postgres
            - switch.production_development_dev_redis
          namespace: "development"

Rollout Restart Examples

Restart Deployment via Button

automation:
  - alias: "Restart web app on button press"
    trigger:
      platform: state
      entity_id: input_button.restart_web_app
    action:
      - service: kubernetes.restart_workload
        data:
          workload_name: "web-app"
          namespace: "production"

Scheduled Rolling Restart

automation:
  - alias: "Weekly rolling restart of all workloads"
    trigger:
      platform: time
      at: "04:00:00"
    condition:
      condition: time
      weekday:
        - sun
    action:
      - service: kubernetes.restart_workload
        data:
          workload_names:
            - switch.production_default_web_app
            - switch.production_default_api_server
          namespace: "default"

Node Monitoring Automations

Alert on Node Not Ready

automation:
  - alias: "Alert on node not ready"
    trigger:
      - platform: state
        entity_id:
          - sensor.production_master_1
          - sensor.production_worker_1
          - sensor.production_worker_2
        to: "NotReady"
        for:
          minutes: 2
    action:
      - service: notify.mobile_app_your_phone
        data:
          title: "Kubernetes Node Alert"
          message: >
            Node {{ trigger.entity_id.split('.')[-1].replace('production_', '').replace('_', '-') }}
            is not ready. Status: {{ trigger.to_state.state }}
          data:
            priority: high

Monitor Node Resource Usage

Each node has a Memory Usage Percent sensor (relative to node capacity, needs metrics-server). Replace sensor.production_worker_1_memory_usage_percent with your node's entity.

automation:
  - alias: "High memory usage alert"
    trigger:
      - platform: numeric_state
        entity_id: sensor.production_worker_1_memory_usage_percent
        above: 90
        for: "00:05:00"
    action:
      - service: notify.persistent_notification
        data:
          title: "High Memory Usage"
          message: >
            {{ trigger.to_state.name }} is at {{ trigger.to_state.state }}%.

Log Node Status Changes

automation:
  - alias: "Log node status changes"
    trigger:
      - platform: state
        entity_id:
          - sensor.production_master_1
          - sensor.production_worker_1
          - sensor.production_worker_2
    condition:
      - condition: template
        value_template: "{{ trigger.from_state.state != trigger.to_state.state }}"
    action:
      - service: logbook.log
        data:
          name: "Kubernetes Node Status"
          message: >
            Node {{ trigger.entity_id.split('.')[-1].replace('production_', '').replace('_', '-') }}
            changed from {{ trigger.from_state.state }} to {{ trigger.to_state.state }}
          entity_id: "{{ trigger.entity_id }}"

Dashboard Examples

Basic Cluster Overview

views:
  - title: "Kubernetes Cluster"
    path: kubernetes
    cards:
      - type: entities
        title: "Cluster Status"
        entities:
          - entity: sensor.production_pods_count
            name: "Running Pods"
          - entity: sensor.production_nodes_count
            name: "Cluster Nodes"
          - entity: sensor.production_deployments_count
            name: "Active Deployments"
          - entity: sensor.production_statefulsets_count
            name: "StatefulSets"
          - entity: sensor.production_cronjobs_count
            name: "CronJobs"
          - entity: binary_sensor.production_cluster_health
            name: "Cluster Health"

Deployment Control Dashboard

views:
  - title: "Deployment Control"
    path: kubernetes-deployments
    cards:
      - type: entities
        title: "Production Deployments"
        entities:
          - switch.production_default_web_app
          - switch.production_default_api_server
          - switch.production_default_cache_service
      - type: entities
        title: "Replica Counts"
        entities:
          - number.production_default_web_app_replicas
          - number.production_default_api_server_replicas
      - type: entities
        title: "Development Services"
        entities:
          - switch.production_default_dev_api
          - switch.production_default_test_runner

Resource Monitoring

views:
  - title: "Resource Monitoring"
    path: kubernetes-resources
    cards:
      - type: gauge
        entity: sensor.production_pods_count
        min: 0
        max: 50
        name: "Pod Count"
        severity:
          green: 0
          yellow: 30
          red: 45
      - type: history-graph
        entities:
          - sensor.production_pods_count
          - sensor.production_deployments_count
        hours_to_show: 24

Node Monitoring Dashboard

views:
  - title: "Node Status"
    path: kubernetes-nodes
    cards:
      - type: entities
        title: "Node Overview"
        entities:
          - entity: sensor.production_master_1
            name: "Master Node"
          - entity: sensor.production_worker_1
            name: "Worker Node 1"
          - entity: sensor.production_worker_2
            name: "Worker Node 2"
        show_header_toggle: false
      - type: custom:auto-entities
        card:
          type: entities
          title: "All Cluster Nodes"
        filter:
          include:
            # Filtering by the OS_image attribute (unique to node sensors) instead
            # of an entity_id wildcard, since node sensor IDs no longer have a
            # distinguishing prefix (see Entity Naming) and a "sensor.production_*"
            # wildcard would also match the count sensors.
            - attributes:
                OS_image: "*"
        sort:
          method: name
      - type: markdown
        content: >
          ## Node Resources

          **{{ states('sensor.production_worker_1') }}** Worker 1:
          - Memory: {{ state_attr('sensor.production_worker_1', 'memory_allocatable_(GiB)') }} / {{ state_attr('sensor.production_worker_1', 'memory_capacity_(GiB)') }}
          - CPU: {{ state_attr('sensor.production_worker_1', 'CPU') }}
          - Internal IP: {{ state_attr('sensor.production_worker_1', 'internal_IP') }}
          - OS: {{ state_attr('sensor.production_worker_1', 'OS_image') }}

Script Examples

Deployment Management Scripts

Scale Environment

script:
  scale_production_environment:
    alias: "Scale Production Environment"
    sequence:
      - service: kubernetes.scale_workload
        data:
          workload_names:
            - switch.production_default_web_frontend
            - switch.production_default_api_backend
            - switch.production_default_worker_queue
          replicas: "{{ replicas | default(3) }}"
          namespace: "default"

Rolling Restart

script:
  rolling_restart_deployment:
    alias: "Rolling Restart Deployment"
    sequence:
      - service: kubernetes.scale_workload
        data:
          workload_name: "{{ workload_name }}"
          namespace: "{{ namespace }}"
          replicas: 0
      - delay: "00:00:30"
      - service: kubernetes.scale_workload
        data:
          workload_name: "{{ workload_name }}"
          namespace: "{{ namespace }}"
          replicas: "{{ original_replicas | default(1) }}"

Notification Examples

Deployment Failure Alerts

automation:
  - alias: "Alert on deployment failure"
    trigger:
      platform: state
      entity_id: switch.production_default_critical_app
      to: "off"
      for:
        minutes: 2
    action:
      - service: notify.mobile_app
        data:
          title: "Deployment Alert"
          message: "Critical app deployment is down!"
          data:
            priority: high

Problem Pod Alerts

The Problem Pods sensor counts every pod that is currently broken (CrashLoopBackOff, ImagePullBackOff, failed, unschedulable, …) and lists them in its pods attribute, so one automation covers the whole cluster:

automation:
  - alias: "Notify on problem pods"
    trigger:
      platform: numeric_state
      entity_id: sensor.production_problem_pods
      above: 0
      for:
        minutes: 5
    action:
      - service: notify.mobile_app
        data:
          title: "Kubernetes: problem pods"
          message: >
            {% for pod in state_attr('sensor.production_problem_pods', 'pods') %}
            {{ pod.namespace }}/{{ pod.name }}: {{ pod.reason }}
            {% endfor %}

Restart Loop Alerts

Problem Pods only sees the current state, so a pod that is OOMKilled every few minutes and restarts cleanly can look healthy each time it is read. Restarting Pods catches it: it lists pods with at least 3 restarts whose last restart was within the past hour.

automation:
  - alias: "Notify on restarting pods"
    trigger:
      platform: numeric_state
      entity_id: sensor.production_restarting_pods
      above: 0
    action:
      - service: notify.mobile_app
        data:
          title: "Kubernetes: pods restarting"
          message: >
            {% for pod in state_attr('sensor.production_restarting_pods', 'pods') %}
            {{ pod.namespace }}/{{ pod.name }}: {{ pod.restarts }} restarts,
            last {{ pod.last_terminated_reason }} (exit {{ pod.last_terminated_exit_code }})
            {% endfor %}

Failed Job Alerts

Failed Jobs lists Jobs that failed for good (retries exhausted or deadline exceeded) within the last 24 hours, including runs of CronJobs. A failed CronJob run disappears from the list once a later run succeeds.

automation:
  - alias: "Notify on failed jobs"
    trigger:
      platform: numeric_state
      entity_id: sensor.production_failed_jobs
      above: 0
    action:
      - service: notify.mobile_app
        data:
          title: "Kubernetes: job failed"
          message: >
            {% for job in state_attr('sensor.production_failed_jobs', 'jobs') %}
            {{ job.namespace }}/{{ job.name }}{% if job.cronjob %} (CronJob {{ job.cronjob }}){% endif %}: {{ job.reason }}
            {% endfor %}

To catch runs that never happened at all, check the CronJob's last_successful_time instead:

automation:
  - alias: "Nightly backup missed"
    trigger:
      platform: time
      at: "08:00:00"
    condition:
      - condition: template
        value_template: >
          {% set last = state_attr('sensor.production_backup', 'last_successful_time') %}
          {{ last is none or now() - as_datetime(last) > timedelta(hours=26) }}
    action:
      - service: notify.mobile_app
        data:
          title: "Kubernetes: backup missed"
          message: "No successful backup run in the last 26 hours."

Degraded Workloads (workloads attribute) and Nodes Under Pressure (nodes attribute) work the same way. Keep the for: delay on Degraded Workloads in particular: every rolling update briefly has fewer available replicas than desired, so without it the automation fires on each deploy.

The sidebar panel keeps its tab and filters in the URL, so a notification can open the Pods tab already filtered. The mobile app opens the uri of a notification action (or clickAction on Android and url on iOS for a tap on the notification itself) inside Home Assistant:

automation:
  - alias: "Notify on problem pods with a panel link"
    trigger:
      platform: numeric_state
      entity_id: sensor.production_problem_pods
      above: 0
      for: "00:05:00"
    action:
      - service: notify.mobile_app
        data:
          title: "Kubernetes: problem pods"
          message: "{{ states('sensor.production_problem_pods') }} pod(s) are not healthy."
          data:
            clickAction: "/kubernetes_panel/pods?phase=problems&namespace=default"
            url: "/kubernetes_panel/pods?phase=problems&namespace=default"  # iOS
            actions:
              - action: URI
                title: "Open failed jobs"
                uri: "/kubernetes_panel/workloads?category=jobs&status=degraded"

See URL scheme for the tabs and query parameters.

Cluster Health Monitoring

automation:
  - alias: "Cluster health alert"
    trigger:
      platform: state
      entity_id: binary_sensor.production_cluster_health
      to: "off"
    action:
      - service: notify.slack
        data:
          message: "🚨 Kubernetes cluster is unhealthy!"
          target: "#devops"

Advanced Examples

Conditional Scaling Based on Day

automation:
  - alias: "Smart scaling based on day"
    trigger:
      platform: time
      at: "08:00:00"
    action:
      - choose:
          - conditions:
              - condition: time
                weekday:
                  - mon
                  - tue
                  - wed
                  - thu
                  - fri
            sequence:
              - service: kubernetes.scale_workload
                data:
                  workload_name: switch.production_default_business_app
                  namespace: "default"
                  replicas: 5
          - conditions:
              - condition: time
                weekday:
                  - sat
                  - sun
            sequence:
              - service: kubernetes.scale_workload
                data:
                  workload_name: switch.production_default_business_app
                  namespace: "default"
                  replicas: 2

Multi-Environment Management

script:
  promote_to_production:
    alias: "Promote to Production"
    sequence:
      - service: kubernetes.stop_workload
        data:
          workload_name: "{{ workload_name }}"
          namespace: "staging"
      - delay: "00:01:00"
      - service: kubernetes.start_workload
        data:
          workload_name: "{{ workload_name }}"
          namespace: "production"
          replicas: 3

These examples demonstrate the flexibility of the Kubernetes integration for automating cluster management tasks directly from Home Assistant.