Read Data from IPFire in Home Assistant

Hello,
This might be of interest to anyone who uses Home Assistant and wants to display some information from IPFire within Home Assistant.
I’ve written an integration for Home Assistant that imports traffic data from Red on IPFire.
Additionally, if you copy the included api.cgi script to the web folder, you can enable and disable the external connection using a button or HA automation.
The api.cgi script is necessary because IPFire does not provide its own API.
I look forward to your feedback.

Frank

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

Thank you very much for reporting the error; I’ll look into it.

I just released HA-IPFire v0.3.2.
Hopefully, I’ve fixed the reported bug.

When using redctrl, I had to add some additional error handling. This is because if Red is connected and you run ‘redctrl start’ again, the status file is deleted, which causes the “Idle” message to appear.

Error reports and suggestions for improvement are welcome.