Make 2 ESPHome control each other - Read json entries from web http_request.get

Hello Guys,

I am trying to make 2 esphome devices “communicate”. There is one toggle light (sonoff mini set up as toggle light) which has the following json payload when perfoming the http_request.get:
{"id":"light-licht_kche","state":"OFF"}
I want to read the sate with another esphome device and when this other device is activated it shall

- http_request.post: "http://192.168.1.32/light/licht_kuche/toggle"
- delay: 120s

the first one if it is off, else it should not toggle.
what i have so far for the second device is:

substitutions:  
  node_name: rf_bridge1 # only lower case and connected _
  friendly_name: "Sonoff RF Bridge 1"
  ip_address_replace: "192.168.1.28"

esphome:
   name: $node_name
   platform: ESP8266
   board: esp01_1m

<<: !include .template.basics.yaml

###########################################################
################ from here DEVICE specific ################
###########################################################
remote_receiver:
   pin:
     number: 4
   dump: rc_switch
   filter: 50us # 50us # 5us - 250us works so far
   idle: 2ms # 1-9ms work so far
   tolerance: 50% #default = 25%

globals:
  - id: kitchen_state
    type: bool

binary_sensor:
  - platform: remote_receiver
    name: "Bewegungsmelder Küche"
    device_class: motion
    filters:
      - delayed_off: 100ms ### controll time via automation rather than in flash memory
    rc_switch_raw:
      code: "000101000011011100000110"
      protocol: 1
    on_press:
     then: # from here I just tried to transfer ideas from other solutions ... not working....
        - lambda: |-
            HTTPClient http;
            http.begin("http://192.168.1.32/light/licht_kche/");
            http.GET();
            id(kitchen_state) = http.getString()[36] == 'n';
            http.end();
        - if:
            condition:
              lambda: '!id(kitchen_state);'
            then:
              - http_request.post: "http://192.168.1.32/light/licht_kche/toggle"
              - delay: 120s

I assume that some c++ in lambda could help figure the state but I have no clue about c++

I know i could just use home assistant automation, but using the http_request I cuold have these devices running even without home assistant.

Also here is something similar, which i cannot transfer:

TIA

If you want to toggle only when it is off, you can simply turn it on without prior checking, using the turn_on action instead of toggle.

Thats what I tried, but since the sonoff mini (on recieving end) has only toggle set up, there is no turn_on to refer to.
It is set up like this:


output: #define an output here, so that the "technical" relay is not explicitly shown in HA; instead below the "light" is shown
  - platform: gpio
    id: relay_1
    pin: GPIO12

light:
  - platform: binary
    id: light_1
    name: "Licht $room"
    output: relay_1    #light is beeing controlled by the gpio using the above output as code-internal relay

binary_sensor:
  - platform: gpio
    pin: GPIO00
    id: reset
    internal: true
    filters:
      - invert:
      - delayed_off: 25ms
    on_press:
      - light.toggle: 
          id: light_1

  - platform: gpio
    name: "Schalter Licht $room"
    pin: GPIO04
    id: switch_1
    on_press:
      then:
#        - switch.turn_on:
        - light.toggle: 
            id: light_1
    on_release:
      then:
#        - switch.turn_off:
        - light.toggle: 
            id: light_1

I don’t have the hardware to test it, but check this topic.
The user here seems to be trying to use toggle instead of turn_on/turn_off.

So turn_on should work.

Hello. I also have two esphome devices communicating and aplying logic completely without home assistant. For one device I publish values with web_server: component. It publishes json state. I then read it with other esphome device with this lambda:
in esphome: component:

  includes: /home/janbenes/.platformio/packages/framework-arduinoespressif32/libraries/ESPmDNS/src/ESPmDNS.h
      id(reading_http)=true;  //avoid conflict if any other component is just using http
      HTTPClient http2;
      IPAddress serverIp = MDNS.queryHost("blinds-cs1");  //this section looks up for IP address
      int i = 5;
      while ((serverIp.toString() == "0.0.0.0") && (i > 0)) {
        delay(250);
        i--;
        serverIp = MDNS.queryHost("blinds-cs1");   //##################### Change server for color
        }
      if (i > 0) {
        http2.useHTTP10(true);
        while (WiFi.status() != WL_CONNECTED) {
          delay(1000);
          }
        http2.begin("http://"+serverIp.toString()+"/sensor/color_nr");
        if (http2.GET() == 200) {
          id(cs1_available) = true;
          DynamicJsonBuffer doc(200);
          JsonObject& root = doc.parseObject(http2.getStream());
          int color = atoi(root["value"].as<char*>());
          http2.end();
          id(reading_http)=false;
          return color;
          }
        else {
          id(cs1_available) = false;
          ESP_LOGD("main", "################### CS1 not available!");
          http2.end();
          id(reading_http)=false;
          return -1;
          }
        }
      else {
        id(cs1_available) = false;
        ESP_LOGD("main", "################### CS1 not available, mDNS error!");
        http2.end();
        id(reading_http)=false;
        return -1;
      }

I, i’m not an expertin programming… Can you better explain how to use the code you posted? Thank you