with the original JSON portion of the code in the api cgi i was getting and error
SyntaxError: JSON.parse: expected property name or '}' at line 1 column 32 of the JSON data
original:
my @json;
push @json, '"api_version":1';
push @json, '"connection":{';
push @json, '"state":' . json_string($state);
if (defined $connected_since) {
push @json, '"connected_since":' . $connected_since;
push @json, '"duration":' . int($duration);
push @json, '"duration_text":' . json_string(&General::format_time(int($duration)));
} else {
push @json, '"connected_since":null';
push @json, '"duration":0';
push @json, '"duration_text":""';
}
push @json, '"profile":' . json_string($profile);
push @json, '}';
print '{' . join(',', @json) . '}\n';
I played with it a little and this code works for me:
my @json;
push @json, '"api_version":1';
my @connection_json;
push @connection_json, '"state":' . json_string($state);
if (defined $connected_since) {
push @connection_json, '"connected_since":' . $connected_since;
push @connection_json, '"duration":' . int($duration);
push @connection_json, '"duration_text":' . json_string(&General::format_time(int($duration)));
} else {
push @connection_json, '"connected_since":null';
push @connection_json, '"duration":0';
push @connection_json, '"duration_text":""';
}
push @connection_json, '"profile":' . json_string($profile);
print '{"api_version":1,"connection":{' . join(',', @connection_json) . '}}' . "\n";
as far as the home assistant part of it i have not tested the output with it.
output:
{"api_version":1,"connection":{"state":"connected","connected_since":1789182449,"duration":211925,"duration_text":"2d 10h 52m 5s","profile":""}}
I don’t know if this helps or not but i saw this and wanted to tinker
thanks
sio