Using Python to download data

I read the online information about the API for the IoToWatt and I can manually pull the data from a web browser. I’m trying to automate the process using Python3. I tried to use the Python requests module, but received errors. How are others pulling this data using Python? I would love to see an example.

What are you trying to do?
Why do you want to use Python to do it?
What code did you use?
What error did you get?

I did the most basic query using query show;

url = 'http://iotawatt.local/query?show=series'
r = requests.get(url, verify=False)
inputs = r.json()

There was a stack trace that said (in a lot more words) Failed to establish a new connection…Name or service not known. Python seems to have an issue with DNS. The Ubuntu I was running it on had no issue, so I just used the IP address and Python was happy.

If you have access to ChatGPT or GitHub Copilot you could try them. Copilot struggled with how to call the Iotawatt API, since it doesn’t know it, yet.

I use PHP to retrieve IoTaWatt data and I’m sure Python can do something very similar - Execute curl command within a Python script - Stack Overflow

PHP example using the API Query syntax to pull the most recent, single row of data from IoTaWatt:

	$fp = fsockopen("iotawatt.local","80",$errCode,$errStr,1);
	if(is_resource($fp)) {
		$ch = "http://iotawatt.local/query?select=[time.local,I1Black.Watts,I1Red.Watts]&begin=s-5s&end=s&group=5s&group=5s&missing=0&format=csv	
		$handle = fopen($ch, "rb");
		$contents = stream_get_contents($handle);
		$wattAry = explode(",",$contents);   // csv is ',' comma separated data
	} else {
               // error....

For a batch of records, it’s similar but I add &limit=xxx so I don’t over-pull the max number of records IoTaWatt allows in a single query in conjunction with &begin and &end dates in a loop to pull as large a date range as I need.

You can just type the API Query in a browser and see the results to figure out the exact query you want. Like this for the query in the PHP example above…

Thanks for the input. I’ll give it a try when I get back home from my trip.
I’m tinkering while trying to enhance my limited Python skills. I figure if I can pull the data using Python, then I can run a script on the raspberry pi to automatically pull the data. Then I can analyze the data for my wall ovens to determine in real time if they are on. If they are on, I will use an output from the RPi to trigger a relay that will activate a non-alarm zone on my alarm system, which is a way for me to notify my home automation system that the oven is on. This way I can use the info to provide instant voice notifications at the proper times, such as when leaving the house or going to bed. I’ve already done something similar with my gas cooktop and it works great! No more leaving the house or going to bed with the gas on (something we had done twice in the past.)

I haven’t used PHP before, but it looks simple enough. Thanks for the info.

Looks can be deceiving. Where are you going to run the PHP?

If your development skills are limited, you are probably better off using a home automation system designed to not require coding. I believe Home Assistant has an integration with Iotawatt that some people find useful. It would save you the trouble of having to figure out the API call. It probably has a way to define an action too.

Okay, so today I had a few minutes to play with this a bit. I’ve got a bare-bones Python program that can pull the data and print it, so I think I have what I need to get started once I have a little more time. This is going to be fun!

import requests
import json

url = ‘http://iotaleft.local/query?select=[time.iso,Ovens,MainLPL,MainLPR,Dryer,Radon]&begin=d-1d&end=d&group=h&format=csv&header=yes

page = requests.get(url, verify=False)


this is the main reason why you should be using a “force multiplier” like Home Assistant. The question every developer/person has to ask is how much of this do I have to build do myself and how much to I find an easier way to do it. I still have the tube of power measuring ICs that I bought over a decade ago and was going to use to make an energy monitoring system. Luckily, I found IotaWatt many years ago and was wise enough to realize it would save me a bunch of time by utilizing all the work @overeasy had done creating it. While I don’t agree with every decision he made, he did create a product that meets all the requirements I really had.

If you really want to do the development, I would suggest you start with actually writing down your requirements. It should help you clarify how much of the system you actually need to build yourself and how much you can find that somebody has already done and documented how they did it. In the last two decades or so, I have only found a very few things that I have been interested in that no one else has not already done and documented.

Home Assistant ( is written in Python, so you can use your skills to add something that is missing, but I suspect you could do what you want without needing to use any Python (though understanding Python would likely come in handy if you need to debug something that wasn’t working as you expected).

HA is on my list to try out. I set up OpenHAB years ago, but didn’t really find its model to be very helpful and switched to using NodeRED. I think HA is easier to use for many things. My needs were mostly data translation and minimal but custom control and NoeRED is pretty good for that.

If your primary goal is to be forced to learn some more Python, the path you are going down might be okay, but without actual review of the code you create, how will you know if you are actually getting more knowledgeable?

Congratulations on getting a few simple lines to pull data.

Just to speak up for the alternate view here - no reason to run something like Home Assistant if all you need is a few data points and you have means / framework to use them. This is my case, I pull data points to add to my DIY web page dashboard.

I love that IoTaWatt let’s me do this :slight_smile:

Python + scripts run everywhere.

I do agree that 1) it’s fun to just fool around and explore options but also 2) its good to clarify you’re goals and this will inform where to put your efforts and perhaps using HA is the way to go.

I appreciate everyone’s opinions and advice. It’s good to hear what others have done and how they use the tools. I find this a good way to get new ideas.

My goals are clearly defined to me. With this effort, they are: 1) to broaden my Python skills, 2) complete a project that has practical usage. (We have left the oven on by mistake in the past and I’m making it a learning project to come up with a solution. My main goal is not the end product.) The project, to automate the detection of my electric ovens being on or off, is simply a motivating tidbit that tells my subconscious there is a reason for the effort. :slight_smile:

FYI - On a previous, an somewhat similar project (not involving IoToWatt), my main goal was to detect whether or not the gas was left on on my cooktop. (We left it on overnight one time and another time when we left the house.) In this case my main goal WAS the end product. I did research for a “canned solution”, but found none. So, I applied the skills I had to come up with a unique solution. I say unique because it was a solution that works great for me, but wouldn’t be practical for most others. In a nut shell, whenever one of the burners is on, there is an LED that illuminates under the knob. The circuitry for the LEDs is laid out in a way that you can’t get to it in any easy way, so I dropped the idea of pulling the signal directly from the voltage across the LED. So what I decided to do was install LDRs (photoresistors) under the knobs, right next to each LED to detect when ever an LED came on. I drilled a hole in the bottom of the cooktop to get the wires where I needed them and then up inside the wall to the cabinet above where I hid an Arduino to monitor the status of the LEDs, and fire a relay, which goes to one of the non-alarm zones of my alarm system. I use HomeSeer for my home automation and there is a plugin that allows it to talk to my alarm system, making things simple. Now, whenever I set my alarm system, if the cooktop has any burners on I get a voice announcement over the audio system. Works great!

Once I complete the oven/IoTaWatt project, I may add a little more automation to detect motion in the kitchen and use that, along with the knowledge that either the oven or cooktop are on, AND that there has been no motion in the kitchen for a period of time. This way I’ll be notified if I’m watching a movie (or doing something else) while inside the house (without arming the alarm system) and forget about the appliances.

Sorry, for being so long winded, but like I said, it is good to share ideas and experiences.

A very late response, but if you’re still interested in downloading IoTaWatt data with Python and have not yet implemented what you need, take a look at IoTaWatt_Access, a Python module providing an interface to the IoToWatt Query API as well as some utility scripts for downloading data and getting device status information.