I want to use a Python script to set DemandShaper & openEVSE parameters rather than having to use the webpages to do this.
Of particular interest is the target_soc which currently must be set by dragging the light green bar on the DemandShaper webpage.
The following script is a work-in-progress which works OK until the last line …
# PURPOSE ...
#
# ---------------------------------------------
import requests, json
from requests.structures import CaseInsensitiveDict
# Emoncms server configuration ...
emoncms_apikey = "18f634838244c193b816f51aabe048da" # For Node 15 - use appropriate key
emoncms_server = "http://127.0.0.1"
username = "emonpi"
password = "emonpimqtt2016"
# Get the Demand Shaper data ...
headers = CaseInsensitiveDict()
headers["Accept"] = "application/json"
headers["Authorization"] = "Bearer APIKEY"
headers["Content-Type"] = "application/json"
params = (
('device', 'openevse'),
('apikey', emoncms_apikey),
{'username', username},
{'password', password}
)
reply = requests.get(emoncms_server+"/demandshaper/get", params=params)
jsonstr = reply.text.rstrip()
ds_data = json.loads(jsonstr)
print(reply.status_code)
print (ds_data)
# Extract relevant data from the json
data1 = ds_data['schedule']['settings']['ctrlmode']
data2 = ds_data['schedule']['settings']['on_completion']
data3 = ds_data['schedule']['settings']['target_soc']
data4 = ds_data['schedule']['settings']['divert_mode']
print (data1)
print (data2)
print (data3)
print (data4)
# Do tests and make any changes here ...
# First test ...
di3 = 0.8
ds_data['schedule']['settings']['target_soc'] = di3
print (ds_data) # IT WORKS UPTO & INCL THIS LINE ... target_soc has been modified to 0.8
# Script fails at this point - Status Code = 406
reply = requests.post(emoncms_server+"/demandshaper/post", headers=headers, params=params, data=json.dumps(ds_data))
print(reply.status_code)
Here’s the output …
pi@emonpi-node-15:~/ev-charging-scripts $ python3 set-DemandShaper.py
200
{'schedule': {'settings': {'device': 'openevse', 'device_type': 'openevse', 'device_name': 'openevse', 'ctrlmode': 'smart', 'period': 5.108695652173913, 'end': 8, 'end_timestamp': 1608451200, 'interruptible': 0, 'runone': 0, 'on_completion': 'smart', 'forecast_config': [{'name': 'octopusagile', 'weight': 1, 'gsp_id': 'H'}], 'forecast_units': 'generic', 'timer': [{'start': 0, 'end': 0}, {'start': 0, 'end': 0}], 'ip': '', 'soc_source': 'input', 'battery_capacity': 75, 'charge_rate': 6.9, **'target_soc': 0.9,** 'current_soc': 0.43, 'balpercentage': 0.9, 'baltime': 2, 'car_economy': 3.4, 'charge_energy': 35.25, 'charge_distance': 119.8, 'distance_units': 'miles', 'ovms_vehicleid': '', 'ovms_carpass': '', 'divert_mode': 1}, 'runtime': {'started': True, 'periods': [{'start': [1608427800, 1.5], 'end': [1608446191, 6.6]}], 'timeleft': 18391.304347826088}}}
smart
smart
0.9
1
{'schedule': {'settings': {'device': 'openevse', 'device_type': 'openevse', 'device_name': 'openevse', 'ctrlmode': 'smart', 'period': 5.108695652173913, 'end': 8, 'end_timestamp': 1608451200, 'interruptible': 0, 'runone': 0, 'on_completion': 'smart', 'forecast_config': [{'name': 'octopusagile', 'weight': 1, 'gsp_id': 'H'}], 'forecast_units': 'generic', 'timer': [{'start': 0, 'end': 0}, {'start': 0, 'end': 0}], 'ip': '', 'soc_source': 'input', 'battery_capacity': 75, 'charge_rate': 6.9, **'target_soc': 0.8,** 'current_soc': 0.43, 'balpercentage': 0.9, 'baltime': 2, 'car_economy': 3.4, 'charge_energy': 35.25, 'charge_distance': 119.8, 'distance_units': 'miles', 'ovms_vehicleid': '', 'ovms_carpass': '', 'divert_mode': 1}, 'runtime': {'started': True, 'periods': [{'start': [1608427800, 1.5], 'end': [1608446191, 6.6]}], 'timeleft': 18391.304347826088}}}
406
The target_soc has clearly been changed 0.9 > 0.8 but this has not been written back to the OS.
Apparently exit code 406 is unusual and covers diverse problems – which is not very helpful info.
Is my approach flawed?
Suggestions most welcome.