Power Button for a Proxmox VM

https://github.com/dougiteixeira/proxmoxve

I am trying to create a power button for each of my proxmox vm’s. I want to support 3 states on my button - power on, hibernate and long press to power off (with appropriate icons to indicate the 3 states).

I did some testing and the power on event has to understand the current state of the vm (in this case sensor.qemu_win11_101_status) in order to issue a either start or restore event (i.e restore won’t start a stopped vm and start won’t startup a hibernating vm). I tried creating a button card with this logic but it doesn’t work. Excuse my lack of understanding how service calls work but the code below should give an idea of what I want to do. Not sure if I need to move the the hacs button-card?

type: button
name: Proxmox Win11 Power Button
tap_action:
  action: call-service
  service: >
    {% if is_state('sensor.qemu_win11_101_status', 'stopped') %}
      button.qemu_win11_101_start
    {% elif is_state('sensor.qemu_win11_101_status', 'suspended') %}
      button.qemu_win11_101_resume 
    {% else %}
      button.qemu_win11_101_hibernate
    {% endif %}
hold_action:
  action: call-service
  service: button.qemu_win11_101_stop

Any takers? I’m still stuck on this an really want to get this going.

I suggest you use a script to simplify things, here is an example.

Code for the script:

alias: VM start/stop
description: ""
fields:
  status:
    selector:
      entity:
        domain: sensor
        integration: proxmoxve
    name: Status
    required: true
  start:
    selector:
      entity:
        domain: button
        integration: proxmoxve
    name: Command start
    required: true
  stop:
    selector:
      entity:
        domain: button
        integration: proxmoxve
    name: Command stop
    required: true
  resume:
    selector:
      entity:
        domain: button
        integration: proxmoxve
    name: Command resume
    required: true
  hibernate:
    selector:
      entity:
        domain: button
        integration: proxmoxve
    name: Command hibernate
    required: true
  reset:
    selector:
      entity:
        domain: button
        integration: proxmoxve
    name: Command reset
    required: true
sequence:
  - choose:
      - conditions:
          - condition: template
            value_template: "{{is_state( status, 'stopped') }}"
        sequence:
          - action: button.press
            metadata: {}
            data: {}
            target:
              entity_id: "{{ start }}"
      - conditions:
          - condition: template
            value_template: "{{is_state( status, 'suspended') or is_state( status, 'paused') }}"
        sequence:
          - action: button.press
            metadata: {}
            data: {}
            target:
              entity_id: "{{ resume }}"
      - conditions:
          - condition: template
            value_template: "{{is_state( status, 'running') }}"
        sequence:
          - action: button.press
            metadata: {}
            data: {}
            target:
              entity_id: "{{ hibernate }}"
    default:
      - action: button.press
        metadata: {}
        data: {}
        target:
          entity_id: "{{ reset }}"

Code for the card:

show_name: true
show_icon: false
type: button
tap_action:
  confirmation:
    text: Do you confirm the command execution?
  action: perform-action
  perform_action: script.vm_start_stop
  target: {}
  data:
    status: sensor.qemu_123_status
    start: button.qemu_123_start
    stop: button.qemu_123_stop
    resume: button.qemu_123_resume
    hibernate: button.qemu_123_hibernate
    reset: button.qemu_123_reset
name: VM Control
entity: sensor.qemu_123_status
show_state: true
hold_action:
  confirmation:
    text: Do you confirm the command execution?
  action: perform-action
  perform_action: button.press
  target:
    entity_id: button.qemu_123_shutdown
1 Like

Thanks Doug - I used a HACS button-card to add some animation that gives me a visual when containers or vm’s are running or stopped, plus added some basic information on the button showing processor and memory % usage.

Is there a way to get the some of the sensors (i.e status) to update on a faster interval? There seems to be a long lag when I fire up a vm for the lovelace button to change state.

Also for anybody interested, here your script modified for containers (without notification prompts). This one just supports start and (graceful) container shutdown:

alias: CT start-stop 
fields:
  status:
    selector:
      entity:
        domain: binary_sensor
        integration: proxmoxve
    name: Status
    required: true
  start:
    selector:
      entity:
        domain: button
        integration: proxmoxve
    name: Command start
    required: true
  stop:
    selector:
      entity:
        domain: button
        integration: proxmoxve
    name: Command shutdown
    required: true
sequence:
  - choose:
      - conditions:
          - condition: template
            value_template: "{{is_state( status, 'off') }}"
        sequence:
          - action: button.press
            metadata: {}
            data: {}
            target:
              entity_id: "{{ start }}"
      - conditions:
          - condition: template
            value_template: "{{is_state( status, 'on') }}"
        sequence:
          - action: button.press
            metadata: {}
            data: {}
            target:
              entity_id: "{{ stop }}"

and the button yaml (in a horizontal stack):

type: horizontal-stack
cards:
  - type: custom:button-card
    entity: binary_sensor.lxc_docker_103_status
    name: CT Frigate
    show_state: false
    show_name: true
    tap_action:
      action: call-service
      service: script.ct_start_stop
      service_data:
        status: binary_sensor.lxc_docker_103_status
        start: button.lxc_docker_103_start
        stop: button.lxc_docker_103_shutdown
    state:
      - value: "on"
        icon: mdi:fan
        styles:
          icon:
            - color: green
            - animation: rotating 2s infinite linear
      - value: "off"
        icon: mdi:fan
        styles:
          icon:
            - color: grey
    custom_fields:
      info: |
        [[[
          if (entity.state === 'on') {
            let cpuState = states['sensor.lxc_docker_103_cpu_used'].state;
            let cpuValue = parseFloat(cpuState);
            let cpu = isNaN(cpuValue) ? 'N/A' : Math.round(cpuValue) + '%';
            let memState = states['sensor.lxc_docker_103_memory_used_percentage'].state;
            let memValue = parseFloat(memState);
            let mem = isNaN(memValue) ? 'N/A' : Math.round(memValue) + '%';
            return `Running<br>C: ${cpu} / M:${mem}`;
          } else {
            return `Stopped<br>C: N/A M: N/A`;
          }
        ]]]
    styles:
      grid:
        - grid-template-areas: "\"i\" \"n\" \"info\""
        - grid-template-columns: 1fr
        - grid-template-rows: auto auto auto
      name:
        - font-size: 12px
      custom_fields:
        info:
          - font-size: 10px
          - padding: 5px

See: Increase refresh interval · dougiteixeira/proxmoxve · Discussion #431 · GitHub