# Map proximity to servo

I have set up a servo in esphome, according to the docs, to hopefully display the distance my wife is from home, using proximity. The servo has an annoying range where the movement of the servo is mapped (0= 60) and (180 = -100), so kind of backwards to what’s logical.

What I need is some type of equation that converts the distance from proximity, where 0 metres is minimum and 2000 metres is maximum and convert those values to 60 and -100.

So:
0 metres = 60%
2000 metres = -100%

Is there any way to make an automation template that makes this conversion? The answers I have found where way beyond me.

Here’s my automation:

``````- alias: Push Wife Distance to Display
trigger:
- platform: state
entity_id:
- proximity.wife_home
action:
- service: esphome.distance_display_control_distance_servo
data_template:
level: '{{ trigger.to_state.state | int}}'
``````

Here’s my espHome config:

``````esphome:
name: distance_display
platform: ESP8266
board: d1_mini
on_boot:
priority: 800
then:
- servo.write:
id: distance_servo
level: 60%
- delay: 1s
- servo.write:
id: distance_servo
level: -100%

# Enable Home Assistant API
api:
services:
- service: control_distance_servo
variables:
level: float
then:
- servo.write:
id: distance_servo
level: !lambda 'return level / 100.0;'
ota:

output:
- platform: esp8266_pwm
id: servoPin
pin: D6
frequency: 50 Hz

servo:
- id: distance_servo
output: servoPin
min_level: 3.0%
idle_level: 7.5%
max_level: 12.0%

``````

Any ideas would be greatly appreciated.

So I’m not sure why, but this kind of works:

``````(60-states.proximity.wife_home.state | int/12)
``````

Why? It is pretty simple maths problem, a linear graph from 2 points.

The correct answer is 12.5 not 12.

Thank you for that, I stand corrected : )