Return data from RESTful command?

I have implemented a RESTful_command that communicates with my whole-home audio amp to turn various rooms on/off and I have it working just fine. However, the API call does return some status information (JSON formatted) that I would like to use. Is there any way to get access to that JSON data?

Here’s how I handle a situation like that.

I have a sprinkler system that I parse JSON for a sensor value.

The sensor is defined thusly:

  - platform: command_line
    name: sprinkler last run
    command: python3 ~/.homeassistant/scripts/
    scan_interval: 43200

the python script looks like this:
import requests, time
from datetime import datetime, timedelta

startDate = ( - timedelta(days=5)).strftime('%s');
endDate ='%s');
url = '' + startDate + '&edate=' + endDate
r = requests.get(url);
logs = r.json()['logs'];
maxdate = 0;
#iterate through all the entries finding the oldest one that was more than 5 minutes
for z in range(0, len(logs)):
    for e in range (0, len(logs[z]['entries'])):
        duration = logs[z]['entries'][e]['duration'];
        rundate = logs[z]['entries'][e]['date'];
        if (duration > 300 ) and (rundate > maxdate):
            maxdate = rundate;
diff = - datetime.fromtimestamp(maxdate)
print (diff.days)

Interesting. Can you make the sensor update “on-demand”? That’s really the issue I am trying to solve… I need to be able to programmatically request an update. The problem I have been having with anything in sensors: is that it only updates at scan_interval and I am trying to get it to update as part of an action in an automation.

A command line switch has a defined on-command, off-command, and state-command.

Why not set up a sensor?

I guess I should be more explicit… I have an audio distribution amp that I use a rest_command to send on/off commands to. When I send an “on” command to an audio zone, the amp replies with a JSON response with information about that zone - including the current volume. I want to use that JSON response to update a volume slider.

I tried using a rest_sensor but they only update periodically (30s by default). So the issue then is, if I turn a zone on, it can be as much as 30s before the volume slider updates to reflect the current volume. That makes for a bad user experience. It’s like turning on your tv by the remote and then it randomly turns on sometime in the next 30s.

There appears to be no way to tell the rest_sensor to “update now” (which would solve the issue) or to be able to get the return JSON from a rest_command (which would also solve the issue).

Ah. Now I get it. Yes, the 30s delay would make it virtually useless. You mention that you send a rest_command to the device. Could you send the command and then follow up with a request to update the sensor? Perhaps a script or a shell command?

@ha_steve, I’m still pretty new to HA, so I might be missing something obvious. But from what I’ve experienced so far, if I wanted to do something like this I would probably write a custom component. It could register a service that would send the appropriate “REST” command and capture its output. Then it could use that output to update the state of an entity (such as the volume slider.) You could have it take input regarding what & how to turn something on or off, and what entity or entities to update with the response information. Before suggesting any concrete code, though, I’d need to know some more details.

As far as I can tell, there’s no way to force a sensor to update… and from what I gather, this is by design

@pnbruckner, this is likely the correct answer - I just need to get the time to write the custom component…

You can use an API call to set the state of a sensor:

@treno a few posts up I indicated what I’m trying to do. Setting the sensor to a value manually is not it. I need to tell the sensor to go out and make its REST call to get the value from the API so that I can take action on the value returned from REST API call.

Let me put the parts together for you…

  platform: command_line
      command_on: "python3 ~/.homeassistant/scripts/"

Then in the file

  url= 'http://whatever my restful url is to do the work'
  r = requests.get(url)
  result = r.json()[whatever the JSON root is]
  do the python here to extract your return value from the JSON response
  url= 'http://yourip/api/states/TheEntityYouWantToControl'
  r=requests.put(url)  (along with the state data)

So let me ask a couple of questions.

First, are you trying to cause a slider (aka input_number) to effectively be a remote version of an amp’s volume? I.e., if you change the slider you want to change that amp’s volume, and if that amp’s volume changes you want to update the slider accordingly?

Assuming that’s what you ultimately want to do, how responsive do you want the slider to be to changes at the amp itself? I.e., if someone changes the volume at the amp, how quickly do you want/need that to update the HA slider?

I guess where I’m going with this is, you may need to significantly reduce the scan interval anyway. And if you do, then you may not need to go through the effort of trying to change the slider (or sensor or whatever) with the output from when you turn the amp on.

@treno - thanks I’ll give that a shot this weekend. The second part will be updating an input_number slider in HA, but it should work. I’ll report back

That’s what I did at first, the problem is the when the amp is off, the system still keeps trying to update the sensor and it of course fails. @treno suggested the command_line_command, which I’ll give a go asap…

@treno - thanks for the pointers! I was able to implement exactly what I wanted this way. I didn’t use a switch, however… I used a shell_command, but with the same concept.

1 Like

@ha_steve do you mind sharing your solution?