LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 02-14-2024, 03:34 AM   #1
stockton
Member
 
Registered: Jan 2006
Location: Midrand, Gauteng, South Africa
Distribution: Raspbian, Mint 13, Slackware 14, Debian & Ubuntu
Posts: 105

Rep: Reputation: 2
SOLVED Not understanding bash script


With the following bash script I get error message "./GetGridVoltage.sh: line 3: [: 0.0: integer expression expected"

Code:
voltage=$( mosquitto_sub -h 192.168.8.119 -p 1883 -v -t '#' -C 15 | grep grid_voltage | awk 'NF>1{print $2}' )
gpio mode 6 output
if [ $voltage -gt 200 ] 
then
gpio write 6 1
else
gpio write 6 0
fi
gpio mode 6 input
Please tell me how to fix my error.

Last edited by stockton; 02-16-2024 at 11:45 AM. Reason: Solved
 
Old 02-14-2024, 03:59 AM   #2
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,313
Blog Entries: 3

Rep: Reputation: 3723Reputation: 3723Reputation: 3723Reputation: 3723Reputation: 3723Reputation: 3723Reputation: 3723Reputation: 3723Reputation: 3723Reputation: 3723Reputation: 3723
The program mosquitto_sub is going to stay running and you won't get just one result in $()

I'd move the work into AWK:

Code:
mosquitto_sub -h 192.168.8.119 -p 1883 -v -t '#' -C 15 \
| awk '/grid_voltage/ { 
        "gpio mode 6 output"; 
        if ($2) > 200 { 
                "gpio write 6 1" 
        } else { 
                "gpio write 6 0"}; 
        "gpio mode 6 input";'
(untested)

You might need to have AWK take buffering into account, you'll have to test.

If it gets too much more complex then consider having a Perl or Python script interact with MQTT directly rather than via the Mosquitto client.
 
Old 02-14-2024, 04:36 AM   #3
stockton
Member
 
Registered: Jan 2006
Location: Midrand, Gauteng, South Africa
Distribution: Raspbian, Mint 13, Slackware 14, Debian & Ubuntu
Posts: 105

Original Poster
Rep: Reputation: 2
Not understanding bash script

Would the $2 have the value of grid_voltage?
 
Old 02-14-2024, 04:39 AM   #4
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,313
Blog Entries: 3

Rep: Reputation: 3723Reputation: 3723Reputation: 3723Reputation: 3723Reputation: 3723Reputation: 3723Reputation: 3723Reputation: 3723Reputation: 3723Reputation: 3723Reputation: 3723
Quote:
Originally Posted by stockton View Post
Would the $2 have the value of grid_voltage?
It would depend on what you are getting from Mosquitto.

Please show some sample lines of output from mosquitto_sub as you have it configured and we can guess.

Also port 1883 implies that you are making decisions based on unencrypted messages. If this is for anything serious, you need to consider sending MQTT over TLS.
 
Old 02-14-2024, 04:57 AM   #5
stockton
Member
 
Registered: Jan 2006
Location: Midrand, Gauteng, South Africa
Distribution: Raspbian, Mint 13, Slackware 14, Debian & Ubuntu
Posts: 105

Original Poster
Rep: Reputation: 2
solar_assistant/inverter_1/pv_voltage/state 64.2
solar_assistant/inverter_1/grid_frequency/state 0.0
solar_assistant/inverter_1/pv_power/state 1216
solar_assistant/inverter_1/battery_voltage/state 53.7
solar_assistant/inverter_1/load_apparent_power/state 437
solar_assistant/inverter_1/temperature/state 53.0
solar_assistant/inverter_1/load_percentage/state 8
solar_assistant/inverter_1/battery_current/state 15.0
solar_assistant/inverter_1/grid_power/state 0
solar_assistant/inverter_1/device_mode/state Solar/Battery
solar_assistant/inverter_1/grid_voltage/state 0.0
solar_assistant/inverter_1/ac_output_frequency/state 50.0
solar_assistant/inverter_1/pv_current/state 18.9
solar_assistant/inverter_1/ac_output_voltage/state 230.0

This is just monitoring my solar system. I via this script wish to inform my tenants of wether we are on the grid or using batteries due to grid being down.
I can extract the value from MQTT but get an error when using it in an if statement.
 
1 members found this post helpful.
Old 02-14-2024, 05:11 AM   #6
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,313
Blog Entries: 3

Rep: Reputation: 3723Reputation: 3723Reputation: 3723Reputation: 3723Reputation: 3723Reputation: 3723Reputation: 3723Reputation: 3723Reputation: 3723Reputation: 3723Reputation: 3723
Quote:
Originally Posted by stockton View Post
solar_assistant/inverter_1/pv_voltage/state 64.2
solar_assistant/inverter_1/grid_frequency/state 0.0
solar_assistant/inverter_1/pv_power/state 1216
solar_assistant/inverter_1/battery_voltage/state 53.7
solar_assistant/inverter_1/load_apparent_power/state 437
solar_assistant/inverter_1/temperature/state 53.0
solar_assistant/inverter_1/load_percentage/state 8
solar_assistant/inverter_1/battery_current/state 15.0
solar_assistant/inverter_1/grid_power/state 0
solar_assistant/inverter_1/device_mode/state Solar/Battery
solar_assistant/inverter_1/grid_voltage/state 0.0
solar_assistant/inverter_1/ac_output_frequency/state 50.0
solar_assistant/inverter_1/pv_current/state 18.9
solar_assistant/inverter_1/ac_output_voltage/state 230.0
Thanks. $1 will be solar_assistant/inverter_1/ac_output_voltage/state and $2 will be 230.0 because white space is the default field separator (FS) in AWK.

You can narrow the subscription there substantially so that Mosquitto shows only the output from the relevant topic, such as solar_assistant/inverter_1/ac_output_voltage/state or any other item there. See "man mosquitto_sub" and scroll down to -t or see "man 7 mqtt"

If you are watching more than one inverter, then you can subscribe to the topics with a wildcard: solar_assistant/+/ac_output_voltage/state

Quote:
Originally Posted by stockton View Post
This is just monitoring my solar system. I via this script wish to inform my tenants of wether we are on the grid or using batteries due to grid being down.
In that case, top priority ought to be deploying TLS first, before working on the rest. Otherwise, with unencrypted connections anything can happen, including spoofed messages.
Quote:
Originally Posted by stockton View Post
I can extract the value from MQTT but get an error when using it in an if statement.
That's not what was happening. It's what you wanted but the [url=https://mywiki.wooledge.org/CommandSubstitution]command substitution[/font] was capturing something else.
 
Old 02-14-2024, 05:11 AM   #7
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,868
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
Quote:
solar_assistant/inverter_1/grid_voltage/state 0.0

line 3: [: 0.0: integer expression expected
Then the error message is correct: "0.0" isn't an integer value.

Last edited by NevemTeve; 02-14-2024 at 05:12 AM.
 
Old 02-14-2024, 05:16 AM   #8
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,313
Blog Entries: 3

Rep: Reputation: 3723Reputation: 3723Reputation: 3723Reputation: 3723Reputation: 3723Reputation: 3723Reputation: 3723Reputation: 3723Reputation: 3723Reputation: 3723Reputation: 3723
Quote:
Originally Posted by NevemTeve View Post
Then the error message is correct: "0.0" isn't an integer value.
Specifically, AWK can handle floating point to an extent, Bash cannot. Bash itself handles only integers.
 
Old 02-14-2024, 05:20 AM   #9
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,130

Rep: Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121
Be aware that Turbocapitalist has referenced the wrong field, but I'm sure that'll be put right.

Can you use grid_voltage/state ? - that looks like it'll be a binary 0/1, should make using it easier.
 
Old 02-14-2024, 05:35 AM   #10
stockton
Member
 
Registered: Jan 2006
Location: Midrand, Gauteng, South Africa
Distribution: Raspbian, Mint 13, Slackware 14, Debian & Ubuntu
Posts: 105

Original Poster
Rep: Reputation: 2
Thank you NevemTeve and Turbocapitalist but I should have seen that 0.0 was not an integer.

Last edited by stockton; 02-14-2024 at 05:43 AM.
 
Old 02-14-2024, 05:52 AM   #11
stockton
Member
 
Registered: Jan 2006
Location: Midrand, Gauteng, South Africa
Distribution: Raspbian, Mint 13, Slackware 14, Debian & Ubuntu
Posts: 105

Original Poster
Rep: Reputation: 2
grid_voltage/state also returns the voltage as a float
 
Old 02-14-2024, 06:22 AM   #12
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,879

Rep: Reputation: 7317Reputation: 7317Reputation: 7317Reputation: 7317Reputation: 7317Reputation: 7317Reputation: 7317Reputation: 7317Reputation: 7317Reputation: 7317Reputation: 7317
I think you can specify the topic:
Code:
mosquitto_sub -t solar_assistant/inverter_1/grid_voltage/state ...
Additionally you do not need to calculate that much:
Code:
...
val=$(awk '{($2>200)?a=1:a=0;print a}')
gpio write 6 $val
...
may work (not tested)
 
Old 02-14-2024, 06:47 AM   #13
stockton
Member
 
Registered: Jan 2006
Location: Midrand, Gauteng, South Africa
Distribution: Raspbian, Mint 13, Slackware 14, Debian & Ubuntu
Posts: 105

Original Poster
Rep: Reputation: 2
Tried pan64 suggestion but val is still a float
#
mosquitto_sub -h 192.168.8.119 -p 1883 -v -t '#' -C 15 | grep grid_voltage | awk 'NF>1{print $2}'

val=$(awk '{($2>200)?a=1:a=0;print a}')
echo $val
gpio mode 6 output
gpio write 6 $val
gpio mode 6 input
#
 
Old 02-14-2024, 07:35 AM   #14
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,879

Rep: Reputation: 7317Reputation: 7317Reputation: 7317Reputation: 7317Reputation: 7317Reputation: 7317Reputation: 7317Reputation: 7317Reputation: 7317Reputation: 7317Reputation: 7317
what you posted is not a working script. Otherwise would be nice to use code tags.
I did not post complete solution, you need to adjust it.
 
Old 02-14-2024, 08:19 AM   #15
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,868
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
[code]
Code:
voltage=$(mosquitto_sub -h 192.168.8.119 -p 1883 -v -t '#' -C 15 | grep grid_voltage | awk 'NF>1 {print int(1*$2)}')
if [ $voltage -gt 200 ] ...
[/code]
@OP There is an underlined part in it, try to find out what it is good for.
 
1 members found this post helpful.
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Help with understanding the 'with' keyword and understanding file reading and writing. vysero Programming 3 05-30-2018 02:37 PM
Understanding of set -x output when ran against Bash script JockVSJock Linux - Newbie 5 03-16-2018 03:52 AM
Simple BASH script; understanding loops and case Sinensis Linux - Newbie 1 06-17-2010 04:32 AM
Help understanding a bash script cylon Linux - Newbie 4 09-16-2009 10:04 AM
bash script, understanding variables antis Programming 5 11-15-2007 04:27 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:59 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration