Extracting values from Proxmox API using REST

I’m trying to pull data from Proxmox and create HA sensors (CPU.MEMORY.DISK…).

Proxmox API data is here:

{"data":[{"disk":3499233280,"status":"online","cpu":0.0938838498434457,"maxcpu":4,"ssl_fingerprint":"","maxmem":8046727168,"level":"","uptime":9387,"mem":2572218368,"maxdisk":31044079616,"type":"node","id":"node/prox","node":"prox"}]}

and my sensors yaml is here:

- platform: rest
  resource: https://192.168.1.20:8006/api2/json/nodes
  name: NUC API TEST
  verify_ssl: false
  method: GET
  username: ***
  password: ***
  value_template: '{{ value_json.data[0].mem }}'

Result for the sensor is always “UNKNOWN” what am i doing wrong?

EDIT:
For anyone interested, Proxmox requests usage of API token. Which can be acquired by this command: curl -k -d "username=root@pam&password=yourpassword" https://10.0.0.1:8006/api2/json/access/ticket

Then following REST command can be used to extract data (in this example memory usage):

- platform: rest
  resource: https://192.168.1.20:8006/api2/json/nodes
  name: NUC API TEST 
  verify_ssl: false
  headers:
    Cookie: 'PVEAuthCookie=PVE:root@pam:5E1BAB79::Cxmr3rm3SRDww+.......................'
    content-Type: application/json
  method: GET
  value_template: '{{value_json.data[0].mem}}'

The remaining is to figure out how to renew cookie as it has lifetime of 2 hours.

4 Likes

Are you getting any related errors or warnings in the log? You can also enable debug for the rest sensor and check the details log…

logger:
  default: info
  logs:
    homeassistant.components.rest.sensor: debug

a bunch of those

2020-01-11 21:36:21 DEBUG (SyncWorker_2) [homeassistant.components.rest.sensor] Updating from https://192.168.1.20:8006/api2/json/nodes

Ok. What do you get for the state of the sensor if you remove (or temporarily comment out) value_template from the config?

Same thing, it must be that my JSON is incorrect

Same thing in the log, or same thing for the state, meaning unknown? If the former, I was asking about the latter.

yeah, same thing in the log , state is just empty now, no “unknown”

Ok, well that explains why a template using value_json isn’t working. It seems to be getting back an empty string, not the JSON string you expect.

I updated first post for anyone interested in Proxmox API access

1 Like

Could this be associated to an ON/OFF switch?
My idea is to create a switch to start or stop a particular VM.

Here is the command for ON/OFF switch to a VM inside Proxmox

    - platform: command_line
      switches:
        proxmox_vm:
          command_on: 'curl -X POST "https://10.10.1.100:8006/api2/json/nodes/$nodename/qemu/$vmid/status/start" -H "Cookie: PVEAuthCookie=PVE:root@pam:5EDFE09C::RZ3itdckiTad7NMx..............." -H "Content-Type: application/x-www-form-urlencoded" -H "CSRFPreventionToken: 5EDFE09C:riyCwV+............" --insecure'
          command_off: 'curl -X POST "https://10.10.1.100:8006/api2/json/nodes/$nodename/qemu/$vmid/status/shutdown" -H "Cookie: PVEAuthCookie=PVE:root@pam:5EDFE09C::RZ3itdckiTad7NMx9x.............." -H "Content-Type: application/x-www-form-urlencoded" -H "CSRFPreventionToken: 5EDFE09C:riyCwV+..........." --insecure'
1 Like

How did you get around the 2 hour expiration of the API ticket?

This shell saves new cookie to a file every hour, that i use that file for everything else.

proxcookie: "curl --silent --insecure --data 'username=root@pam&password=*******'  https://192.168.1.20:8006/api2/json/access/ticket  | jq --raw-output '.data.ticket' | sed 's/^/PVEAuthCookie=/' > proxapi.yaml"

Ah, can you please explain in more detail how to do this. I got the other part running but a bit unsure on how to do this.

Where do I put it in and do I need to create some new files?

  1. You add this line in your config
shell_command:

  proxcookie: "curl --silent --insecure --data 'username=root@pam&password=PASSWORDFORPROXMOX'  https://IPOFYOURPROXMOX:8006/api2/json/access/ticket  | jq --raw-output '.data.ticket' | sed 's/^/PVEAuthCookie=/' > proxapi.yaml"
  1. This creates python script in HA, then you use automation to execute script and create proxapi.yaml in your root folder, every 1 hour:
- id: 'get prox cookie'
  alias: 'Prox cookie'
  trigger: 
    platform: time_pattern
    hours: '/1'
  action:
    service: shell_command.proxcookie
  1. then you create sensor to use proxapi.yaml file for query, for example:
- platform: command_line
  command: curl --insecure -b $(cat proxapi.yaml) https://192.168.1.20:8006/api2/json/nodes
  name: Prox 20 Cpu
  value_template: '{{(value_json.data[0].cpu | float * 100 )| round(2) }}'
#  unit_of_measurement: %
  scan_interval: 60   
3 Likes

that should be hours: '*'?

You made my day!
and also if you use tokens instead of user/pass even better since you don’t need to deal with cookies and so on.
I extended it including command_state.

This is how my config looks like:

    - platform: command_line
      switches:
		proxmox_lxc:
		  command_on: "curl -X POST 'https://10.0.0.1:8006/api2/json/nodes/pve/lxc/102/status/start' -H 'Authorization: PVEAPIToken=USER@REALM!TOKENID=UUID' -H 'Content-Type: application/x-www-form-urlencoded'"
		  command_off: "curl -X POST 'https://10.0.0.1:8006/api2/json/nodes/pve/lxc/102/status/stop' -H 'Authorization: PVEAPIToken=USER@REALM!TOKENID=UUID' -H 'Content-Type: application/x-www-form-urlencoded'"
		  command_state: "curl -X GET 'https://10.0.0.1:8006/api2/json/nodes/pve/lxc/102/status/current' -H 'Authorization: PVEAPIToken=USER@REALM!TOKENID=UUID' -H 'Content-Type: application/x-www-form-urlencoded' 2>&1 | grep -oc 'running'"

More info about tokens here:
https://pve.proxmox.com/wiki/Proxmox_VE_API#API_Tokens

Also important to set proper rights for the token itself in proxmox: [SOLVED] - Empty API Response When Getting VMs | Proxmox Support Forum
I spend some time figuring out why above commands returned {“data”:null}

Than you!,

2 Likes