CLI Module run with "sysinfo" flag or datatypes returned in JSON format are invalid as variable names and string values are encapsulated with single-quotes. The JSON standard requires double-quotes. This is making it difficult to pipe data from the script to any service/application that expects proper JSON data without running it through a wrapper that does a find/replace on the single quotes.
It seems that the kasa code repeats the same commands so we get the same output twice (this is due to not specifying type).
Using SED and AWK. I managed to split the JSON from other debug text, split each output into sections and then only print one output, which has all the information we need. We can then parse using JQ.
I've tested this using a KP115 (smartplug) and KP303 (smartstrip).
I also have an HS110 which outputs non-sensical multi-line JSON so JQ can't parse it.
"dev_name": "Smart Wi-Fi Plug With Energy "
"Monitoring",
This gives: parse error: Expected separator between values at line 84, column 52
For my working KP115, as an example, I can fetch the emeter information using jq
Regarding
None of the commands of the cli tool output json, but a PR to add support for defining the output format would be welcome!
Simply the output from the device is all we'd want. Any calculations or interpretation aren't needed.
@lazynooblet (and everyone else), feel free to test the linked PR, it should cover ~all commands if I didn't miss something. The simplest way to use the raw data is just access the output from state which provides the complete internal state of the device:
Its also much faster. Whilst there was a pause of half a second as the commands went back and forth, now the response is instant.
I now just get the data I need, nothing else, which I then graph in Zabbix.
I can finally remove that monstrosity of sed/awk/jq 😏
Activity
rytilahti commented on Oct 4, 2021
None of the commands of the cli tool output json, but a PR to add support for defining the output format would be welcome!
tuxthepenguin84 commented on Nov 26, 2022
I'm sure there's a cleaner way but this works as a temp solution:
And if you want to just grab the data from a key:
lazynooblet commented on Jan 6, 2023
I hacked something togethor to make a working JSON output.
(kasa --debug --host IP) 2>&1 1>/dev/null | sed -n '/DEBUG:kasa.protocol:[^ ]* <</,/DEBUG/p;' | sed 's/DEBUG:kasa.protocol:[^ ]* << \({.*\)$/BEGIN\n\1/; s/^DEBUG.*$/END/; s/'"'"'/"/g' | awk '/^BEGIN/ {n++;} !/BEGIN|END/ {if (n==3) print;}'
The debug output has the JSON we need.
It seems that the kasa code repeats the same commands so we get the same output twice (this is due to not specifying type).
Using SED and AWK. I managed to split the JSON from other debug text, split each output into sections and then only print one output, which has all the information we need. We can then parse using JQ.
I've tested this using a KP115 (smartplug) and KP303 (smartstrip).
I also have an HS110 which outputs non-sensical multi-line JSON so JQ can't parse it.
This gives: parse error: Expected separator between values at line 84, column 52
For my working KP115, as an example, I can fetch the emeter information using jq
Regarding
Simply the output from the device is all we'd want. Any calculations or interpretation aren't needed.
rytilahti commented on Feb 18, 2023
@lazynooblet (and everyone else), feel free to test the linked PR, it should cover ~all commands if I didn't miss something. The simplest way to use the raw data is just access the output from state which provides the complete internal state of the device:
❯ kasa --json --host 192.168.xx.xx --type plug state|jq 'map_values(keys)' { "system": [ "get_sysinfo" ], "schedule": [ "get_daystat", "get_monthstat", "get_next_action", "get_realtime", "get_rules" ], "anti_theft": [ "get_next_action", "get_rules" ], "time": [ "get_time", "get_timezone" ], "cnCloud": [ "get_info" ], "emeter": [ "get_daystat", "get_monthstat", "get_realtime" ] }lazynooblet commented on Mar 7, 2023
@rytilahti this works great thank you!
Its also much faster. Whilst there was a pause of half a second as the commands went back and forth, now the response is instant.
I now just get the data I need, nothing else, which I then graph in Zabbix.
I can finally remove that monstrosity of sed/awk/jq 😏
Thanks again! 🥇