Catch shell_command output?

How can I catch the output (standard out) of a shell_command into HA?

And no, I don’t want to use a command line sensor, since that is called with an interval. I just need to run a script through an automation.

Have you considered using the REST API? You could just add a
&& curl PARAMETERS at the end of your shell command

I am pretty new to HA and don’t understand exactly what you mean. Could you please be more specific?

You could do something like this

shell_command:
  something: YOUR_CMD | curl -X POST -H "x-ha-access: YOUR_PASSWORD" -H "Content-Type: application/json"  -d '{"state": "$(</dev/stdin)", "attributes": {"unit_of_measurement": "°C"}}' http://localhost:8123/api/states/sensor.YOURSENOR

something along those lines (meaning not tested and possibly wont work like this…) Eould be better to write a small script here and just call that…but that’s basically it.

~Cheers

4 Likes

Ok. I get it. Will give it some thought.

Did you ever figure this out? I need to do something similar. Is it possible to set the scan_interval to 0 to have it only set a state when queried?

EDIT:

I figured this out using the REST api called via python script although curl works fine as specified above. My python script pulls the values from secrets.yaml to be a little cleaner. Can look on github for the shell_command.

1 Like

Can you please post the link to your python_script? I am looking for something similar…thanks!

So this is probably the best I have at this point and probably overkill. https://github.com/tschmidty69/homeassistant-config/blob/master/shell_command/get_playlist.py

But the basic idea is within my script I am getting some data, then calling the REST api to set a sensor value. If you can provide exactly what you are trying to do I may be able to be more helpful…

I am trying to connect Machinebox image classification with HA. I am using a command line sensor for now and it works well.

  - platform: command_line
    name: Porch
    value_template: "{{ value_json.tags|map(attribute='tag')|join(', ') }}"
    command: >-
      curl -H 'Content-Type: application/json' -d '{"url":"https://bla.duckdns.org/api/camera_proxy/camera.porch?token=xyz"}' http://192.168.2.212:8081/tagbox/check

The only problem is it cannot be used in an automation. So, I want it to run only, say, when motion is detected on my cameras. Let me know if you need additional details.

Hmm, you can’t trigger on the value of that sensor? you should be able to use sensor.porch in an automation. You are setting the value to me the tag attributes joined together. Depending on how your motion is reported, you may want to set another binary_sensor and do a jinja if statement to set true/false for motion.

Running it as a sensor takes up API calls and is heavy on the system. Ideally, I would like to run the command only when required (i.e., motion detected).

We need a REST service :slight_smile:

Thanks for this - sorted out an issue I was having with rest not taking variables in the payload!