How can i run a perl script?

Hello, i ve a perl script and have problems to run this on my Raspi with HA.

i add a file in ~\config\ … ph803_mqtt.pl (code below) and get this message if i start this script:

config ./ph803_mqtt.pl
Can't locate Net/MQTT/Simple.pm in @INC (you may need to install the Net::MQTT::Simple module) (@INC contains: /usr/local/lib/perl5/site_perl /usr/local/share/perl5/site_perl /usr/lib/perl5/vendor_perl /usr/share/perl5/vendor_perl /usr/lib/perl5/core_perl /usr/share/perl5/core_perl) at ./ph803_mqtt.pl line 20.
BEGIN failed--compilation aborted at ./ph803_mqtt.pl line 20.

how can i install perl / perl-modules or how can i make this script running?

#!/usr/bin/perl -w


# Simple proof of concept program to get pH and ORP (redox) values from a PH-803W
# inside your local network without the PH803 app and without the gizwits cloud
# (the app is still needed to initially connect the PH-803W to your WLAN)
#
# No auto discovery, IP address of PH-803W must be known
# (change my $PH_803_IP = ... below)
#
# Based on the protocol from https://github.com/Apollon77/node-ph803w/blob/727e998c0aea73a4ed7a611b9ff6b65d08998f5f/PROTOCOL.md
# and code from https://www.xmodulo.com/how-to-write-simple-tcp-server-and-client-in-perl.html
# Special thanks to alf_1 !!


use strict;
use IO::Socket::INET;
use warnings;
use autodie;
use Net::MQTT::Simple;


my $PH_803_IP = '111.222.333.444';


# idx für PH und ORP

my $idxph = '111';
my $idxorp = '222';


# MQTT

my $mqtt = Net::MQTT::Simple->new('localhost:1883');

my $mqtt_username = 'user';
my $mqtt_password = 'pass';


# Allow unencrypted connection with credentials

$ENV{MQTT_SIMPLE_ALLOW_INSECURE_LOGIN} = 1;



sub hex2bytes {
my ($hex) = @_;
my $bytes = pack('H*', $hex);
return $bytes;
}


sub bytes2hex {
my ($bytes) = @_;
my $hex = unpack('H*', $bytes);
return $hex;
}


# send a hex string to the server (convert to bytes before)
sub sendhex {
my ($socket, $datah) = @_;

my $datab = hex2bytes($datah);
my $len = length($datab);
my $sentlen = $socket->send($datab);
if ($sentlen != $len) {
die "failed to send all $len bytes of $datah, only sent $sentlen";
}
print "sent $datah\n";
}


# receive a response of up to 1024 characters from server (returned as a hex string)
sub receivehex {
my ($socket) = @_;

my $datab = '';
$socket->recv($datab, 1024);
my $datah = bytes2hex($datab);
print "received $datah\n";
return $datah;
}

# verify a response from the server against an expected value (meant for hex strings)
sub verifyhex {
my ($resph, $expectedh) = @_;
if ($resph ne $expectedh) {
die "invalid response: received $resph, expected $expectedh";
}
print "verified response $resph\n";
}


# convert a 4 character hex string to a unsigned 16 bit short value
sub hex2ushort {
my ($datah) = @_;
die "input must be a hex string of 4 characters: $datah" unless length($datah) == 4;
my $ushort = unpack('n', hex2bytes($datah));
return $ushort;
}


# auto-flush on socket
$| = 1;

# create a connecting socket
my $socket = new IO::Socket::INET (
PeerHost => $PH_803_IP,
PeerPort => '12416',
Proto => 'tcp',
);


if ($socket) {
print "connected to the PH-803W server $PH_803_IP\n";
} else {

# Depending if authentication is required, login to the broker
if($mqtt_username and $mqtt_password) {
$mqtt->login($mqtt_username, $mqtt_password);
}

# Publish Error
$mqtt->publish("domoticz/in", "{\"command\" : \"addlogmessage\", \"message\" : \"PH-803W: Keine Verbindung zu PH-803W ($PH_803_IP)\" }");
$mqtt->publish("domoticz/in", "{\"command\" : \"addlogmessage\", \"message\" : \"PH-803W: näschster Versuch in 5 Minuten\" }");
$mqtt->disconnect();

die "failed to connect to the PH-803W server $PH_803_IP: $!" unless $socket;
}


# step 1:
# send 00 00 00 03 03 00 00 06
# and verify that response starts with 00 00 00 03 0f 00 00 07

my $req1h = '0000000303000006';
sendhex($socket, $req1h);

my $resp1h = receivehex($socket);
verifyhex(substr($resp1h,0,16), '000000030f000007');

# step 2:
# send response from step 1, but with byte 8 changed from 07 to 08,
# and verify that response is 00 00 00 03 04 00 00 09 00

my $req2h = substr($resp1h,0,14) . '08' . substr($resp1h,16);
sendhex($socket, $req2h);

my $resp2h = receivehex($socket);
verifyhex($resp2h, '000000030400000900');


# step 3:
# send 00 00 00 03 04 00 00 90 02
# and verify that response starts with 00 00 00 03 0d 00 00 91

my $req3h = '000000030400009002';
sendhex($socket, $req3h);

my $resp3h = receivehex($socket);
verifyhex(substr($resp3h, 0, 16), '000000030d000091');

# response from step 3 contains pH value at bytes 11/12,
# must be interpreted as an unsigned 16 bit value and divided by 100

my $phh = substr($resp3h, 20, 4);
my $ph = hex2ushort($phh) / 100;

# response from step 3 contains ORP value at bytes 13/14,
# must be interpreted as an unsigned 16 bit value and decreased by 2000

my $orph = substr($resp3h, 24, 4);
my $orp = hex2ushort($orph) - 2000;

print "pH=$ph orp=$orp mV\n";
# add code here to do something useful with the values, e.g. send them to MQTT

# Depending if authentication is required, login to the broker
if($mqtt_username and $mqtt_password) {
$mqtt->login($mqtt_username, $mqtt_password);
}

# Publish Messwerte
$mqtt->publish("domoticz/in", "{\"idx\":$idxph,\"svalue\":\"$ph\"}");
$mqtt->publish("domoticz/in", "{\"idx\":$idxorp,\"svalue\":\"$orp\"}");
$mqtt->disconnect();

$socket->close();

Look at the shell command and command line sensor integrations.

u mean like this?

shell_command:
  ph803w: bash "/config/ph803_mqtt.pl"

Maybe? Does it work?

no, u see the error? i have no experience with comand lines in HA. unfortunately.

supposedly it works like this in iobroker, but i don’t really want to switch there just because of the script.

error on my side. error is gone.

ve this now:

shell_command:
ph803w: bash “/config/ph803_mqtt.pl”

now i need a command line sensor ?

start service, get this in my LOG

strong text**Error running command: `/config/ph803_mqtt.pl`, return code: 127
2:09:56 PM – (ERROR) Shell Command - message first occurred at 2:09:41 PM and shows up 2 times

perhaps i buy a 2nd raspberry for perl scripts. the users in the iobroker comminity have also no ideas how it works in HA.

As discussed on Discord there’s no Perl in the container. The simplest solution is to use the custom component that exists.