PVOutput.org Push

Leveraging ScottBOnline’s senseapi python at GitHub - scottbonline/sense: Sense Energy Monitor API

I use a weather module and then something i wrote to push the data collected to PVoutput.org

sense_api_pvoutput.py (1.1 KB)
weather.py (864 Bytes)

1 Like
import sys, subprocess
from datetime import datetime, date, time
from sense_api import Senseable
from weather import current_temp, min_temp, max_temp, forecast_text

DATE = datetime.now().strftime("%Y%m%d")
TIME = datetime.now().strftime("%H:%M")
username = 'email'
password = 'pass'
PVOutputURL = 'https://pvoutput.org/service/r2/addstatus.jsp'
PVOutputAPI = '83587u834jk'
PVOutputSID = '3543'

sense = Senseable(username,password)
active_power = str(sense.active_power).split('.')[0]
active_solar_power = str(sense.active_solar_power).split('.')[0]
active_voltage_split = str(sense.active_voltage).split(' ')[0]
active_voltage_strip = str(active_voltage_split).strip('[ ,')
active_voltage = active_voltage_strip[:5]
temp = round(float(current_temp),1)

PVOutputCURL = """curl -d "d={}" -d "t={}" -d "v4={}" -d "v2={}" -d "v5={}" -d "v6={}" -H "X-Pvoutput-APIkey:{}" -H "X-Pvoutput-SystemId:{}" {}""".format(DATE, TIME, active_power, active_solar_power, temp, active_voltage,PVOutputAPI, PVOutputSID, PVOutputURL)

subprocess.call(PVOutputCURL, shell=True)
print()
1 Like

I’m doing the same thing but with home-assistant.io (and the same python):
rest_command:
pvoutput_status:
url: ‘https://pvoutput.org/service/r2/addstatus.jsp
headers:
X-Pvoutput-Apikey:
X-Pvoutput-SystemId:
method: post
content_type: application/x-www-form-urlencoded
payload: “d={{now().strftime(‘%Y%m%d&t=%H:%M’)}}&v2={{states.sensor.energy_production_stats_mean.state|int}}&v4={{states.sensor.energy_usage_stats_mean.state|int}}&v5={{(states.sensor.pws_temp_f_stats_mean.state|float - 32)/1.8}}&v6={{states.sensor.
dehumidifier_voltage.state|float * 2}}”

cron as an automation is left as an exercise to the reader. I created a bunch of mean stats sensors to capture the 5-minutely data pvoutput wants.

1 Like

Just a note - the API should not be used this way.

Rather than call the authentication every time:
sense = Senseable(username,password)

Instead, call it once, then have a loop that uses the existing instance to get the latest information.

im only running the call once every 5 mins… how long does the key last? we can change the logic to use the key for x amount of time and then rekey after x time.

From what I’ve seen, it never expires. I’ve been using it in a homeassistant component that initializes only once and then reports data until the server is restarted

ok… ill do that and see how long it lasts… like your HA setup

AFAIK the key never expires. I’ve been using the same one for over a month.

What Home Assistant component is that?

This one maybe?

I have that one enabled but I was wondering if there was another component you can use to record the data better…

Home Assistant will log the data automatically if you set up a sensor. You could also pass the data to another component in HA if you needed.

1 Like

sense_api_pvoutput.py (1.2 KB)

Updated script with changes that work with latest Sense API Python and small other changes.


import sys, subprocess
from datetime import datetime, date, time
from sense_energy import Senseable
from weather import current_temp, min_temp, max_temp, forecast_text

DATE = datetime.now().strftime(“%Y%m%d”)
TIME = datetime.now().strftime(“%H:%M”)
username = ‘user’
password = ‘pass’
PVOutputURL = ‘https://pvoutput.org/service/r2/addstatus.jsp
PVOutputAPI = ‘PVAPI’
PVOutputSID = ‘PVSID’

sense = Senseable(wss_timeout=30,api_timeout=30)
sense.authenticate(username, password)
sense.update_realtime()
active_power = str(sense.active_power).split(‘.’)[0]
active_solar_power = str(sense.active_solar_power).split(‘.’)[0]
active_voltage_split = str(sense.active_voltage).split(’ ‘)[0]
active_voltage_strip = str(active_voltage_split).strip(’[ ,')
active_voltage = active_voltage_strip[:5]
temp = round(float(current_temp),1)
if active_solar_power < “0”:
active_solar_power = “0”

PVOutputCURL = “”“curl -d “d={}” -d “t={}” -d “v4={}” -d “v2={}” -d “v5={}” -d “v6={}” -H “X-Pvoutput-APIkey:{}” -H “X-Pvoutput-SystemId:{}” {}”“”.format(DATE, TIME, active_power, active_solar_power, temp, active_voltage,PVOutputAPI, PVOutputSID, PVOutputURL)

subprocess.call(PVOutputCURL, shell=True)
print()

1 Like

I apologize in advance for my ignorance, but in spite of doing a lot of googling, I can’t figure out how to get these Python scripts to work, so I hope the Sense Community can help.

I’m essentially trying to copy danderson’s setup to push to PVOutput.org. II think I’ve got ScottBOnline’s senseapi properly installed on my Windows server. But when I try to run sense_api_pvoutput.py I get this authentication error:

image

But as far as I can tell, I’ve properly entered my Sense username and password in the relevant part of senseable.py (assuming this is the relevant part and that I’ve entered my info correctly):

def authenticate(self, username, password):
auth_data = {
MyRealEmail@gmail.com”: username,
“MyRealPassword”: password
}

Any ideas about what’s going wrong and how to fix it?
Thanks!

I emailed you privately

You corrupted the auth_data structure, just call the authenticate function:
authenticate( “MyRealEmail@gmail.com”, “MyRealPassword”)