Parse multiple values in a json retreived only once

Hi everybody,

I’ve set up a relay board in HA using the command line platform.

Here is a sample of my config file wich works fine :

- platform: command_line
    switches:
    pool_pump:
      command_on: "/usr/bin/curl -X GET http://192.168.0.103/leds.cgi?led=0"
      command_off: "/usr/bin/curl -X GET http://192.168.0.103/leds.cgi?led=0"
      command_state: "/usr/bin/curl -X GET http:/192.168.0.103/api/xdevices.json?cmd=20"
      value_template: '{{value_json.OUT1 == 1}}'
      friendly_name: Pool Pump
    pool_robot:
      command_on: "/usr/bin/curl -X GET http://192.168.0.103/leds.cgi?led=1"
      command_off: "/usr/bin/curl -X GET http://192.168.0.103/leds.cgi?led=1"
      command_state: "/usr/bin/curl -X GET http://192.168.0.103/api/xdevices.json?cmd=20"
      value_template: '{{value_json.OUT2 == 1}}'
      friendly_name: Pool Robot 
    ...

(there is no ON and OFF command, only a switch command)

Regarding the state, I want to get the json file just once and parse it for each output. Currently, the same json is retreieved 16 times for 16 relays : my relay board doesn’t seems to like it very much.

Do you know a way to acheive this ?

Ouch, this seems rough. Maybe create a sensor that stores all the information in one json object.

Then create 16 template switches that use those sensors as the value_template?

could be something like this:

sensor:
  - platform: command_line
    name: "dummy"
    command: "/usr/bin/curl -X GET http://192.168.0.103/api/xdevices.json?cmd=20"

then where your current config is

- platform: command_line
    switches:
    pool_pump:
      command_on: "/usr/bin/curl -X GET http://192.168.0.103/leds.cgi?led=0"
      command_off: "/usr/bin/curl -X GET http://192.168.0.103/leds.cgi?led=0"
      value_template: '{{ states.sensor.dummy.attributes.OUT1 == 1}}'
      friendly_name: Pool Pump

No clue if that will work. Worth a shot?

I don’t see how the sensor value will change when the switch is updated.

Not sure, I was offering a solution that may work. I haven’t tried it as I don’t have the hardware. My assumption with this method is that the senor updates when changes occur, like all sensors do. And that the value_template for the switch is pulling the information out of the sensor to display the state. So instead of 16 switches sending “/usr/bin/curl -X GET http://192.168.0.103/api/xdevices.json?cmd=20” to get the state, you have 1 sensor sending it, and 16 switches accessing the sensor. Worse case scenario is that the method doesn’t work and it’s back to the drawing board.

Yes, but that will be when the command for the sensor runs, which will not be in sync with when the switch command is sent.

So the switch will return to its previous state after it is changed on the UI (because it read the state from the sensor which hasn’t changed), and will be set correctly the next time the sensor command runs. This isn’t exactly user friendly, but I suppose might be acceptable.

Exactly. Sometimes this is a necessary evil. I have to use a similar method with the harmony remote because you can easily overload the hub so that you get upwards of 30 seconds of lag from the device. Having a few second delay on the interface is much better than standing in front of your device waiting for it to react.

hello,

Thank you for your answer.
I’ve tried your suggestion and unfortunately, it doesn’t work as the relay board returns a full json file, wich is too long : “State max length is 255 characters.”
I’m wondering if it’s possible to use script for that purpose ?

Yes, you can use whatever method you need. A python script would work well because you can store the value as a dictionary which has the exact same shape as a JSON response.