Trigger switch before taking sensor reading

I’m new to ESPHome. I have a ESP8266 connected to a sensor and a relay. They are configured below:

switch:
  - platform: gpio
    name: relay_1
    inverted: True
    pin: 13
    restore_mode: RESTORE_DEFAULT_OFF
    
sensor:
  - platform: adc
    pin: A0
    name: "Water Tank Voltage"
    update_interval: 5s
    filters:
      - median:
      - multiply: 3.3

In order for the sensor to work, the relay needs to be switched ON. How can I ensure that the relay is switched on + a delay before taking a sensor reading? I’m a bit new to ESPHome so appreciate the help!

See the second attempt in this post:

1 Like

@tom_l you are a hero of this forum, thank you for the answer (and for being so active and helpful!)

Below is my final solution after integrating from the post you shared:

output:
  - platform: gpio
    pin: 13
    inverted: True
    id: water_sensor_power
sensor:
  - platform: adc
    pin: A0
    id: "_waterTankVoltage"
    internal: True
    filters:
      - median:
      - multiply: 3.3

  - platform: template
    name: "Water Tank Sensor Voltage"
    id: "waterTankVoltage"
    unit_of_measurement: "%"
    icon: "mdi:water-percent"

interval:
  - interval: 15s
    then:
      - output.turn_on: water_sensor_power
      - delay: 10s
      - component.update: _waterTankVoltage
      - sensor.template.publish:
          id: waterTankVoltage
          state: !lambda |-
            return id(_waterTankVoltage).state;      
      - delay: 200ms
      - output.turn_off: water_sensor_power

Works exactly as expected. ESPHome is magic

2 Likes

Yeah it’s a fantastic project. Makes DIY IoT devices very easy.