Python Script for Sense + Weather Data Tool

All, I’ve created a Python file that gathers Sense data every 65 seconds as well as Weather data, and logs both minute by minute details and hourly summaries. See attached.

It works quite smoothly and it pretty reliable. I found that unless I waited slightly more than a minute between each requested sample of Sense data, I would just get the exact same data back again.

Please feel free to make use of what I’m put in the example file and tweak it for your own usage.

Mark
sense_python_example.py (12.8 KB)

4 Likes

Am trying to use your script on linux:
Filled out my email/password & generated API number

$ python3 /usr/local/bin/sense_python_example.py
Initialize Weather API
Initializing Sense library…
Authenticating with Sense servers…
Waiting for next minute to start…
Unable to call the weather API, retrying…
One of the weather calls failed, using zeros as values.
Traceback (most recent call last):
File “/usr/local/bin/sense_python_example.py”, line 242, in
if (get_temp_fahrenheit_float(observation1.weather) > max_temp1):
NameError: name ‘observation1’ is not defined

Any suggestion?

UPDATE

Tried again later on the day and this time it gave me some useful info:

Date/Time: 2022/01/08 19:12:05 Power Being Used: 1446 watts, Power Being Generated: 0 watts, net production -1446 watts
Date/Time: 2022/01/08 19:12:05 Auburn temp: 40.12 Enumclaw temp: 37.22 Auburn Cloud %: 40 Enumclaw Cloud %: 96

It seems I need to figure out how to set my openweather API key to the right location.
Will figure that out eventually :wink:

Hi there. Hmm, not sure what’s up. It could be that you can’t connect to the Weather API due to a firewall or other rule.

Try adding these lines to get_weather_data(), instead of “except:” to get more details on the exception that’s being returned.

except Exception as e:
    print('Failed to call the weather API: '+ str(e))
    print("Unable to call the weather API, retrying...")
    sleep(10)
    observation = mgr.weather_at_id(city_id)
1 Like

I don’t know if you have seen my updated info on the original posting.
Later on the same day “it started working” [TM]
I now have collected this output so far:

13016 Jan 8 23:59 detailed_data_2022_01_08.csv
70082 Jan 9 23:58 detailed_data_2022_01_09.csv
2594 Jan 10 09:00 summary_data_2022_01.csv
27133 Jan 10 09:06 detailed_data_2022_01_10.csv

Thanks a lot for you sharing your work.

1 Like

Hey, that’s great, glad it’s working!
I hope it will provide some good data for your to monitor your system.

Mark

3 Likes

Yesterdays stats:

Very informative info :wink:
Thanks again.

3 Likes

Glad you like it and find it useful!

By the way, what’s the size of your system?

Mark

2 Likes

I have seen 12kW AC at good times.
Part of it is down atm,
Waiting for a new 12kW DC hybrid inverter with 20kWh usable li-ion storage that will help beat the ludicrous Summer On-Peak: 69 cents/kWh from 2 p.m. to 8 p.m. from my utility.

1 Like

I would love to use this but I don’t know how to download the libraries this program relies on, I get:

ImportError: No module named sense_energy

on my Mac, I am running the latest MacOS, and Python 2.7

Do I need to be a LOT smarter to get this working, or am I just a single hint away from it going?

Hi there. Getting a proper python environment setup can be tricky (depending on if you are on Windows, Mac, or Linux), at least from my experience. I installed Visual Studio Code, and Python 3.10 first. Then, to install the sense_energy, use this command from https://github.com/scottbonline/sense:

pip install sense_energy

There are other tips/tricks at that web site too.

Hopefully that will help you get to the next step.
Mark

You need to have python3 installed (from google searches it seems it is)
then you need to install pip3 to get the other packages.

curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py
python3 get-pip.py
pip3 install sense_energy
pip3 install pywemo

3 Likes

Thanks all, that worked great, the Python3 trick was very useful, didn’t even realize I had both versions on the computer already.

I REALLY hope I am not just going to go line by line thru there complaining about errors, but I did the above installs and now I get:

ModuleNotFoundError: No module named ‘pyowm’

Assuming that there is also a pip3 install for that I tried and succeeded, that moved me forward, but not far enough. Then I got a:

Exception when initializing Sense library: Connection failure: HTTPSConnectionPool(host=‘api.sense.com’, port=443): Read timed out. (read timeout=5)

Which I thought was gonna be the end, but just for grins I tied it again and it WORKED. Thanks for getting me over the hump. I will now try to figure out talking to the Tesla API and see if I can actually make use of this to save me tweaking the charging as the clouds roll by or the sun begins to set.

1 Like

Hey, great to hear that it’s working for you!

My next steps are figuring out how get a notification to my phone or elsewhere if I have excess solar, so I can run specific power loads, kind of like your Tesla charging.

It would be great if you could share your Python code when you get the integration with your Tesla working, I’m sure there are others who would love to build off that as well!

Take care.
Mark

Don’t expect anything soon, I am still debugging all the changes I made to your code to remove the second location

I already built something similar to charge my golf cart.

1 Like

OK, maybe it didn’t take as long as I thought, still a few bugs in the system, but it seems to be doing the tracking of the free solar and adjusting the charging of the Tesla in response. When my hot tub or space heater or dish washer comes on the Tesla backs down or turns off, than usually later it turns back on, check it out, updates will appear here:

1 Like

Awesome, thanks for sharing!

Mark

Of course, now that I have finished most of the debugging of the code I realize I wanna add the feature where the charging setting for the Tesla is relayed to the Sense Monitor so it doesn’t show as Other. But I don’t know how to spoof the Kasa plugs in Python…

Should also figure out how to turn Kasa smart plugs off and on in the script like the WEMO Control python script does.

Code for pretending to be a reporting Kasa plug here:
I can’t give support (haven’t used yet). But others possibly can.

2 Likes