Variable in Shell Script not working

Trying to post a KM Variable with a curl command to Home Assistant

that´s what I have so far:

curl -X POST -H "x-ha-access: MYPASSWORD" \
    -H "Content-Type: application/json" \
    -d '{"state": "$KMVAR_TotalMins", "attributes": {"friendly_name": "iMac Aktiv"}}' \
    https://MYHASSIOURL/api/states/sensor.imacaktiv

but I get there only "$KMVAR_TotalMins" back and not the variable itself, can somebody help?

Do you mean that the variable $KMVAR_TotalMins isn't being evaluated, and instead it sends the literal string "$KMVAR_TotalMins" inside the JSON data string ?

If this is the case, I believe it's because you've used single quotes with the -d option, and the shell won't expand/evaluate variables if they are contained inside single quotes. Therefore, you might need to change your single quotes to double quotes; then go through your JSON string and escape all of the double quotes within. I've done it here for you, so see if that solves the problem:

curl -X POST -H "x-ha-access: MYPASSWORD" \
    -H "Content-Type: application/json" \
    -d "{\"state\": \"$KMVAR_TotalMins\", \"attributes\": {\"friendly_name\": \"iMac Aktiv\"}}" \
    https://MYHASSIOURL/api/states/sensor.imacaktiv
1 Like

yes, that fixed it - its working now, thaks alot!

15