Shell_command return data?

How to do this,
the reason why i’m trying is that the REST platform doesn’t support variables in the payload and that’s really annoying

I’m using payed version of 17track and created a command that registers the package number, that works really great.

However i need to use this code to return the data.
but how to return the data when executed ??

shell_command:
track_17_details: ‘curl -X POST --header “17token: MYAPI” --header “Content-Type: application/json” --data “[ { “number”: “{{ number }}”, “carrier”: “{{carrier}}” } ]” https://api.17track.net/track/v2/gettrackinfo

In the rest api it does return when manually input the trackingcode, but like said before variables dont work, and i cant find a way arround it… only using shell_command…

SO what i want is that the shellcommand returns the data in a entity like rest does, is that possible ?

I do it by putting the command in a bash script and then (in the script) calling the HA API. Here is an example :

The shell command:

shell_command:
  get_aircraft_reg: bash /config/scripts/aircraft_reg.sh {{states('sensor.low_level_registration')}}

The bash script:

#!/bin/sh

echo $1

curl --request GET --url https://hexdb.io/api/v1/aircraft/$1 > /tmp/aircraft_reg && sed -i 's/Registration/state/g' /tmp/aircraft_reg | curl -X POST -H 'Authorization: Bearer Your Token' -H 'Content-Type: application/json'  -d @/tmp/aircraft_reg  http://192.168.1.25:8123/api/states/sensor.aircraft_reg

I hope this gives you an idea of what you can do :slight_smile:

Alternatively, you can use a rest sensor like this :

rest:
   - resource_template: https://api.spotify.com/v1/search?q={{ states('sensor.media_title_office')|default('')|replace(' ','%20')|replace("'",'%27') |replace(':','%3A') }}%2C%20{{ states('sensor.media_artist_office') |default('')|replace(' ','%20')|replace("'",'%27')|replace(':','%3A')|regex_replace('\([0-9]+\)')}}&type=track%2Cartist&limit=1
     headers:
       Authorization: Bearer {{ state_attr("sensor.spotify_token","access_token") }}
       Content-Type: application/json
       Accept: application/json
     scan_interval: 1800
     sensor:
       - name: spotify_url_rest
         unique_id: sur
         value_template: 'OK'
         json_attributes_path: "$.tracks.items[0].external_urls"
         json_attributes:
           - spotify
       - name: spotify_track_rest
         unique_id: str
         value_template: 'OK'
         json_attributes_path: "$.tracks.items[0]"
         json_attributes:
           - name
       - name: spotify_artist_rest
         unique_id: sar
         value_template: 'OK'
         json_attributes_path: "$.tracks.items[0].artists[0]"
         json_attributes:
           - name
       - name: spotify_album_rest
         unique_id: sal
         value_template: 'OK'
         json_attributes_path: "$.tracks.items[0].album"
         json_attributes:
           - name

Set the scan interval to something like 3600 and then just call the refresh like this :

action:
  - delay:
      hours: 0
      minutes: 0
      seconds: 5
      milliseconds: 0
  - service: homeassistant.update_entity
    data: {}
    target:
      entity_id:
        - sensor.spotify_artist_rest
        - sensor.spotify_track_rest_3
        - sensor.spotify_url_rest
        - sensor.spotify_album_rest

many many thanks @Neil_Brownlee

as i want to keep it simple, how can i modify the rest sensor like you did ?
i have something like this:
but don’t know how to get arround the payload.

If i manually fill in the number and carrier this works, however no variables.

rest:

  • authentication: basic
    scan_interval: 86400
    resource: https://api.17track.net/track/v2/register
    method: POST
    headers:
    content-type: application/json
    17token: MYAPI
    payload: |
    [
    {
    “number”: “{{ states(‘input.track_17_carrier_number’) }}”,
    “carrier”: “{{ states(‘sensor.track_17_carrier_mapping’) }}”
    }
    ]
    sensor:
    • name: track_17_reg
      value_template: “{{ value_json }}”

i also did try an other approach but im stuck.
i did use the command_line platform to capture the output.

sensor:

  • platform: command_line
    name: tracking_details
    command: “bash -c ‘{{ shell_command.get_tracking_details }}’”
    value_template: “{{ value_json }}”

however this throws me errors.

With rest, you need to use :

resource_template:

Although looking at it, maybe that won’t help if you can’t send the variables in the URL. I am sure that’s why I went with the bash scripts in the end for some of my calls. There may be better ways, but until you can use templates in some of the calls you are sort of banjaxxed.

ok,
and using this way ?
i tried but getting errors.
as it should work this way… just sad the devs do not allow variables in the payload…

sensor:

  • platform: command_line
    name: tracking_details
    command: “bash -c ‘{{ shell_command.get_tracking_details }}’”
    value_template: “{{ value_json }}”

No variables allowed in the command: sadly.

its just weird, in shell_command: i can use the variables but not retrieve the data…
well i’m stuck now lol

however this dude did manage to do it, but i need to find out how

ive done it, i found a way arround how to do it when rest doesnt support variables… hehe

1 Like

You need to share you solution to assist others :slight_smile:

i forget, here is my solution to this problem

i created this to run the same command as i did in rest with command_line and it’s basically the same except u can use variables in the payload.

command_line:

  • sensor:
    name: tracktest
    scan_interval: 9000
    json_attributes:
    - data

    command: >-
    curl -X POST --header “17token: MYTOKEN” --header “Content-Type: application/json” --data “[ { “number”: “{{ states(‘input_text.17_track_code_input’) }}”, “carrier”: “{{ states(‘input_text.17_track_carried_input’) }}” } ]” https://api.17track.net/track/v2/gettrackinfo
    value_template: “{{ value_json.data.tracking_result }}”