I have an analog sensor that I need to read periodically, but to do so I fist need to set a digital pin on HIGH and set it on LOW after the reading(I don’t want to leave it always on).
I’m getting quite confused on how to make it happen on esphome, any help?
There may be a better way to do it but I think you can do it via scripting, filters, and a global variable. This assumes that your expected voltage is within a predefined range and that when the digital pin is off the ADC will read outside that range. This should be close but I didn’t test so you may have to play with it:
globals:
- id: adc_value
type: float
restore_value: no
initial_value: '0'
sensor:
- platform: adc
pin: A0
name: "Your Analog Sensor"
id: your_analog_sensor
internal: true
update_interval: 1s
on_value_range:
above: <low value>
below: <high value>
then:
- globals.set:
id: adc_value
value: !lambda "return id(your_analog_sensor);"
output:
- platform: gpio
pin: D1 #make this your pin number
id: your_digital_pin
script:
- id: get_sensor_value
then:
- output.turn_on: your_digital_pin
- delay: 2s # or however long you need (needs to be longer than update_interval above)
- output.turn_off: your_digital_pin
Thank you but I don’t think this will do for me.
Here is a better explanation of what I’m trying to achive:
I have an analog sensor and I want to get on home-assistant its value every hour. The problem is that I don’t want to let this sensor always connected to the power, for reasons, so I put the VIN of the sensor on a digital pin. What I’m trying to achieve is to power on the digital pin before the reading of the analog sensor and power off after.