Turn on/off virtual machines (unRAID/KVM) and best practice

I’m new to Home-Assistant (Hello everyone!) and as a starting project I wanted to be able to turn on/off my two virtual gaming machines running on unRAID. After searching the forums a bit I managed to create two switches that allowed me to control the two gaming machines and use ping to determine the status. So far so good. I have also been reading about structuring the code as “packages” as I like the idea of keeping code grouped together.

If anyone is interested here’s my current code:

# Switch
switch:
  - platform: template
    switches:
      kodipower:
        friendly_name: Kodi PC
        value_template: >-
          {% if is_state('timer.kodi_ping_delay', 'idle') %}
          {{ states("binary_sensor.kodi_ping") }}
          {% else %}
          {{ states("input_boolean.kodi_power_status") }}
          {% endif %}
        turn_on:
          - service: shell_command.kodi_turn_on
          - service: input_boolean.turn_on
            data:
              entity_id: input_boolean.kodi_power_status
          - service: timer.start
            entity_id: timer.kodi_ping_delay
        turn_off:
          - service: shell_command.kodi_turn_off
          - service: input_boolean.turn_off
            data:
              entity_id: input_boolean.kodi_power_status
          - service: timer.start
            entity_id: timer.kodi_ping_delay
        icon_template: mdi:desktop-classic

  - platform: template
    switches:
      gamerpower:
        friendly_name: Gamer PC
        value_template: >-
          {% if is_state('timer.gamer_ping_delay', 'idle') %}
          {{ states("binary_sensor.gamer_ping") }}
          {% else %}
          {{ states("input_boolean.gamer_power_status") }}
          {% endif %}
        turn_on:
          - service: shell_command.gamer_turn_on
          - service: input_boolean.turn_on
            data:
              entity_id: input_boolean.gamer_power_status
          - service: timer.start
            entity_id: timer.gamer_ping_delay
        turn_off:
          - service: shell_command.gamer_turn_off
          - service: input_boolean.turn_off
            data:
              entity_id: input_boolean.gamer_power_status
          - service: timer.start
            entity_id: timer.gamer_ping_delay
        icon_template: mdi:desktop-classic
       
# Shell Commands
shell_command:
  kodi_turn_on: 'ssh -o StrictHostKeyChecking=no [email protected] virsh start Kodi'
  kodi_turn_off: 'ssh -o StrictHostKeyChecking=no [email protected] virsh shutdown Kodi'

  gamer_turn_on: 'ssh -o StrictHostKeyChecking=no [email protected] virsh start Gamer'
  gamer_turn_off: 'ssh -o StrictHostKeyChecking=no [email protected] virsh shutdown Gamer'

# Timer  
timer:
  kodi_ping_delay:
    duration: '00:00:25'

  gamer_ping_delay:
    duration: '00:00:25'
    
# Input Boolean
input_boolean:
  kodi_power_status:
    name: Kodi Power Status

  gamer_power_status:
    name: Gamer Power Status

# Binary Sensor    
binary_sensor:
  - platform: ping
    host: 192.168.1.2
    name: kodi_ping
    scan_interval: 2
    count: 2
 
  - platform: ping
    host: 192.168.1.3
    name: gamer_ping
    scan_interval: 2
    count: 2
    
# Groups
group:
  kodi_pc:
    name: unRAID VMs
    control: hidden
    entities:
      - switch.kodipower
      - switch.gamerpower
      
# Customize
homeassistant:
  customize:
    # Hidden
    switch.unraid:
      hidden: true
    binary_sensor.kodi_ping:
      hidden: true
    input_boolean.kodi_power_status:
      hidden: true
    timer.kodi_ping_delay:
      hidden: true
    binary_sensor.gamer_ping:
      hidden: true
    input_boolean.gamer_power_status:
      hidden: true
    timer.gamer_ping_delay:
      hidden: true

My next idea is to implement logic that turns the unRAID on/off depending on these virtual machines.

  1. Use wake on lan to start the unRAID server when any of those two switches go from “off” to “on” and unRAID isn’t on already running (using ping). I would also need to implement another wait, so the switch doesn’t try SSH into the server before it’s up (say 30 seconds later).

  2. When all/both switches are set to off for more then 5 minutes, I want to turn off the server using another SSH command.

I have two questions here…
The first is… how should I proceed with integrating support for the server on/off function? Should I automation here that controls the server or is there a better way of archiving the same functionality? I feel that the “code” is more complex then it should need to be, but I’m also not sure how to improve it. Any tips on “best practice” would be nice here. I don’t really like the idea of creating a whole lot of “helper” items that are visible from the HA dashboard/list of items, but that’s maybe the way it’s supposed to work?

I though about removing most of the code and try to rebuild it using Node-Red, but is that wise? A part of me thinks it’s better to keep as much as possible inside HA as packages for easier sharing and not having to be depending on another system for “core functions” such as a switch. For complex automation routines I guess I’ll use external tools, but I would love to try to keep all switches and their logic in HA.

Any ideas would be highly appreciated and I hope it’s not a too “open” question. :slight_smile:

NeoID,

Thanks for this post. I am trying to implement your suggestions. Quick question are you running home assistant as a docker or hassio? I am having an issue with the Shell commands. How are these configured? Do you put these entries in the configuration.yaml? Is any other config necessary as I don’t see a password being sent.

Thanks,
Dan

At that time I was running HA inside Docker, but I have now switched to Hass.io. I’m about to remove UnRAID and go back to standalone machines, so I won’t be using this code anymore (I guess). SSH needs to be available (and it is in the docker image) and you’ll need to setup SSH keys so you can login without a password.

So how to you make the keys available to the docker? Did you map a path as part of the docker template? Trying to figure it out but having some issues.

Thanks,
Dan

hey @NeoID
have you made progress on this? Just started with Home Assistant on Unraid and would be happy for any cool Input

Sorry, I’ve stopped using UnRAID so I’m not using that solution anymore