This started as a question, but after a full day of research, experiment and maths, I’ve come to a somewhat good conclusion. Now I have it, I’d love to know if anyone else has found a better way of doing this, and if so, what is it?

**Original question:** I’d like to reliably convert the number provided by my voltage sensor connected to the A0 analogue pin of my ESP8266 flashed with Tasmota, to a measured input voltage.

Result of one full day of faffing:

- Connect the Wemos to a USB supply, the Voltage Sensor to +5v, GND, and (S) A0.
- Create a spreadsheet, and add information as below, transposing the bold text with your own voltage readings, and Tasmota outputs:

Actual Voltage | Detected A0 Number | Calculated by formula | Difference (error) | Reverse Voltage REAL Test |
---|---|---|---|---|

0 | 0 |
0 | 0.00 | 0.00 |

5 | 317 |
325.6997455 | -8.70 | 5.00 |

10 | 645 |
651.3994911 | -6.40 | 10.00 |

12 | 778 |
781.6793893 | -3.68 | 12.00 |

15.72 |
1024 | 1024 | 0.00 | 15.72 |

Start by putting 0 ‘Actual V’ in, and ensuring you get a 0 in the Tasmota output screen. Then ramp the voltage up while watching the Tasmota output. Once it reaches 1024 (assumed max number for the sensor) read the voltage, and put it in the Actual voltage column at the bottom.

Now create the second table as below from column G:

Formula | ||
---|---|---|

Maximum Analog0 Wemos number | 1024 | a |

Voltage detected at that number | 15.72 |
v |

Formula (a/v) | 65.13994911 |

Reference the voltage field (=A6) in the ‘Voltage detected…’ column and add the formula =H2/H3 to divide the 1024 by the voltage.

Now put the voltage up to 5, and input the number in the ‘Detected A0…’ column in the left table, mine was 317. Do the rest of the columns, I added 5, 10, and 12 but the more the merrier (more accurate).

The calculated formula in column ‘**Calculated by formula**’ is =A3*$H$4

**Difference (error)** column should be =A3*$H$4

**Reverse Voltage REAL Test** should be =C2/$H$4

Hopefully not too confusing.

With those figures, it’s possible to add entries to ‘configuration.yaml’ with lines like the following, pasting the formula after the float value:

```
- sensor:
- name: "SolarBattery5"
unit_of_measurement: "V"
state: >
{{((states('sensor.solarbattery5_analog_a0')|float) /65.1399491) | round (2)}}
```

I found this to be imperfect, as the spreadsheet shows the lower the voltage the more error, but not by much.