Fast response Light switch without overloading the network

Hey there!
I’m new to ESPhome so I wanted to practice and learn how to develop my own sensors.
I made a light switch with an LDR and a relay. I want to read the analog input fairly fast so the relay reacts fast as well.
It all works with the code I have below, but I don’t like the idea of overloading the network with useless MQTT messages when my analog read remains on 0.2 for 8 hours, for example.

Is there a way I can send updates to HA based on a specific value threshold while having the internal reading working as fast as possible?

Here is the yaml I have:

sensor:
  - platform: adc
    pin: A0
    name: "Brightness"
    update_interval: 0.2s
    on_value_range:
      - above: 0.5
        then:
          - switch.turn_on: relay_1
      - below: 0.5
        then:
          - switch.turn_off: relay_1

switch:
  - platform: gpio
    id: relay_1
    name: "Relay"
    pin: D5

Thanks!

You could add internal to your sensor, see https://esphome.io/components/sensor/index.html

Thanks for the tip!
I didn’t know about that feature!
It says that the component will not be exposed to the front end, but would it still send data over the network?

I believe not.

Interesting. I just tried it out and it definitely keeps sending log data through the serial port, but I don’t know how to check the network.

I also don’t see any change on the .cpp file it creates as it keeps adding the line App.register_component(adc_adcsensor); but that might not have anything to do.

Any idea how to check the network?

Look at your mqtt server and see what messages are coming in.

oh man… fighting with that right now… I can’t find the right mosquitto_sub arguments to make it work…
Sorry, I’m pretty new to MQTT as well.

shouldn’t this let me see all the topics?
mosquitto_sub -h 192.168.1.43 -p 6053 -t '#'

Is 192.168.1.43 your mqtt broker? I suspect not.

The host (-h) requires the host the broker is on, and -p is the port, usually 1883.

port 6053 has nothing to do with mqtt, that’s used by the esphome api, which tells me you might be confused as to whether you are using mqtt or the api.

You got it absolutely right.
Now I’m not even sure if I’m using MQTT or not.
I just plugged the ESP via serial port, wrote the above yaml and it worked just fine… but of course I have no idea now if I’m even using MQTT or not!

Sorry, I may be going through the documentation a bit too fast.

Yoy must have more than that. If it is connecting to ha, it will either mqtt or api in the config.

yeah, here is the full yaml:

esphome:
  name: light_switch
  platform: ESP8266
  board: nodemcuv2
  includes:

wifi:
  ssid: "XXXXX"
  password: "XXXXXXX"

  # Enable fallback hotspot (captive portal) in case wifi connection fails
  ap:
    ssid: "Light Switch Fallback Hotspot"
    password: "l9htsCuFY5ut"

captive_portal:

# Enable logging
logger:

# Enable Home Assistant API
api:

ota:

sensor:
  - platform: adc
    pin: A0
    name: "Brightness"
    update_interval: 1s
    internal: true
    on_value_range:
      - above: 0.5
        then:
          - switch.turn_on: relay_1
          
      - below: 0.5
        then:
          - switch.turn_off: relay_1
          
switch:
  - platform: gpio
    id: relay_1
    name: "Relay"
    pin: D5

You are using the api, not mqtt. They are alternatives.

gotcha!
Thanks for that clarification!!!

Well, I tried internal: true and at least it doesn’t hit the DB, but I would still like to publish the value from time to time, so I guess that might have to be a combo of template+lambda, so a lot more to learn now.

Does that sound like in the right direction for you?

I think a template sensor (in esphome) that is simply equal to the adc sensor (should be an easy lambda!) and has an update_interval set to something more reasonable. At least that’s where I would start :slight_smile:

God damnit! now I have a different problem getting the value of the ADC.

I wrote this yaml that should solve the problem using your trick, @nickrout.


sensor:
  - platform: adc
    pin: A0
    id: ldr
    update_interval: 0.1s
    internal: true
    on_value_range:
      - above: 0.5
        then:
          - switch.turn_on: relay_1
          
      - below: 0.5
        then:
          - switch.turn_off: relay_1
          
  - platform: template
    name: "Brightness"
    lambda: |-
      return id(ldr).state;
    update_interval: 5s

But it seems like it doesn’t like my lambda

src/main.cpp: In lambda function:
src/main.cpp:201:19: error: request for member 'state' in 'ldr', which is of pointer type 'esphome::adc::ADCSensor*' (maybe you meant to use '->' ?)
       int x = ldr.state;
                   ^

Any idea why I can’t get the status of my adc sensor?

I have no idea, I develop by copying other people’s code.

Fair enough! I’m doing almost the same so far :slight_smile:
I’m gonna open a different thread to ask that question because I can’t find the answer anywhere, but I’ll be back here once I compile everything and solve this.

Thanks, @nickrout

1 Like

Never mind, I just fixed it myself.
I simply had too much code I didn’t need because I was using the full example from the documentation.

In case this is useful for anyone (most probably future me once I forget), here is the final code to make a light switch that controls a relay in a 50ms loop, and updates its brightness only every 30s to not overload the network and DB.

esphome:
  name: light_switch
  platform: ESP8266
  board: nodemcuv2
  includes:

wifi:
  ssid: "WIFI_SSID"
  password: "WIFI_PASS"

  # Enable fallback hotspot (captive portal) in case wifi connection fails
  ap:
    ssid: "Light Switch Fallback Hotspot"
    password: "WHATEVER_PASS_YOU_LIKE"

captive_portal:

# Enable logging
logger:

# Enable Home Assistant API
api:

ota:

sensor:
  - platform: adc
    pin: A0
    id: ldr
    update_interval: 0.05s
    internal: true
    on_value_range:
      - above: 0.5
        then:
          - switch.turn_on: relay_1
          
      - below: 0.5
        then:
          - switch.turn_off: relay_1
          
  - platform: template
    name: "Brightness"
    unit_of_measurement: 'V'
    lambda: |-
      return id(ldr).state;
    update_interval: 30s

switch:
  - platform: gpio
    id: relay_1
    name: "Relay"
    pin: D5

Thanks again for your help, @nickrout!!!

1 Like

It is pleasing when something works!

1 Like

Absolutely!!!
But now I feel the need to learn more about templates and lambdas… it still feels like magic to me for now.