flavio-fernandes / mqtt2kasa

Python-based project that provides wrapper to python-kasa for MQTT access
MIT License
30 stars 14 forks source link

feature request: publish EmeterStatus #6

Closed MaxRower closed 1 year ago

MaxRower commented 1 year ago

What's really missing: mqtt2kasa does not publish the EmeterStatus, which is reported by kasa.

Output of kasa --host hs110 --type plug ` == Current State ==

`
flavio-fernandes commented 1 year ago

hi @MaxRower. Sorry for the delay. Is this still something you would like to have done? I am have not used EmeterStatus but can integrate that with the poller code: https://github.com/flavio-fernandes/mqtt2kasa/blob/69d1ba8e63a157530addfc5020aa2f92f51905b6/mqtt2kasa/kasa_wrapper.py#L153

Can you elaborate a little more on how you would like to see this working? Are you looking for a specific topic for the device with a json that includes the values power voltage current total ?

MaxRower commented 1 year ago

Yes, sort of.

My other (non kasa) plugs using tasmota do publish that data at user defined intervals, and that data then goes into my influxdb with a grafana GUI, so I can view it's complete history any time. Currently, I am using the influxdb logging feature of home assistant (with it's kasa integration) as a workaround (in addition to telegraf mqtt to influxdb logging), but home assistant is not very consistently logging data, so there are missing values, which show up in the graphs as straight lines with the wrong value. Here is an example of what I get from one of my tasmota based plugs: tele/tasmota_xxxxxx/SENSOR {"Time":"2023-05-29T12:14:09","ENERGY":{"TotalStartTime":"2023-03-17T23:12:03","Total":13.8426,"Yesterday":0.2577,"Today":0.1336,"Period":0.0,"Power":11.0,"ApparentPower":50.2,"ReactivePower":48.9,"Factor":0.22,"Voltage":222.9,"Current":0.225}} I think the optimum would be, if mqtt2kasa could also publish all necessary topics required by home assistant to handle the kasa plug (and other device types) with its mqtt integration alone, so one could get rid of its kasa integration completely. But publishing EmeterStatus alone would help a lot as well.

MaxRower commented 1 year ago

Sorry, wrong button with close clicked.

flavio-fernandes commented 1 year ago

Sorry, wrong button with close clicked.

np. Thank you for the nice explanation!

Let me think a bit about it and see if I can code the energy subtopic off of the device. Please be patient with me -- as you have already have -- as I only get a spare cycles here and there to play with this. Lots of fun, tho. :)

flavio-fernandes commented 1 year ago

hi @MaxRower . I wonder if you still are interested on this, but I figured.... better late than never? :) Had some free time to play today, and added the changes to publish the emeter. If this is still something that is interesting to you, give it a try and let me know, please: https://github.com/flavio-fernandes/mqtt2kasa/commit/17096b010773433ba80c5bdc2e63fdc11d0bf858

MaxRower commented 9 months ago

hi @MaxRower . I wonder if you still are interested on this, but I figured.... better late than never? :) Had some free time to play today, and added the changes to publish the emeter. If this is still something that is interesting to you, give it a try and let me know, please: 17096b0

Sorry for my late reply, I forgot to test it up to now. It seems to work with 0.0.2, with one exception: one KP115 plug discovery times out most of the time, don't know why, ping is constantly below 10ms. I had the same problem with the latest version of python-kasa, when I did not specify --type plug. But in mqtt2kasa, there seems to be no way to specify the device type, to speed up discovery? Maybe you could add an option for json or just a template string for the emeter text output? All of my mqtt compatible devices (zigbee2mqtt and tasmota) use the json format for logging, it would be nice, if this was possible to use it with mqtt2kasa as well.

MaxRower commented 9 months ago

When I use moquitto_sub to display what's coming in from mqtt2kasa, I notice, I get only a constant ~1 lines/second, regardless what I set for the rate_limit. Even with rate_limit=1000, there is no change. My current emeter_poll_interval is 10, so there is no chance to get all data from 6 plugs, when there are 5 entries for every plug sent for every emeter update.

flavio-fernandes commented 9 months ago

When I use moquitto_sub to display what's coming in from mqtt2kasa, I notice, I get only a constant ~1 lines/second, regardless what I set for the rate_limit. Even with rate_limit=1000, there is no change. My current emeter_poll_interval is 10, so there is no chance to get all data from 6 plugs, when there are 5 entries for every plug sent for every emeter update.

Ah, sorry for the confusion, @MaxRower. So, there is a throttle to prevent devices from continuously bouncing.

There is also a dampener that prevents the code from publishing excessively. You can disable or adjust that in this line:

https://github.com/flavio-fernandes/mqtt2kasa/blob/a2f09c37f78f1eb3544b4a7b8c5006c3fb12f0ac/mqtt2kasa/mqtt.py#L31

Please give it a try and let me know if that addresses your question.

MaxRower commented 9 months ago

Yes, with that sleep code removed, all messages come in instantly!

MaxRower commented 9 months ago

I tried to change the code to output json instead of unformatted text, but I think that's not the right place in env/lib/python3.10/site-packages/kasa/emeterstatus.py, as it's not part of your code but the kasa dependency?

#            f"<EmeterStatus power={self.power} voltage={self.voltage}"
#            f" current={self.current} total={self.total}>"
            f"{{\"ENERGY\":{{\"Power\":{self.power},\"Voltage\":{self.voltage},"
            f"\"Current\":{self.current},\"Total\":{self.total}}}}}"

But it does the produce the right output, and eliminates the output of each value in a single message. I will try to find, where to patch your code :)