Get specific attribute from a state with curl

Hey there! This is more a beginner question, not a program expert but I hope someone can help:

I try to get the state of an entity with an curl command. I use Keyboard Maestro on my Mac, where I can execute Shell scripts. I found on the REST API docs the command:

curl -X GET -H "x-ha-access: Password" -H "Content-Type: application/json" http://IPAdress:8123/api/states/media_player.schlafzimmer

which works fine, no problem. The thing is I just want to get one specific attribute from the result (“media_album_name”) - Do I have to add something on that curl command?

I don’t think the api or curl will let you get specific attributes, you need to do some post processing of the result from curl. jq is a unix command line that enables you to parse the json output from curl, like

$ curl -s http://hassbian.local:8123/api/states/automation.appdaemon_started|jq '.state'
"on"

I don’t know if jq is available by default on a mac, but its home page is here. If not, you can probably implement a filter with grep, but that’s going to be more work.

thanks for your reply! Found a solution in Keyboard itself with “Regular Expressions”

Ok, looks like you already have a solution. Cool! But since I already went through the effort, I’ll go ahead and reply. Maybe someone else could use this info…

If you wanted to do it with grep, you could use this:

curl ... | grep -o '"media_album_name": "[^"]*"' | grep -o '"[^"]*"$'

Or, you could use the jq command that @gpbenton mentioned. (Thanks gpbenton, didn’t know about it. Very cool! And it’s available for Linux on rpi - “sudo apt-get install jq”):

curl ... | jq '.attributes.media_album_name'
1 Like

wow thanks for the reply @pnbruckner! this grep solution looks way more nicer!

1 Like

hi there
guys, i see you are working with the JQ command

can you help me with my first question here.? i think i need the same, just dont know how i need to put the JQ in my command line

command: (echo ‘{“cmd”:“listactions”}’; sleep 1) | nc XXXX 8000 | jq ‘.id .value1’

something like that i need? to get .id and .value1 from the data array

You can do it in a bash script… Have a look in my repo for my abbusage script - I use jq etc there…

ok, in what yaml file are you using it?

ok, looked in your .sh files, but dont see it :slight_smile:

Look in the repo! It’s in configuration.yaml (A shell command down the bottom and also an automation that calls it)

this one

jq -r ‘.services.NBN[0].service_id’)

so what was your curl output?
you had multiple services.NBN ?
and that created an array of sensors?

Look in the abbusage_ha.sh - that one just sets up auth.

It creates one sensor that has a bunch of attributes. There’s a screenshot of it. (You could create multiple sensors)

The json file it pulle looks like this:
{“usedMb”:265358,“downloadedMb”:228091,“uploadedMb”:37268,“remainingMb”:null,“daysTotal”:31,“daysRemaining”:20,“lastUpdated”:“2019-05-17 19:32:04”}

Answered in your topic.

BTW, I get that you weren’t getting any answers right away, but asking the same question in five or more topics is a bit much.

Ok, sorry for this