question

richardcarrey avatar image

VRM API python example

Could you post a simple example of a python program to log in to VRM API and return a piece of data, eg SoC of batteries please? I have been going round in circles for hours and just can't get the simplest thing to work!

VRM
2 |3000

Up to 8 attachments (including images) can be used with a maximum of 190.8 MiB each and 286.6 MiB total.

2 Answers
richardcarrey avatar image
richardcarrey answered ·

I have spent way too long on this! I came up with an answer and I will put it here for anyone else going down this rabbit hole. Probably me in a few months.


import requests

import json


login_url = 'https://vrmapi.victronenergy.com/v2/auth/login'

batterysummary_url = "https://vrmapi.victronenergy.com/v2/installations/93772/widgets/BatterySummary"

#use your own victron installation id instead of "93772"


login_string = '{"username":"name@domain.com","password":"password123"}'

#use the name and password you log in to VRM with


response = requests.post(login_url , login_string)

token = json.loads(response.text)["token"]

headers = {'X-Authorization': "Bearer " + token }


response = requests.get(batterysummary_url, headers=headers)

batterySOC = response.json()["records"]["data"]["51"]["valueFormattedWithUnit"]

print("Battery SOC is ",batterySOC)



The magic number "51" is the field for SOC. Look in the variable "response" for more such magic numbers


instead of batterysummar_url you can also try :


https://vrmapi.victronenergy.com/v2/installations/93772/widgets/BMSDiagnostics

https://vrmapi.victronenergy.com/v2/installations/93772/widgets/HistoricData

https://vrmapi.victronenergy.com/v2/installations/93772/widgets/IOExtenderInOut

https://vrmapi.victronenergy.com/v2/installations/93772/widgets/LithiumBMS

https://vrmapi.victronenergy.com/v2/installations/93772/widgets/MotorSummary

https://vrmapi.victronenergy.com/v2/installations/93772/widgets/PVInverterStatus

https://vrmapi.victronenergy.com/v2/installations/93772/widgets/SolarChargerSummary

https://vrmapi.victronenergy.com/v2/installations/93772/widgets/Status


This document gives some details : https://docs.victronenergy.com/vrmapi/overview.html#retrieve-diagnostic-data-all-current-installation-data

2 |3000

Up to 8 attachments (including images) can be used with a maximum of 190.8 MiB each and 286.6 MiB total.

richardcarrey avatar image
richardcarrey answered ·

Actually, forget all that above. Those magic numbers change over time! use the field "code" to tell find the element you want. You can get all the important stuff from the diags message :


import requests

import json

from datetime import datetime



login_url = 'https://vrmapi.victronenergy.com/v2/auth/login'

diags_url = "https://vrmapi.victronenergy.com/v2/installations/93772/diagnostics?count=1000"


login_string = '{"username":"name@domain.com","password":"password123"}'


response = requests.post(login_url , login_string)

token = json.loads(response.text)["token"]

headers = {'X-Authorization': "Bearer " + token }


response = requests.get(diags_url, headers=headers)

data = response.json()["records"]


batterySoC=[element['formattedValue'] for element in data if element['code']=="SOC"][0]

batterycurrent=[element['rawValue'] for element in data if element['code']=="I"][0]

solarpower=[element['formattedValue'] for element in data if element['code']=="Pdc"][0]

loadpower=[element['formattedValue'] for element in data if element['code']=="o1"][0]

generatorpower=[element['formattedValue'] for element in data if element['code']=="gs1"][0]

timestamp=(datetime.fromtimestamp([element['timestamp'] for element in data if element['code']=="SOC"][0]))


print("SoC : ",batterySoC)

print("Battery charge / discharge (A) :" ,batterycurrent)

print("Solar power : ",solarpower)

print("Load : ",loadpower)

print("Generator : ",generatorpower)

print("data taken at : ", timestamp)

2 |3000

Up to 8 attachments (including images) can be used with a maximum of 190.8 MiB each and 286.6 MiB total.