Parse http_request.get

Hello.

I am not using Home Assistant yet. This is my first device, and for now I am skiping HA.

I recently replaced one of my home lights with a Nanoleaf panels set.
I am trying to program, using ESPHome, a Sonoff T0EU2C to control nanoleaf panels.
Button 1 would be toggle on/off and button 2 next light scene.

I already managed to turn on and of the light through http_request.send.
Unfortunatly, nanoleaf API does not have a toggle function.
I would like to:

  • send a get request to get the current on off state
  • store the response in a global variable
  • send the PUT request toggling state

My current problem is: How do I parse the GET response ({value: on/off})?
Can I do it in the YAML file?

globals:
   - id: leaf_state
     type: int
	 
binary_sensor:
  - platform: gpio
    pin:
      number: GPIO0
      mode: INPUT_PULLUP
      inverted: True
    id: button_1    on_press:
      then:
        - http_request.get:
			url: http://192.168.1.74:16021/api/v1/myAuthorizationToken/state/on
			... parse the result and place it on global leaf_state
        - http_request.send:
            method: PUT
            url: http://192.168.1.74:16021/api/v1/myAuthorizationToken/state
            headers:
              Content-Type: application/json
            json: |- 
              root.createNestedObject("on");
              root["on"]["value"] = !leaf_state;
            verify_ssl: false

Thanks

Couldn’t you just set leaf_state when you toggled it?

The problem is that will get out of sync if something else is also controlling the nanolight.

1 Like

as at november 2019, this cannot receive a response https://github.com/esphome/esphome/pull/719#issuecomment-552264290

There may be future plans though https://github.com/esphome/esphome/pull/719#pullrequestreview-304042620

Thanks for the inputs @KTibow and @nickrout.
@KTibow, the problem with that is the switch getting out of sync if I use the phone or the buttons on the nanoleaf controler.
Thats why I want to question (GET) the device prior to toggling. To resync beforehand.

Yes, I was aware of that, but I didn’t bother including it.

I realise you weren’t wanting to install home assistant yet, but this may be a good reason as home assistant supports the nanoleaf.

Yes it does. I even tried it once.
But having a server running (even in a pi) to control a single light and a wall switch is a bit overkill.
I will go with HA when I have some more devices to manage.

I managed to make it work.
I dismissed the visual track for on / off so that I had no synchronization problems. It also prevents needless relay activation.
How I dit it:

globals:
  - id: nanoState
    type: bool

binary_sensor:
  - platform: gpio
    pin:
      number: GPIO0
      mode: INPUT_PULLUP
      inverted: True
    id: button_1
    on_press:
      then:
        - lambda: |-
            HTTPClient http;
            http.begin("http://192.168.1.74:16021/api/v1/myauthtoken/state/on");
            http.GET();
            id(nanoState) = http.getString()[9] == 't';
        - http_request.send:
            method: PUT
            url: http://192.168.1.74:16021/api/v1/myauthtoken/state
            headers:
              Content-Type: application/json
            json: |- 
              root.createNestedObject("on");
              root["on"]["value"] = !id(nanoState);
            verify_ssl: false

I’ll be working on changing color scenes next.

I was going to suggest a lamda. good work.