Hey! I'm pretty new to Node-RED (and Node.JS), but have been loving using it for some home automations. My latest project is to pull hourly meter readings from the power company via an API that they provide. Someone else has done the hard work of developing the Node.JS library to access the information, but I'm trying to adapt that to Node-RED using the function node. I'm having some trouble converting it and was hoping someone could provide a little advice. The repo can be found at
github.com/apearson/southern-company-api.
I believe that the first part:
/* Importing Library */
import {SouthernCompanyAPI} from 'southern-company-api';
/* Or requiring for a script */
var SouthernCompanyAPI = require('../southern-company-api').SouthernCompanyAPI;`Preformatted text`
Is supposed to be configured in the new "Setup" window in Node-RED. I've created a module named "southern-company-api" and imported as "SouthernCompanyAPI". I've also commented out that section in the "On Message" tab where the rest of my code is. However, now I'm getting
TypeError: SouthernCompanyAPI is not a constructor
which I believe is due to the next section of code (populated with my login info in the actual code):
/* Instantiating API */
const SouthernCompany = new SouthernCompanyAPI({
username: 'username',
password: 'password',
accounts: ['123123123']
});
Is there a different function or construction I should be using in order to instantiate this constant "SouthernCompany"?
Are there any other obvious issues you see that I'm going to run into later in the code? Any help is appreciated!
/* Importing Library */
import {SouthernCompanyAPI} from 'southern-company-api';
/* Or requiring for a script */
var SouthernCompanyAPI = require('../southern-company-api').SouthernCompanyAPI;
/* Instantiating API */
const SouthernCompany = new SouthernCompanyAPI({
username: 'username',
password: 'password',
accounts: ['123123123']
});
/* Listening for login success */
SouthernCompany.on('connected', ()=>{
console.info('Connected...');
async function fetchMonthly() {
/* Getting Monthly Data */
const monthlyData = await SouthernCompany.getMonthlyData();
/* Printing Monthly Data */
console.info('Monthly Data', JSON.stringify(monthlyData));
}
fetchMonthly();
async function fetchDaily() {
/* Getting Daily Data */
const startDate = new Date(2020, 2, 1);
const endDate = new Date();
const dailyData = await SouthernCompany.getDailyData(startDate, endDate);
/* Printing daily data */
console.info('Daily Data', JSON.stringify(dailyData));
}
fetchDaily();
});
/* Listening for any errors */
SouthernCompany.on('error', console.error);
For some background, I'm using the Node-RED v2.2.2 add-on in HomeAssistant on a Raspberry Pi. I'm not sure what the Node.JS version is, but everything is as up to date as I can get it.