Simple question, yet not found solution :)

All kinds of 'OS' scripts

Moderator: leecollings

Post Reply
jadijkstra2000
Posts: 58
Joined: Monday 26 May 2014 10:18
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Simple question, yet not found solution :)

Post 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? :)
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Simple question, yet not found solution :)

Post 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
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
User avatar
heggink
Posts: 979
Joined: Tuesday 08 September 2015 21:44
Target OS: Raspberry Pi / ODroid
Domoticz version: 12451
Location: NL
Contact:

Re: Simple question, yet not found solution :)

Post 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 )
Docker in Truenas scale, close to latest beta
DASHTICZ 🙃
RFXCOM, zwavejs2mqtt, zigbee2mqtt,
P1 meter & solar panel
Google home, Wifi Cams motion detection
Geofence iCloud, Bluetooth & Wifi ping
Harmony hub, Nest, lots more :-)
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Simple question, yet not found solution :)

Post 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
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
jadijkstra2000
Posts: 58
Joined: Monday 26 May 2014 10:18
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Simple question, yet not found solution :)

Post by jadijkstra2000 »

Awesome, this is exactly what I am searching for :)
User avatar
heggink
Posts: 979
Joined: Tuesday 08 September 2015 21:44
Target OS: Raspberry Pi / ODroid
Domoticz version: 12451
Location: NL
Contact:

Re: Simple question, yet not found solution :)

Post 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.
Docker in Truenas scale, close to latest beta
DASHTICZ 🙃
RFXCOM, zwavejs2mqtt, zigbee2mqtt,
P1 meter & solar panel
Google home, Wifi Cams motion detection
Geofence iCloud, Bluetooth & Wifi ping
Harmony hub, Nest, lots more :-)
jadijkstra2000
Posts: 58
Joined: Monday 26 May 2014 10:18
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Simple question, yet not found solution :)

Post by jadijkstra2000 »

It's working like a charm :)

Thanks guys!!!
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest