Hi. I am struggling to figure out how to do following: turn on digital pin, wait 1ms, read analog sensor, turn off digital pin. Repeat every 5min. So far i have managed to get digital pin to trigger on/off, cannot read analog pin while it is powered. Thank you for any suggestions.
This is my best bet so far, which doesn’t work:
output:
- platform: gpio
pin: D5
id: power_pin
- platform: gpio
pin: D7
id: gnd_pin
switch:
- platform: output
name: "pos"
id: pos
output: power_pin
on_turn_on:
then:
- delay: 1000ms
- lambda: |-
id(analog_sensor).state ;
- switch.turn_off: pos
sensor:
- platform: adc
pin: A0
name: "Analog Sensor"
id: analog_sensor
update_interval: 5s
It gives this statement on compiling:
/config/esphome/rain-sens.yaml: In lambda function:
/config/esphome/rain-sens.yaml:52:22: warning: statement has no effect [-Wunused-value]
52 | id(analog_sensor).state ;
Your lambda
call isn’t triggering a read of the analogue sensor — it’s simply referencing its value and then doing nothing with it, hence the warning message.
I think you want to use the .publish_state()
call (here) in combination with a much higher update_interval
on the sensor — perhaps with a filter on it to get rid of any reading that are taken whilst the thing powered by D7
is off.
Then i’m getting following error:
/config/esphome/rain-sens.yaml: In lambda function:
/config/esphome/rain-sens.yaml:52:22: error: invalid use of non-static member function 'void esphome::sensor::Sensor::publish_state(float)'
From what code?
output:
- platform: gpio
pin: D5
id: power_pin
- platform: gpio
pin: D7
id: gnd_pin
switch:
- platform: output
name: "pos"
id: pos
output: power_pin
on_turn_on:
then:
- delay: 1000ms
- lambda: |-
id(analog_sensor).publish_state ;
- switch.turn_off: pos
sensor:
- platform: adc
pin: A0
name: "Analog Sensor"
id: analog_sensor
update_interval: 5s
It’d need to be publish_state()
as it’s a function.
Try this instead, using component.update
documented here:
on_turn_on:
then:
- delay: 1000ms
- component.update: analog_sensor
- switch.turn_off: pos
Thank you very, very much vise man! This works.