Catting the Battery level in gxmessages

gxmessage "`date` cat /sys/class/power_supply/ip5xxx-battery/capacity $(sudo tuptime | awk -F'=' '/System uptime:/ {print substr($2, 1, length($2)-3)}')" "   $(curl -s http://rss.accuweather.com/rss/liveweather_rss.asp\?metric\=${METRIC}\&locCode\=20902 \ | sed -n '/Currently:/ s/.*: \(.*\): \([0-9]*\)\([CF]\).*/\2°\3 \1/p') "

If you run this script, you see date, tuptime, and weather. What you won’t see is catted battery level.

For cat /sys/class/power_supply/ip5xxx-battery/capacity, what syntax am I missing to produce battery output.

Thanks.`

Perhaps you can find inspiration from [root tip] [Utility Script] Charger Notifier - battery state

1 Like

It’s a string of commands. Not separated properly.
Test each one individually?!

→ first command is:
date

→ second command is:
cat /sys/class/power_supply/ip5xxx-battery/capacity

→ third command here is:
$(sudo tuptime | awk -F'=' '/System uptime:/ {print substr($2, 1, length($2)-3)}')" "
but it is syntactically incorrect, it seems, for what you want the output to be.

tuptime | awk -F'=' '/System uptime:/ {print substr($2, 1, length($2)-3)}'
produces just the uptime, like this:
1d 9h 1m

Make sure you have the command “tuptime” available - install it if you don’t have it.
It doesn’t need sudo to run.
And correct the awk command to provide the desired output format.

→ last command is:
curl -s http://rss.accuweather.com/rss/liveweather_rss.asp\?metric\=${METRIC}\&locCode\=20902 \ | sed -n '/Currently:/ s/.*: \(.*\): \([0-9]*\)\([CF]\).*/\2°\3 \1/p'
and it works, giving output like this:
64°F Partly Cloudy

→ a string of commands
separate them from each other by &&
or put them in a script with some extra logic to make sure that a later command is still executed even if the one before fails

for example (adapted for my laptop battery):

date && cat /sys/class/power_supply/BAT0/capacity && tuptime | awk -F'=' '/System uptime:/ {print substr($2, 1, length($2)-3)}' && curl -s http://rss.accuweather.com/rss/liveweather_rss.asp\?metric\=${METRIC}\&locCode\=20902 \ | sed -n '/Currently:/ s/.*: \(.*\): \([0-9]*\)\([CF]\).*/\2°\3 \1/p'

result:

So 11. Jun 08:39:37 CEST 2023
100
  1d 9h 17m 
64°F Partly Cloudy

I did not fix the awk command …

2 Likes

@ Nachlese – I very much appreciate your work/time on this. I’ve run your cleaner, reworked commands and they produce your results in my terminal.

The rub: the syntax so far, doesn’t work with gxmessage Using gxmessage as the command to set up the rest simply results in a print-out of the commands, not their calculation. Much the way my original cat /sys/class/power_supply/ip5xxx-battery/capacity printed but didn’t execute.

If I remove cat /sys/class/power_supply/ip5xxx-battery/capacity from the original script, the other commands work without complaint.

As an aside, I’m using Manjaro Phosh Beta ver 33 on a PinePhone Beta, and for some reason, notify-send doesn’t work with said setup.

Thanks again for all your help.

I’m totally unfamiliar with gxmessage - and with Posh as well. :person_shrugging:

Are you sure that that path is correct?

/sys/class/power_supply/ip5xxx-battery/capacity

check, for instance with:
ls -hl /sys/class/power_supply
and
ls -hl /sys/class/power_supply/ip5xxx-battery

1 Like

Understood. Thank you.

Try putting it in a variable first.

s=$(date && cat /sys/class/power_supply/BAT0/capacity && tuptime | awk -F'=' '/System uptime:/ {print substr($2, 1, length($2)-3)}' && curl -s http://rss.accuweather.com/rss/liveweather_rss.asp\?metric\=${METRIC}\&locCode\=20902 \ | sed -n '/Currently:/ s/.*: \(.*\): \([0-9]*\)\([CF]\).*/\2°\3 \1/p')

Or better yet, do each separately and combine them into a single string to pass to gxmessage. That would make it easier to get the correct syntax.

1 Like

Thank you for assistance as well. I will try your command, but I’m looking for the close of $( and I’m not certain I see the closing parenthesis.

What am I missing?

Thanks.

It’s the last char, you may need to scroll right.

1 Like

You folks are getting places–@dmt’s s= start executes all commands in gxmessage.

The message also prints out s= Can I remove that, and also for the battery output, since it’s just a plain number, how can I print a following % or a preceding Batt:?

(It’s such a pleasure to huddle with people who know so much more than I do)

How are you running it?

s=$(date && cat /sys/class/power_supply/BAT0/capacity && tuptime | awk -F'=' '/System uptime:/ {print substr($2, 1, length($2)-3)}' && curl -s http://rss.accuweather.com/rss/liveweather_rss.asp\?metric\=${METRIC}\&locCode\=20902 \ | sed -n '/Currently:/ s/.*: \(.*\): \([0-9]*\)\([CF]\).*/\2°\3 \1/p')
gxmessage $s

You’re best dealing with them separately.

date=$(date)
batt="$(cat /sys/class/power_supply/BAT0/capacity)%"
# or
batt="batt: $(cat /sys/class/power_supply/BAT0/capacity)"
# etc

s="$date $batt ..."
gxmessage $s
1 Like

No, I wasn’t. I was running gxmessage s=...

Once I tried your approach, resulting print-out was fine.

1 Like

tuptime, by itself, delivers this output (on my machine, an intel laptop)

tuptime 
System startups: 	1  since  23:22:06 09.06.2023
System shutdowns: 	0 ok  +  0 bad
System life: 	        1d 17h 26m 12s

System uptime: 	        100.0%  =  1d 17h 26m 12s
System downtime: 	0.0%  =  0s

Average uptime: 	1d 17h 26m 12s
Average downtime: 	0s

Current uptime: 	1d 17h 26m 12s  since  23:22:06 09.06.2023

The following awk command will need to be tailored to extract the information from it that you want.

2 Likes

I found that print-out excessive as well, and I just kept playing with print cuts until I got something briefer. The great thing about tuptime is that it remembers your last uptime–so if your machine was up for 59 mins, when you restart it, uptime begins at 59 mins. You can reset everything by blowing away tuptime’s cache in /var

Thanks to folks’ very helpful posts/advice, the script is working now. I made an addition to deal with the PinePhone’s weird two-battery output (one for the phone’s battery, the other for the keyboard’s battery)–the script prints out both percentages and then combines them. The script::

batt1="$(cat /sys/class/power_supply/ip5xxx-battery/capacity)"
batt2="$(cat /sys/class/power_supply/axp20x-battery/capacity)"
batt3="= $(($batt1 + $batt2))%"
uptime="$(sudo tuptime | awk -F'=' '/System uptime:/ {print substr($2, 1, length($2)-3)}')"
weather="$(curl -s http://rss.accuweather.com/rss/liveweather_rss.asp\?metric\=${METRIC}\&locCode\=20902 \ | sed -n '/Currently:/ s/.*: \(.*\): \([0-9]*\)\([CF]\).*/\2°\3 \1/p')"
s="battery: $batt1% + $batt2% $batt3 uptime: $uptime weather: $weather"
gxmessage $s

Resulting in:

battery: 100% + 95% = 195% uptime: 21h 1m weather: 74° Cloudy

It’s a little crowded all on one line. I would like to put each print-out onto a separate line with a space in between each line. I have looked hard but cannot find a way to do this in gxmessage.

Could I avail on your advice again?

And thanks once more for all the assistance.

1 Like

Either of these should do it. :slight_smile:

s="battery: $batt1% + $batt2% $batt3\n\nuptime: $uptime\n\nweather: $weather"
echo -e $s | gxmessage -file -
s="battery: $batt1% + $batt2% $batt3

uptime: $uptime

weather: $weather"
gxmessage $s

Perhaps there’s a better/nicer way of displaying this (ie without gxmessage), unfortunately I don’t have a pinephone so… :man_shrugging:

Does conky work on pinephone?

1 Like

Your first approach did it; the second approach had no effect on the print-out; I wonder why.

I looked high and low for a carriage return solution, so your commands were familiar, I just never found examples of how to put them together.

Odd that it was so easy to find/use something like batt3="= $(($batt1 + $batt2))%" which is much more complicated than simple spacing.

Re: conky–I’ve always been a fan and probably could get it going on Phosh. But the trouble with Phosh is that its home screen is a mess, so I rarely go there and just keystroke whatever I can. I remember in earlier Lubuntu running a permanent conky across the panel and window titlebar with uptime, etc. The setup was like Window’s Serious Samurai, if that rings a bell, though I’m not certain Phosh would allow such placement.

As for marking this solved, I’m going to mark your last post as the solution, even though you solved the script issues in two posts. Unless you suggest differently.

I learned a million things–thank you all.

1 Like

Both work here, so it may be pinephone specific.

That’s dealt with by the shell before being passed to gxmessage.

The newline is a special char (which represents a line feed aka LF) and should be dealt with by gxmessage, except that the dev has chosen not to implement that functionality.

It does however work with literal line feeds (LF), which is what you get with either of those methods, and also copy/pasting the literal LF char (unicode) but that gives the same result as pressing enter/return.

That explains it :slight_smile:

I did explore the nuances of newline, but if the developer hadn’t incorporated it, at least I can understand why I flamed out.

For futures scripting, I assume your second approach in post#15 would work with other programs/scripts outside of gxmessage. Would that be correct?

It depends on how it’s written, but if it accepts a string as a parameter then it should work.

Some programs will interpret the \n properly, however you may need a flag to get the behaviour. (eg echo -e "hello\n")

Others can accept stuff through a pipe |, like the first method. Some programs may work with more than one method.

If you haven’t already, then get used to using man to find out what flags exist and what they do.

Some easy reading:

man echo
man cd

Some less easy reading. :smile:

https://tldp.org/LDP/abs/html/

1 Like