Command line sensor with JSON response

Hi all,
how do you define a command line sensor that has a JSON formatted state?

I don’t mean simply:

  • platform: command_line
    name: my_command_line
    command: /somthing/script.sh
    value_template: “{{ value_json./some_key/}}”

because that retains only one value.

I mean having the whole JSON string and being able to parse it in templates, such as

service: persistent_notification.create
data_template:
message: “{{ states.sensor.my_command_line.state./some_key/ }}”

Thanks,
gl

Can you post an example of the value returned by /somthing/script.sh ? It would help understanding what you need.

It would be something like:

{“id”:“1234”,“date”:“1482759143”,“sender”:“73951041”,“text”:“Something Important”}

In general, my problem is to retrieve multiple information at once from an external script: is that possibile at all in ha?

thanks
gl

not without making a custom sensor, if i am correct.

commandline sensors cant retrieve attributes, unless there is a way i didnt find.

Hi MrMep,

I know this thread is a little old but I only just joined this forum. I have an idea from a logic point of view but am unsure of the implementation (or if its possible)

Could you format the json response as a single string with some sort of a separator or separators (I use , and ; a lot with PowerShell) then return this “string” as a value and split the values back out in a script on the HA side?

Hi,
I finally decided to go the other way around: made a python script that manages the data and send commands/events to HA.
The problem with your solution, if I understand it correctly, is that it isn’t possible to assign those values to attributes of entities in HA, unless through a custom sensor, as previously mentioned.
But I may be wrong…

Good to hear you got it working. Yes I guess that’s right that you would have to create custom sensors as ReneTode mentioned earlier.

Can you share your python script? (if you didn’t already)?

I was trying to interface with Telegram: that was before I started modifying hass-telebot, that’s the script. I will share my version as soon as it gets a little tidier…

1 Like