Shell_command access dict in response_variable

Good morning,

i need help to access diffent keys in dict inside the respons_variable of shell_command.

For example i want to read the value inside key ‘value’ which is 255

Here is my code an the return inside respons_variable status

I am only able to access the dict behind stdout with " {{ status[‘stdout’] }} " but i have no clue how to access keys. I have triede different methods but nothing works for now

" {{ status[‘stdout’].value }} "
" {{ status[‘stdout’] }}[‘value’] "

I hope you can help me with this issue

Code:

configuration.yaml:

shell_command:
  # Get token for auth
  theben_login: 'curl -k "https://192.168.178.200/rest/login" -X POST -H "Accept: application/json, text/plain, */*" -H "Content-Type: application/json;charset=UTF-8" --data-raw  "{\"username\": \"admin\",\"password\": \"admin\"}"'
  
  theben_j4_hoehe: 'curl -k "https://192.168.178.200/rest/datapoints/values" -X "PUT" -H "Accept: application/json, text/plain, */*" -H "Content-Type: application/json;charset=UTF-8" -H "{{ token }}" --data-raw "{\"datapoints_values\": [{\"id\":{{ id }},\"value\":\"{{ value }}\"}],\"command\": 3}"'
  
  theben_j4_pos: 'curl -k "https://192.168.178.200/rest/datapoints/values" -X "PUT" -H "Accept: application/json, text/plain, */*" -H "Content-Type: application/json;charset=UTF-8" -H "{{ token }}" --data-raw "{\"datapoints_values\": [{\"id\":{{ id }},\"value\":\"{{ value }}\"}],\"command\": 3}"'
  
  theben_j4_hoehe_1: 'curl -k "https://192.168.178.200/rest/datapoints/values" -X "PUT" -H "Accept: application/json, text/plain, */*" -H "Content-Type: application/json;charset=UTF-8" -H "{{ token }}" --data-raw "{\"datapoints_values\": [{\"id\":174,\"value\":\"255\"}],\"command\": 3}"'
  
  theben_j4_status: 'curl -k "https://192.168.178.200/rest/datapoints/{{ id }}" -X "GET" -H "Accept: application/json, text/plain, */*" -H "Content-Type: application/json;charset=UTF-8" -H "{{ token }}"'

  theben_j4_test: 'curl -k "https://192.168.178.200/rest/datapoints/values" -X "PUT" -H "Accept: application/json, text/plain, */*" -H "Content-Type: application/json;charset=UTF-8" -H "{{ token }}" --data-raw "{\"datapoints_values\": [{\"id\":{{ id }},\"value\":\"{{ value }}\"}],\"command\": 3}"'

automation.yaml:

- id: '1711567216847'
  alias: j4_up
  description: ''
  trigger: []
  condition: []
  action:
  - service: shell_command.theben_login
    response_variable: return_token
    data: {}
  - service: shell_command.theben_j4_status
    response_variable: status
    data:
      token: 'Cookie: user=%22{{ return_token[''stdout''] }}%22'
      id: 174
  - service: notify.persistent_notification
    metadata: {}
    data:
      title: Theben Status
      message: " {{ status['stdout'] }} "
  mode: single```

If it’s a real dict object:

{{ status['stdout']['value'] }}

If it’s a string that looks like a dict:

{{ (status['stdout']|from_json)['value'] }}
1 Like

Thank you very much!

It now works inside the message with:

  - service: notify.persistent_notification
    metadata: {}
    data:
      title: Theben Status
      message: " {{ (status['stdout']|from_json)['value'] }} "

And outputs the value inside the key value as String.

Now i want to use the return value inside a script to use it as data for a shell_command call

is this the right way to do it?

shell_command:
  # Get token for auth
  theben_login: 'curl -k "https://192.168.178.200/rest/login" -X POST -H "Accept: application/json, text/plain, */*" -H "Content-Type: application/json;charset=UTF-8" --data-raw  "{\"username\": \"admin\",\"password\": \"admin\"}"'
  
  theben_j4_hoehe: 'curl -k "https://192.168.178.200/rest/datapoints/values" -X "PUT" -H "Accept: application/json, text/plain, */*" -H "Content-Type: application/json;charset=UTF-8" -H "{{ token }}" --data-raw "{\"datapoints_values\": [{\"id\":{{ id }},\"value\":\"{{ value }}\"}],\"command\": 3}"'

scripts.yaml:

j4_stop:
  alias: j4 stop
  description: ''
  sequence:
  - service: shell_command.theben_login
    response_variable: return_token
    data: {}
  - service: shell_command.theben_j4_status
    response_variable: status
    data:
      token: 'Cookie: user=%22{{ return_token[''stdout''] }}%22'
      id: 174
  - service: shell_command.theben_j4_hoehe
    #response_variable: test 
    data:
      token: 'Cookie: user=%22{{ return_token[''stdout''] }}%22'
      id: 174
      value: '{{ (status[''stdout'']|from_json)[''value''] }}'
  mode: single