Combine a switch and a sensor

Platform: Nodemcu
I have a moisture sensor that is powered in 3V by one of the NodeMCU ports, and connected to the ADC.

I would like to have a periodic task, lets say ever 10 minutes, that switch the port On, perform the ADC read, and swithch off the port.

I can set up an automation in Home assistant to perform swithch the port on, but I don’t see any function to perform the read of the ADC.

¿could someone help me?

The ADC platform of esphome supports update_interval which is set at default to 60s. I believe your recruirement will be fullflied by changing that to 10min or so. Did you try this?

No, I did not, because It must be synchronized with the switch to power the sensor.
The problem is that ADC does not allow a not periodic redout.
eHc

Set the ADC update interval to a long time and use the update component action when your power is turned on:

Maybe…

The update component action only works on some components and I’m not sure if the ADC is one of them. Try it and see if it compiles.

Thanks a lot, I will check and come back with the result.
eHc

It works; this is the configuratio I’ve implemented:
‘’’
switch:

  • platform: gpio
    pin: GPIO14
    name: “VDD_s_humedad”
    on_turn_on:
    then:
    • component.update: adc_0

sensor:

  • platform: adc
    pin: A0
    name: “Humedad del suelo”
    update_interval: 600s
    accuracy_decimals: 0
    id: adc_0
    filters:
    • lambda: return 100 - x/10.24;
      ‘’’
      This is the log
      [10:36:30][D][switch:021]: ‘VDD_s_humedad’ Turning ON.
      [10:36:30][D][switch:045]: ‘VDD_s_humedad’: Sending state ON
      [10:36:30][D][adc:056]: ‘Humedad del suelo’: Got voltage=0.05V
      [10:36:30][D][sensor:092]: ‘Humedad del suelo’: Sending state 99.99533 V with 0 decimals of accuracy
      [10:36:35][D][switch:025]: ‘VDD_s_humedad’ Turning OFF.
      [10:36:35][D][switch:045]: ‘VDD_s_humedad’: Sending state OFF

Thanks a lot
eHc

1 Like

Great but please learn to format your posts correctly. See point 11 here: How to help us help you - or How to ask a good question

Just to close the issue a couple of things.
I had to put a delay before updating ADC, as the values recorded were incorrect. The final configuration is:

switch:
  - platform: gpio
    pin: GPIO14
    name: "VDD_s_humedad"
    on_turn_on:
      then:
      - delay: 2s
      - component.update: adc_0

sensor:
  - platform: adc
    pin: A0
    name: "Humedad del suelo"
    update_interval: 6h
    accuracy_decimals: 0
    id: adc_0
    filters:
      - lambda: return 100.0 - x*100.0;

and the log:
[17:06:30][D][switch:021]: ‘VDD_s_humedad’ Turning ON.
[17:06:30][D][switch:045]: ‘VDD_s_humedad’: Sending state ON
[17:06:32][D][adc:056]: ‘Humedad del suelo’: Got voltage=0.35V
[17:06:32][D][sensor:092]: ‘Humedad del suelo’: Sending state 65.42969 V with 0 decimals of accuracy
[17:06:35][D][switch:025]: ‘VDD_s_humedad’ Turning OFF.
[17:06:35][D][switch:045]: ‘VDD_s_humedad’: Sending state OFF

What I would like to do is to filter out the values obtained with the sensor not powered, wich are always in the range of 0,01V (90 after the conversion)
I’ve tried “on_value_range” and several lambdas formulas, but I always get compilation errors.
eHc

2 Likes