utilities/tp-link-to-influxdb#6: Improve error handling



Issue Information

Issue Type: issue
Status: closed
Reported By: btasker
Assigned To: btasker

Milestone: v0.25
Created: 20-May-23 22:30



Description

Issue #4 added support for having the script run in persistent mode.

However, the error handling/exception trapping is basically non-existent: the script was written with the intent that it'd be called periodically by cron.

Whilst that can be mitigated by telling docker/systemd/whatever to restart on exit, we should probably look at adjusting things so that errors are handled a bit more gracefully.



Toggle State Changes

Activity


assigned to @btasker

There's a good example of this here: https://github.com/bentasker/tplink_to_influxdb/issues/1#issuecomment-1556135654

Traceback (most recent call last):
  File "/app/collect.py", line 223, in <module>
    main()
  File "/app/collect.py", line 80, in main
    do_work(config, influxes)
  File "/app/collect.py", line 97, in do_work
    now_usage_w, today_usage = poll_kasa(kasa['ip'])
  File "/app/collect.py", line 162, in poll_kasa
    today_usage = p.emeter_today * 1000
TypeError: unsupported operand type(s) for *: 'NoneType' and 'int'

The plugs can sometimes end up not returning daily readings - there's actually an example cause of this in the code just before that exception is thrown

    # emeter_today relies on external connectivity - it uses NTP to keep track of time
    # you need to allow UDP 123 outbound if you're restricting the plug's external connectivity
    # otherwise you'll get 0 or 0.001 back instead of the real value
    # 
    # See https://github.com/home-assistant/core/issues/45436#issuecomment-766454897
    #

    # Convert from kWh to Wh
    today_usage = p.emeter_today * 1000

The example leads to a question though.

If we detect that emeter_today isn't valid, what do we do:

  1. Refuse to proceed
  2. Set it to an arbitrarily high/low value
  3. Don't set that value

Number 1 seems like the worse option, whilst 2 isn't really much better - it's probably better to leave a gap, so that deadman type alerts can fire.

The next question (and I don't know the answer) is whether Tapo devices are also affected by this - I assume that they must be, but I don't know for sure

verified

mentioned in commit github-mirror/tplink_to_influxdb@90c3130b5697a034683cd2ff74d8f1ceb9580256

Commit: github-mirror/tplink_to_influxdb@90c3130b5697a034683cd2ff74d8f1ceb9580256 
Author: B Tasker                            
                            
Date: 2023-05-22T22:50:33.000+01:00 

Message

Handle scenarios where Kasa smart plugs fail to return daily usage (utilities/tp-link-to-influxdb#6)

+11 -3 (14 lines changed)
verified

mentioned in commit github-mirror/tplink_to_influxdb@d9652df200e7fb90e6565f8c6aeaa10ddd7da575

Commit: github-mirror/tplink_to_influxdb@d9652df200e7fb90e6565f8c6aeaa10ddd7da575 
Author: B Tasker                            
                            
Date: 2023-05-22T22:52:05.000+01:00 

Message

Handle Tapo devices not returning daily usage (utilities/tp-link-to-influxdb#6)

I'm not sure whether this can ever actually happen, but handling it doesn't require much extra effort so seems worth guarding against

+9 -2 (11 lines changed)
verified

mentioned in commit github-mirror/tplink_to_influxdb@b302f8fc18145f9db1cf4a8f20d787b790428025

Commit: github-mirror/tplink_to_influxdb@b302f8fc18145f9db1cf4a8f20d787b790428025 
Author: B Tasker                            
                            
Date: 2023-05-22T22:58:40.000+01:00 

Message

Add handling at various points in the code which might experience errors (utilities/tp-link-to-influxdb#6)

This is so that an error doesn't cause the script to exit when run in persistent mode

There are two exceptions to this

  • If the config file fails to parse
  • If there are no devices configured within the config
+16 -3 (19 lines changed)

This was fixed quite a while ago, closing.

I am going to improve the debug logging, but will do so under a dedicated issue.