Shell script and returning or setting HA values from within

Got a bash shell script that I pass 3 values to and it creates a 3 large integer numbers that I would like to have available for use inside an HA script.

Is there a method to set HA variables from within the shell script or return with the values or read as stdout or the like. Having trouble finding examples of such.

It would be very convenient if I could read and write to the variable component.

Thanks, Ynot.

The simplest method is to use curl to send messages to set the value of sensors to the rest api.

If you already have MQTT set up, you could use mosquitto_pub to send values via MQTT.

Everything you ‘echo’ to stdout will be in the resulting sensor value.

Can you elaborate a bit on this approach. New to HassIO and not sure exactly how to configure a sensor to view or read the stdout from my script. Plans are to issue a call to the shell script using the shell_command component.

The shell script will echo three integer values that I would like to capture and use within HASSIO scripts for further actions.

Also an example of the curl approach would also be helpful.

Thanks for your help. Ynot.

This sets the state of sensor.curl_sensor to 1. Note that this sensor doesn’t have to exist before using the call, HA creates it automatically.

$ curl -X POST -d '{"state": 1}' http://localhost:8123/api/states/sensor.curl_sensor

{"attributes": {}, "entity_id": "sensor.curl_sensor", "last_changed": "2018-06-26T13:03:16.969074+00:00", "last_updated": "2018-06-26T13:03:16.969074+00:00", "state": "1"}

Obviously, change the address and use a password if necessary for your setup.

1 Like

Well that seems pretty straight forward. I can’t believe how much time I spent googling the approach but not gettting a clear and simple example. Thank you will give it a shot tonite.

I presume I can use a curl GET to similarly retrieve info from HA sensors or variables, etc…

I appreciate the help. Ynot

This worked perfectly. Thanks for the help. Ynot.

1 Like

I used this before with webhook:
curl -X POST http://homeassistant.local:8123/api/webhook/backyard_motion

however, I am getting some error:
curl -X POST -d ‘{“state”: 12.5}’ http://homeassistant.local:8123/api/states/sensor.smart_battery
401: Unauthorized