Optical Sensor
Binary sensor
An optical water level sensor detects liquid levels using infrared light. It consists of an infrared LED and a photodetector housed in a transparent probe. When the probe is in the air, the infrared light is internally reflected within the sensor. When submerged in liquid, the light refracts into the liquid, reducing the reflected signal. The sensor detects this change and outputs a signal indicating the presence or absence of liquid. Optical sensors are reliable, have no moving parts, and work well in various liquids, making them ideal for precise level detection in tanks and pipelines.
Hardware conection
How its connected
The optical water level sensor has a small conection board PCB, this board needs ground, Power and Signal conections to the Sensor module. they are labeled on the Sensor module PCB respectivly.
In my build I used an Optical sensor where the power input was set with a switch, either 5v or 3v. The Sensor module outputs 5v on the optical port so set the switch to 5v.
Software setup
Checking the Code

In your dashboard you can create a card showing the status of the optical sensor.
​You can edit the card to show a different icon as shown here and also give a custom name.
​
Using the data provided from the sensor you can create automations as you see fit.
If you installed the code correctly when doing the setup you shouldn't have much to do here. HA should recognise the sensor and show it on the dashboard where you can see its output, If not you my need to add a card.
I recomend going into the code and changing the name of each sensor so you know what is linked to what. here is how to do that
-
ensure everything is on and running correctly
-
Go to ESP home and click on edit to open the code.

The code for the Optical sensor is located here and looks as shown here.
There are six elements:
-
Platform: set to Gpio as the optical sensor use the gpio pins on the esp32.
-
Pin: The number of the Pin that this switch is conected too.
-
Inverted: Changes the working state On to Off in this case
-
Mode Input: defines this pin as an input
-
Mode Pullup: enables the internal resistor to reduce false triggers
-
Name: Here you can set the name to what ever you would like, in this example I have it set to have the name "optical"
​
You can change the name here to whatever you like, you can also chance it in the dashboard too.
Photos...

Example of a optical sensor

Optical Sensor PCB

Optical sensor port next to float switch ports

Marine assistant ATO with Optical sensor.
Links
Here are a few links to where you can purchase your sensors, click the link depending on your location.
​
Worldwide link generally takes longer to be delivered but is a little cheaper. Better if you want to bulk order.
​
​
Me Personnaly I order most of my stuff from Aliexpress, it normally gets here fairly quickly and tends to be a little cheaper.
If I need something quick I order from amazon.
Full disclosure: some of these links are affiliate links, clicking them and purchasing though my links helps me support the project.