Closed ecoen66 closed 7 months ago
Here's the script I use to shutdown all running VMs on a host:
alias: Toggle VM host
mode: single
description: Shutdown all running VMs on a host
fields:
sensor_entity:
description: The sensor.esxi_vmhost to work with
example: sensor.esxi_vmhost_esxi222_home_lan
variables:
host_name: |
{{ state_attr(sensor_entity,'name') }}
vm_names: |
{{ states | selectattr('attributes.host_name', 'search', host_name)
| map(attribute='attributes.vm_name') | list }}
sequence:
- repeat:
for_each: "{{ vm_names }}"
sequence:
- service: esxi_stats.vm_power
data_template:
host: 192.168.1.220
vm: "{{ repeat.item }}"
command: shutdown
- wait_template: |
{{ states | selectattr('attributes.host_name', 'search', host_name)
| map(attribute='attributes.vm_name') | list | count < 1}}
Hi @ecoen66 Thank you for your contribution.
A few things:
Thanks. I removed my host_name, but kept vm_name as the proper, actionable name for the VM.
Unfortunately, having merged my code with service_calls branch? 0.7, my integration to vCenter no longer works at all. I now get the dreaded
cannot access local variable 'host_power_policy' where it is not associated with a value
Fixed the "host_power_policy" issue.
More issues. Now cannot call service: esxi_stats.vm_power...
VM UUID not found
I'm not digging this branch of code...
Ok, that was a PITA. I didn't realize that you had changed the service: esxi_stats.vm_power to use the mangled VM name rather than the proper name as you had in the master branch. Sorry for publishing so many updates here while debugging.
My script now reads:
alias: Toggle VM host
mode: single
description: Shutdown all running VMs on a host
fields:
sensor_entity:
description: The sensor.esxi_vmhost to work with
example: sensor.esxi_vmhost_esxi222_home_lan
variables:
host_name: |
{{ state_attr(sensor_entity,'name') }}
vm_names: |
{{ states | selectattr('attributes.host_name', 'search', host_name)
| rejectattr('attributes.state', '==', 'off') | map(attribute='attributes.name') | list }}
sequence:
- repeat:
for_each: "{{ vm_names }}"
sequence:
- service: esxi_stats.vm_power
data_template:
host: 192.168.1.220
vm: "{{ repeat.item }}"
command: shutdown
- wait_template: |
{{ states | selectattr('attributes.host_name', 'search', host_name)
| rejectattr('attributes.state', '==', 'off') | map(attribute='attributes.name') | list | count < 1}}
The latest example script you provided does not use the new attribute you created "vm_name", but instead uses the existing attribute "name". Is that a typo?
No, it's not a typo. That change in the script was due to the fact that you had changed the service: esxi_stats.vm_power to use the mangled VM name rather than the proper name as you had in the master branch. I do still use the new vm_name attribute in the Lovelace UI.
Added additional attributes: VM's proper name (for calling esxi_stats.vm_power), and the host name of the VMware host it is running on (enables searching for running VMs by host, allowing to shutdown the VMs prior to shutting down a host server by IMC or PDU).