What the heck: why there is no shell command that provides response back?

I found it very troublesome to use REST sensors - these updates rarely and with somhow random order. So if need to create automation that relies on values of two or more REST sensors it creates race condition depending on which sensor updates first. This could be avoided if we could have shell command that provide response back to lets say predefined input_text or input_number. At the moment I’m fighting this with command that redirect response to file combined with file sensor, but this is unnecessary additional step and also need to fight here with 255 character limitation for response. Having something like REST sensor, but having control over when it updates value would solve the problem.

I’m a little confused at what is being asked for here. You can set the scan_interval for rest sensors, so update time shouldn’t be an issue.

There are command line sensors if you wanted to return a value from a shell command.
In what ways do these options not solve your issue? The state limit is, however, unfortunately unavoidable.

It is still issue. Specific example I struggle with - DSM Download Station sensors:

  • I have a sensor that logs into DSM using credentials and retrieve Session ID (SID)
  • using SID I connect to DSM to retrieve download tasks list
  • having this list I extract individual tasks IDs
  • having tasks IDs I connect to DSM to retrieve details of each download

all of this is doen using REST sensors. The problem is that sometimes tasks are finished or started between sensors update causing that order of individual sensors does not match order of tasks IDs, creatin undesirable results ahe data from unrelated downloads is presented for one task. Typical race condition. For such case strict control over order of performing of steps is required. So this is not question of interval between refreshes, but order in which these refreshes are performed.
You refer to command line sensor that return value from shell command… Frankly speaking after endless hours of searching I did not found this being available, without some additional steps/tricks. Could you point me to the right direction, please?

You’re going a bit above and beyond the intent of REST sensors. It sounds more like you need to make a custom integration.

https://www.home-assistant.io/integrations/sensor.command_line/ these are the docs for command line sensors

:smiley: yes, you are right… but I’m very casual programmer… not knowing python at all… so I’m just trying to find my way with tools that are available. And after searching for solution I found that I’m not the only who have this problem, so decided to raise this topic…

It does sound to me also like you’re going beyond what automations/REST sensors are really meant for.

Short of creating an integration, I think a better fit for you would likely be to just create a bash shell script which you then run via the command_line sensor.

I have some similar devices where I need to go through a few steps before I can get the information I actually want, in the correct format to store in Home Assistant, and I use a bash script to handle all those details and just return the final info to the command_line sensor.

Shell scripts are easier to get started with than python, as you can basically list the commands as you’d enter them at shell prompt (with the benefit of curl, grep, awk, etc), which you may be more familiar with, rather than learning python-specific libraries etc.

In case they’re new to you, here’s a example (set the file executable by all users: chmod a+x example.bash), if you had to get a session_id, before retrieving a list of Field=Value pairs (one per line), and you wanted to collect a specific one as the sensor value, you could do something like:

#!/bin/bash
DEVICE=192.168.0.1
SESSION_ID=`curl -q --user=username:password https://$DEVICE/get_session` 
# get_data returns a list Field=Value one per line, find just that line, print just the 2nd field
curl -q -H "Cookie: Session=$SESSION_ID" https://$DEVICE/get_data | grep SomeField | awk -F= '{print $2}'

Yeap, fully agree… but if we have response from shell_command no integration would be needed and way more things could be done natively in HA. :smiley:

I’m still confused by what you are asking for. How does the command line sensor not satisfy you “shell command returning value” requirement?

Simple example:

  • sensor 1 logs on to DSM and retrieves SID that allows to retrieve info in future calls.
  • sensor 2 uses SID information to retrieve info about list of download tasks.
  • sensor 3 uses SID and additional info from sensor 2 to retrieve details of specific task detail.

Now, since these sensor are retrieving information in non coordinated way it could happens that before sensor 3 wil try to execute, sensor one will repeat login to DSM and obtain new SID, which will cause sensor 3 to fail, as it will try to authenticate with obsolete SID. These operations need to be executed in specific order. Automation of script can be used to control order of operations, but in this case cannot trigger sensor update.
If shell command could return responce, it could be used instead of sensor to keep desired order of operations.
My idea was that since we already have shell_command that can trigger any script at any moment and we have rest sensor, that is doing basically the same, but is capable of returning output of script, but without control over time, it should be easy to combine these two and have shell_command returning value to lets say input_text or input_number entity.
Alternatively; can we have possibility to disable automatic sensor refresh (refresh_interval: 0, or something like that) and have service available, that would force sensor refresh (service: sensor.refresh)? Result would be the same - obtaining REST value in time controlled way.

You could perform all of the necessary steps in a single command, so I’m still not getting why it would be necessary to do it exactly the way you are describing.

There is already a service to manually update entities homeassistant.update_entity. If you wanted manual control just set the scan_interval to high number, and call homeassistant.update_entity as needed. Then you would have full control over when the new state populates.

:open_mouth: :open_mouth: :open_mouth: :open_mouth:
I was not aware about homeassistant.update_entity service will test this for sure!