JSON + jq mathematics on multiple idx values
Posted: Tuesday 22 March 2022 15:25
I can read multiple values divided over as many idx data.
In bash I can do some mathematical as shwon in the 3rd line but somehow it pastes the 2nd value behind the first without really adding them together like 1 + 2 = 3
bash script Epower.sh:
#!/bin/bash
curl -s "http://localhost:8060/json.htm?type=devices&rid=1" | jq -r '.result[0] .Counter'
curl -s "http://localhost:8060/json.htm?type=devices&rid=1" | jq -r '.result[0] .CounterDeliv'
curl -s "http://localhost:8060/json.htm?type=devices&rid=1" | jq -r '.result[0] .Counter + .result[0] .CounterDeliv'
curl -s "http://localhost:8060/json.htm?type=devices&rid=17" | jq -r '.result[0] .Data'
Outcome:
root@raspberrypi:/home/pi# /home/pi/scripts/Epower.sh
6961.507
4468.416
6961.5074468.416
2328 Watt
And additionally I have a 4th value from idx 17 to which I also want to do an add and minus caluculation but how do I mix that in the equation ?
I understand I need to make the value "clean" without the "Watt" designation but then I still do not know how to make a calculation with values from both idx 1 (Power meter) and idx 17 (Solar Inverter) in the samle mathematical calculation.
Who can help me with the above issues ?
Thank you.
In bash I can do some mathematical as shwon in the 3rd line but somehow it pastes the 2nd value behind the first without really adding them together like 1 + 2 = 3
bash script Epower.sh:
#!/bin/bash
curl -s "http://localhost:8060/json.htm?type=devices&rid=1" | jq -r '.result[0] .Counter'
curl -s "http://localhost:8060/json.htm?type=devices&rid=1" | jq -r '.result[0] .CounterDeliv'
curl -s "http://localhost:8060/json.htm?type=devices&rid=1" | jq -r '.result[0] .Counter + .result[0] .CounterDeliv'
curl -s "http://localhost:8060/json.htm?type=devices&rid=17" | jq -r '.result[0] .Data'
Outcome:
root@raspberrypi:/home/pi# /home/pi/scripts/Epower.sh
6961.507
4468.416
6961.5074468.416
2328 Watt
And additionally I have a 4th value from idx 17 to which I also want to do an add and minus caluculation but how do I mix that in the equation ?
I understand I need to make the value "clean" without the "Watt" designation but then I still do not know how to make a calculation with values from both idx 1 (Power meter) and idx 17 (Solar Inverter) in the samle mathematical calculation.
Who can help me with the above issues ?
Thank you.