lawrencefoley / evergy

A simple utility that you can use to access your Evergy account and retrieve you meter readings.
MIT License
31 stars 13 forks source link

idk #8

Closed davethepear closed 3 years ago

davethepear commented 3 years ago

I don't know if I'm stupid or it no longer works, but I've been beating my head against the wall for a while now. I'm pretty sure it installed, don't know how to import into HA, and running it directly ends up with this:

2021-08-19 06:20:44,360 - INFO - root -  - Logging in with username: myemail@email.com
2021-08-19 06:20:45,488 - ERROR - root -  - Must login first
Traceback (most recent call last):
  File "./kcpl.py", line 81, in <module>
    logging.info("Last usage data: " + str(data[-1]))
TypeError: 'NoneType' object has no attribute '__getitem__'
lawrencefoley commented 3 years ago

Hey, yeah I can try to help out. Fair warning though - I haven't used this in HA myself, but I just started using HA so I'll be working on creating an energy integration so people can use it.

It'd be helpful if you could share your script or how you're running it in HA. I just tried it with my account and it worked, however I did notice they've been doing maintenance on the website recently (#7 ) so it could have been down earlier.

davethepear commented 3 years ago

I guess I don't know how to use it at all. If I can figure out the command line I can probably make it work in HA, I also started using HA a few days ago.

davethepear commented 3 years ago

but if you can share how to use your program, I'll share how to make it work in HA (if I figure that part out)

kreene1987 commented 3 years ago

Hey @davethepear, you can run this via an integration called pyscript. I've installed it via HACS and then added as an integration into Home Assistant. I then use Visual Studio Code add-on to see the pyscript folder and copied in the kcpl.txt shared in another issue, but then renamed as kcpl.py.

Links: Hacs: https://hacs.xyz/docs/installation/prerequisites Pyscript: https://github.com/custom-components/pyscript This is my latest (working) version testing out some stuff: kcpl.txt

Note you need the following helpers: input_boolean.evergyloginstate input_number.evergy_account_number input_text.evergyuser input_text.evergypass input_datetime.evergystartdate input_number.evergy_usage - this is what I am using as the HA field for energy input_number.evergy_usage_today - for testing input_number.evergy_usage_yesterday - for testing input_number.evergy_account_number - for testing input_number.evergy_premise_id - for testing

Also see my handing of template sensors in #4. The last item is you need a last_reset which can be done via:

homeassistant:
  customize_glob:
    sensor.house_energy:
      last_reset: '1970-01-01T00:00:00+00:00'
davethepear commented 3 years ago

wow... I'll see what I can do with that. thanks!

kreene1987 commented 3 years ago

Honestly while it SEEMS complicated, if you take it one step at a time it's not that bad, and I can help along the way. I did figure out a LOT of the above yesterday, so hopefully we can get @lawrencefoley convinced that HA is amazing (which it is!) so he could code this up as a direct integration with home assistant or through HACS.

kreene1987 commented 3 years ago

Hey, yeah I can try to help out. Fair warning though - I haven't used this in HA myself, but I just started using HA so I'll be working on creating an energy integration so people can use it.

It'd be helpful if you could share your script or how you're running it in HA. I just tried it with my account and it worked, however I did notice they've been doing maintenance on the website recently (#7 ) so it could have been down earlier.

@lawrencefoley if you need HA-side assistance please let me know. Above summarizes MOST of the steps, but I threw a few links to threads elsewhere. I'm happy to help if we end up with a cleaner integration without having to go api --> input_number -- > sensor --> energy.

davethepear commented 3 years ago

yes, HA is pretty cool. I've already figured out some really cool things to do with it. I got HACS installed. Thanks :)

davethepear commented 3 years ago

but after the install command in the readme, I'm at a complete loss as to what to do next. there are NO instructions, nothing here.

davethepear commented 3 years ago

yay, I got it working... very cool. a certain special character in my password was making it mad, among other things.

davethepear commented 3 years ago

I edited kcpl.py -changed logging: logging.basicConfig(filename='/home/homeassistant/kcpl.txt', format="%(message)s", level=logging.INFO) -changed the credentials file to an exact location, so I could call it from cron -commented out things I didn't want to see -changed/added these:

logging.info("Last usage reading: " + str(data[-1]["usage"]))
logging.info("Peak Demand: " + str(data[-1]["peakDemand"]))
logging.info("Cost: " + str(data[-1]["cost"]))

then I got into my configuration.yaml and added this:

sensor:
# KCPL
  - platform: command_line
    name: KCPL
    command: 'cat /home/homeassistant/kcpl.txt'

admittedly I tried using > filename.txt but, yeah, python... couldn't get print() to parse right either, so I hijacked the logging... but... it shows up as a card on my main screen and updates every day, dunno how hard evergy likes being pounded, they sure give me one though.

davethepear commented 3 years ago

mm yeah, it doesn't clear it out first... I'm not very good with python. so it does what log files do and append. so I threw rm kcpl.txt in the bash script that I use to call the thing... meh, it works. I'm done for the evening, headache.

kreene1987 commented 3 years ago

Oh shoot, sorry man you are supposed to change the name to kcpl.py in the pyscript folder. My bad I missed that step!!! Can't attach .py files so I changed to .txt.

davethepear commented 3 years ago

oh it's in there... I wonder what it's up to. I'm not very good at this, I guess.

it's erroring out, something about aiohttp (even though it's installed)

davethepear commented 3 years ago

I'm done, well... I guess I am... for now... I'm never done. In configuration.yaml:

sensor evergy:
  - platform: command_line
    name: Last Reading
    command: cat /home/homeassistant/kcpl.txt | awk 'NR == 1'
  - platform: command_line
    name: Peak Demand
    command: cat /home/homeassistant/kcpl.txt | awk 'NR == 2'
  - platform: command_line
    name: Cost
    command: cat /home/homeassistant/kcpl.txt | awk 'NR == 3'

in groups.yaml

evergy:
  name: Evergy
  entities:
    - sensor.last_reading
    - sensor.peak_demand
    - sensor.cost

in customize.yaml

group.evergy:
  icon: mdi:lightning-bolt
sensor.last_reading:
  icon: mdi:speedometer
sensor.cost:
  icon: mdi:currency-usd
sensor.peak_demand:
  icon: mdi:exclamation-thick

in Lawrence's version of kcpl.py

#near the top
logging.basicConfig(filename='/home/homeassistant/kcpl.txt', format="%(message)s", level=logging.INFO)
#near the bottom
    logging.info(str(data[-1]["usage"]))
    logging.info(str(data[-1]["peakDemand"]))
    logging.info(str(data[-1]["cost"]))
it looks like this

I don't think I forgot anything... maybe... idk. It's a hack job, but maybe it's something that can be worked with.

lawrencefoley commented 3 years ago

@davethepear Great to see you got it working! Be sure to star the repo so I can let you know when I create a Home Assistant integration. (I think I will create another repo for that, but reference it in this repo).

tchesket commented 3 years ago

Hey guys, sorry to comment on a closed issue, but I can't seem to get this to work in my HA. I installed pyscript in HACS, placed the kcpl.py file in /config/pyscripts, put the credentials file in /config and in the pyscripts folder (with the correct credentials), manually created all the helpers you mentioned, installed the pyscripts integration. It just doesn't seem to do anything. Manually running the file with python seems to work okay though. I also tried the modified version of the kcpl file that @davethepear shared. It just ends up giving me "import something something" for the sensor values. I'm not sure what I'm doing wrong, is anyone willing to help me figure this out?

tchesket commented 3 years ago

@kreene1987 @kreene1987 Hey, any chance you could help me figure out what I'm doing wrong? I think I completed all the steps you mentioned here and in the other linked issues, it still doesn't seem to be doing anything though. The kcpl.py script seems to pull the correct data when run manually from the command line but I'm not sure what pyscript is doing (if anything). Not sure what I'm missing, I've done enough in HA to know my way around it but I am in no way an expert coder, if you could give me a hand I would really appreciate it

tchesket commented 3 years ago

So, correction, for whatever reason the python script isn't working anymore when ran from hassio via ssh. I thought it was. It is giving me an error:

File "/config/pyscript/kcpl.py", line 84, in @service NameError: name 'service' is not defined

I'm sure it is something simple I am missing. My HA is running on a raspi4 if that helps

tchesket commented 3 years ago

Okay so actually I think I managed to get it working following @davethepear 's method. About pyscript, how often does it run? I assume it will run automatically at a set interval? At the moment all I have done is manually run the kcpl.py to obtain the text file with the data I need for the sensors.

davethepear commented 3 years ago

I set mine to run a few times a day from cron...

tchesket commented 3 years ago

Thank you I got it working with something similar, just created an automation to run the python script rather than going through pyscript. Thanks for figuring all this out for me lol, looks like I'm good now.

davethepear commented 3 years ago

good. I didn't use pyscript either, it wasn't cooperating.

tboyce021 commented 2 years ago

Since this seems to be the best discussion about HA integration that I can find, I'll start here. I have this working with a custom component to create sensors that can be used with the energy management, but I'm a little unsure how to interpret some of the data from this.

Namely, in my case I get that usage + demand = cost. I assume then (I'm not very knowledgeable on energy usage/billing) based on that and the definitions from https://www.evergy.com/manage-account/billing/understanding-my-bill/bill-definitions, that usage represents the total consumption in kWh for the period, demand is "the amount of energy needed during the highest 60-minute interval" which is defined as being in kW (but since it's over a 60 minute period then I assume it ends up being kWh, allowing us to add it with usage?), and cost is just the sum of those (not sure what it actually represents but it doesn't seem to be $ like I was hoping).

Also, from my testing, demand seems to always equal peakDemand, so I'm not sure if there's some cases where they might actually differ. I don't get either of them for intervals other than daily, though. Perhaps they were intended to be different depending on intervals but that's not implemented or is currently broken.

So, are usage and demand/peakDemand the "important" values then? Is it possible to calculate the estimated monetary cost based on those values?

thebradleysanders commented 1 year ago

Home Assistant integration has been completed, can be seen at https://github.com/thebradleysanders/evergy_home_assistant