call the shelly using a script:

root@lnx02:~/bin# ./getShelly1pmTemperature4checkmk.pl 192.168.2.170
<<<checkmk>>>
version: 2023-04-01
<<<local>>>
0 Shelly1pm-Temperature temp=30.6 30.6 C
0 Shelly1pm--t1 temp=42.12 Temperature from Sensor 1: 42.12
0 Shelly1pm--t2 temp=54.38 Temperature from Sensor 2: 54.38
0 Shelly1pm--t3 temp=41.12 Temperature from Sensor 3: 41.12


-------------------------------------------------------------------------------------
>> here is the script
-------------------------------------------------------------------------------------
-------------------------------------------------------------------------------------

#!/usr/bin/perl
################################################################################
#
# getShelly1pmTemperature4checkmk.pl
#
# checkmk local check to get temperature from shelly device
#
# (c> by m.wendig v2023-04
#
################################################################################

use LWP::UserAgent;
use JSON qw( decode_json );
use Data::Dumper;

my $version='2023-04-01';
my $cname='Shelly1pm';
my $debug=0;

my $shelly_ip = shift;
my $ext_temp_name_1 = shift;
my $ext_temp_name_2 = shift;
my $ext_temp_name_3 = shift;
my $ext_temp_name_4 = shift;
my $ext_temp_name_5 = shift;

if (!defined $shelly_ip) {
print "usage:\n";
print "getShelly1pmTemperature4checkmk.pl <Shelly IPy> [ext_temp_name_1] [ext_temp_name_2] [ext_temp_name_3] [ext_temp_name_4] [ext_temp_name_5]\n";
print "Example: getShelly1pmTemperature4checkmk.pl 192.168.1.10 temp-top temp-middle temp-buttom\n";
exit;
}

my $ua = LWP::UserAgent->new;

my $url = "http://$shelly_ip/status";

my $req = HTTP::Request->new(GET => $url);

my $res = $ua->request($req);


print "<<<checkmk>>>\n";
print "version: $version\n";
print "<<<local>>>\n";

if ($res->is_success) {
my $content = $res->content;

# Decode JSON response
my $json = decode_json($content);
print Dumper($json) if $debug;

# Print temperature in Celsius
my $temp = $json->{tmp}{tC};
my $temp_unit = $json->{ext_sensors}{temperature_unit};
my $ext_temp_1 = $json->{ext_temperature}{0}{tC};
my $ext_temp_2 = $json->{ext_temperature}{1}{tC};
my $ext_temp_3 = $json->{ext_temperature}{2}{tC};
my $ext_temp_4 = $json->{ext_temperature}{3}{tC};
my $ext_temp_5 = $json->{ext_temperature}{4}{tC};

print "0 $cname-Temperature temp=$temp $temp $temp_unit\n";
print "0 $cname-$ext_temp_name_1"."-t1 temp=$ext_temp_1 Temperature from Sensor 1: $ext_temp_1\n" if defined $ext_temp_1;
print "0 $cname-$ext_temp_name_2"."-t2 temp=$ext_temp_2 Temperature from Sensor 2: $ext_temp_2\n" if defined $ext_temp_2;
print "0 $cname-$ext_temp_name_3"."-t3 temp=$ext_temp_3 Temperature from Sensor 3: $ext_temp_3\n" if defined $ext_temp_3;
print "0 $cname-$ext_temp_name_4"."-t4 temp=$ext_temp_4 Temperature from Sensor 4: $ext_temp_4\n" if defined $ext_temp_4;
print "0 $cname-$ext_temp_name_5"."-t5 temp=$ext_temp_5 Temperature from Sensor 5: $ext_temp_5\n" if defined $ext_temp_5;
} else {
print "2 $cname-Temperature temp=- Failed to fetch temperature data from Shelly\n";
}

computer2know :: thank you for your visit :: have a nice day :: © 2024