Shell command works but yaml editor gives error

I’m trying to setup the following Grocy shell command as a shell script that I can run as a service:

curl -X 'POST' 'http://homeassistant.local:8888/api/stock/products/1/consume' -H 'accept: application/json' -H 'Content-Type: application/json' -H 'GROCY-API-KEY: redacted' -d '{"amount": 3,"transaction_type": "consume","spoiled": false}'

The command is from the Grocy REST-API and works perfectly in the terminal. However, when I try to save it as a shell command as follows I get the error: “incomplete explicit mapping pair, a key node is missed

shell_command:
  grocy_consume_item_shell_4: "curl -X 'POST' 'http://homeassistant.local:8888/api/stock/products/1/consume' -H 'accept: application/json' -H 'Content-Type: application/json' -H 'GROCY-API-KEY: redacted' -d '{"amount": 3,"transaction_type": "consume","spoiled": false}'"

I’m pretty sure it has something to do with the quotes this part but I can’t say for sure:

 '{"amount": 3,"transaction_type": "consume","spoiled": false}'"

Any ideas?

First you do not need to enclose command in double quote. Make it just:

shell_command:
  grocy_consume_item_shell_4: curl -X 'POST' 'http://homeassistant.local:8888/api/stock/products/1/consume' -H 'accept: application/json' -H 'Content-Type: application/json' -H 'GROCY-API-KEY: redacted' -d '{"amount": 3,"transaction_type": "consume","spoiled": false}'

and check if this helps…

Thanks for the suggestion. Unfortunately, I still get the same error.

When I run the code in an online yaml checker I get the following error:

Error : incomplete explicit mapping pair; a key node is missed; or followed by a non-tabulated empty line at line 2, column 119:
     ... k/products/1/consume' -H 'accept: application/json' -H 'Content- ... 
                                         ^
Line : undefined  undefined

Found a solution here.

In case someone else is having the same problems this worked:


shell_command:
  grocy_consume_item: "curl -X POST http://{{server_ip}}:9283/api/stock/products/{{product_id}}/consume -H 'Accept: application/json' -H 'Content-Type: application/json' -H 'GROCY-API-KEY: {{grocy_key}}' -H 'cache-control: no-cache' -d '{ \"amount\": {{amount}},  \"transaction_type\": \"consume\",  \"spoiled\": false }'"

A good method is to use:

Both are tools that you can install using pip.

Is it possible to run these on RPi4 with HA?

I do not see why not. I can do it on a NUC using HAOS.

This is how I configured the ‘Terminal & SSH’ Addon:

authorized_keys:
  - >-
    ssh-rsa
    AAAAB...
    name@host
password: ""
apks:
  - screen
  - logrotate
  - sqlite
  - py3-pip
server:
  tcp_forwarding: false

That makes pip available, do then just do pip install yamllint pip install yamlfix .
The only drawback is that you have to run that after each ‘Terminal + SSH’ restart. (The installation could be automated by adding something smart to .bash_profile. (I already share my bash setup - maybe I’ll add something smart to install the packages in a requirements.txt somewhere only after restart or requirements.txt updates ).

I updated my bash setup to install the python packages listed in requirement.txt that is in the same directory as the real .bash_profile.