Page 1 of 1
Simple question, yet not found solution :)
Posted: Friday 10 July 2020 16:30
by jadijkstra2000
Hi all,
I have a little question... the only thing I want to do is to read the state of a switch in Bash...
how can I do that? I can use json but, not found how to call a specific value (Data or Status) of a switch..
I have a script (doorbell) which needs to check if gong is turned on yes or no, based on that the gong works or not...
Anyone?

Re: Simple question, yet not found solution :)
Posted: Friday 10 July 2020 17:44
by waaren
jadijkstra2000 wrote: ↑Friday 10 July 2020 16:30
I have a script (doorbell) which needs to check if gong is turned on yes or no, based on that the gong works or not...
Could be something like
Code: Select all
domoticzIP=127.0.0.1
domoticzPort=8084
gongIDX=382
devicestatus=$(curl -s "$domoticzIP:$domoticzPort/json.htm?type=devices&rid=$gongIDX" | grep '"'Status'"' )
if [[ $devicestatus == *"On"* ]]; then
echo Gong is On
else
echo Gong is Off
fi
Re: Simple question, yet not found solution :)
Posted: Friday 10 July 2020 17:58
by heggink
probably better to not grep for Status (which returns the entire Status = "result" line), pipe the result into the command line json processor jq:
Code: Select all
devicestatus=$(curl -s "http://domoticz:8080/json.htm?type=devices&rid=1670" | jq .result[0].Status )
Re: Simple question, yet not found solution :)
Posted: Friday 10 July 2020 18:34
by waaren
heggink wrote: ↑Friday 10 July 2020 17:58
probably better to not grep for Status (which returns the entire Status = "result" line)
Please help me understand why you think using jq is better. My example does grep for "Status" so including the quotes.
I timed both methods bout could not see any difference in result nor in real, -user and sys times
Re: Simple question, yet not found solution :)
Posted: Friday 10 July 2020 20:04
by jadijkstra2000
Awesome, this is exactly what I am searching for

Re: Simple question, yet not found solution :)
Posted: Friday 10 July 2020 20:19
by heggink
waaren wrote: ↑Friday 10 July 2020 18:34
heggink wrote: ↑Friday 10 July 2020 17:58
probably better to not grep for Status (which returns the entire Status = "result" line)
Please help me understand why you think using jq is better. My example does grep for "Status" so including the quotes.
I timed both methods bout could not see any difference in result nor in real, -user and sys times
As I said, grep returns the entire line
"Status" : "Off" and puts that into variable devicestatus. Either you check against that (instead of just "On" or "Off") or you use jq to only return the "Status" variable's value or just put the 3rd argument into devicestatus (there's lot's of ways). Since the output is json, a json processor might be easier to also test other fields and their values. Just a thought.
Re: Simple question, yet not found solution :)
Posted: Friday 10 July 2020 20:23
by jadijkstra2000
It's working like a charm
Thanks guys!!!