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.
-
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).
-
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.