1

i'm new into bash scripts and i really need your help.

The following Script gets humidity and temperature from a DHT22 sensor on my BananaPi and sends it to my HomeAutomation.

#!/bin/bash
cd /opt/lol_dht22
WERTE=$(./loldht 7 | grep "Humidity")
Temp=( $(echo $WERTE | awk '{ print $ 7}'))
Hum=( $(echo $WERTE | awk '{ print $ 3}'))
perl /opt/fhem/fhem.pl 7072 "setreading DHT22 temperature $Temp"
perl /opt/fhem/fhem.pl 7072 "setreading DHT22 humidity $Hum"

The results are like 30.00 (With a Comma), and sometimes the sensor fails and gives an unrealistic value like -3000.00 or similar.

So i wanted to implement a IF Condition which checks if it's greater or equal 0 or less or equal 100.

I tried things like:

#!/bin/bash
cd /opt/lol_dht22
WERTE=$(./loldht 7 | grep "Humidity")
Temp=( $(echo $WERTE | awk '{ print $ 7}' | tr '.' ','))
Hum=( $(echo $WERTE | awk '{ print $ 3}' | tr '.' ','))
if [[ $Temp -ge 0 && $Temp -le 100 && $Hum -ge 0 && $Hum -le 100 ]]; then
Temp1=( $(echo $Temp | tr ',' '.'))
Hum1=( $(echo $Hum | tr ',' '.'))
perl /opt/fhem/fhem.pl 7072 "setreading DHT22 temperature $Temp1"
perl /opt/fhem/fhem.pl 7072 "setreading DHT22 humidity $Hum1"
else
exit;
fi 

It seemed like the comma (.) was the problem, so i tried switching it to a (,) and back. But there are sometimes still values which are not between 0 and 100.

I hope someone can help me, thank you!

Regards,

Klaus

4 Answers 4

1

awk uses doubles, and can easily be used to test the range of decimal numbers.

...
Temp=$(echo $WERTE | tr , . | awk '$7>=0&&$7<=100{print$7}')
Hum=$(echo $WERTE | tr , . | awk '$3>=0&&$3<=100{print$3}')
if [ -n "$Temp" -a -n "$Hum" ]; then
    ...
fi
Sign up to request clarification or add additional context in comments.

1 Comment

What a smooth way, i love this solution, thank you !
0

-lt, -ge, etc. only work with integers. You can use <, >=, etc. inside [[, and it should work on floating point numbers too.

Some good practices in bash:

#!/bin/bash
set -e #autoexit if you run into an error
cd /opt/lol_dht22

#Reserve all-caps variables for exported variables and bash config variables
werte="$(./loldht 7 | grep "Humidity")" #always double-quote $ expressions
#foo=( bar ) creates arrays, you don't want that
#printf "%s\n" is more robust than echo if you're printing the contents of variables
Temp="$(printf '%s\n' "$werte" | awk '{ print $7 }' | tr . ,)"
Hum="$(printf '%s\n' "$werte" | awk '{ print $3 }' | tr . ,)"

#-lt etc. only work with integers
if [[ "$Temp" >= 0 && "$Temp" < 100 && "$Hum" >= 0 && "$Hum" < 100 ]]; then
  Temp1="$(printf '%s\n' "$Temp" | tr , .)"
  Hum1="$(printf '%s\n' "$Hum" | tr , .)"
  perl /opt/fhem/fhem.pl 7072 "setreading DHT22 temperature $Temp1"
  perl /opt/fhem/fhem.pl 7072 "setreading DHT22 humidity $Hum1"
else
  exit 1 #return nonzero codes for errors
fi

2 Comments

Hi, thanks for your awesome answer, the content of WERTE is for example: "Humidity = 23.30 % Temperature = 20.90 *C" is it needed to switch from . to , and back for => <=?
Glad you like it. I missed the tr part. If the ./loldht executables outputs numbers with decimal points (not commas), then you shouldn't be calling tr at all. No English-based computer language wants floats with decimal commas.
0

Please mention what is content of WERTE. It will help in answering the question because awk FS by default is space. We need to properly understand your content of $7 and $3

1 Comment

Hi, the content of WERTE is: Humidity = 23.30 % Temperature = 20.90 *C
0

Thank your Guys,

i've learned a lot.

and it seems to work:

#!/bin/bash
set -e
cd /opt/lol_dht22
WERTE=$(./loldht 7 | grep "Humidity")
Temp=$(echo $WERTE | awk '$7>=0&&$7<=100{print$7}')
Hum=$(echo $WERTE |  awk '$3>=0&&$3<=100{print$3}')
if [ -n "$Temp" -a -n "$Hum" ]; then
perl /opt/fhem/fhem.pl 7072 "setreading DHT22 temperature $Temp"
perl /opt/fhem/fhem.pl 7072 "setreading DHT22 humidity $Hum"
else
exit 1
fi

You've saved my Sunday =)

Best Regards,

Klaus

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.