├── LICENSE └── README.md /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2024 3ATIVE 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Using Servos in Home Assistant with ESPHome 2 | 3 | Using Servos from ESPHome is quite simple. Check out the 🎬[YouTube tutorial](https://youtu.be/1f2tuS_PxIE) for setting up and options available. 4 | ___ 5 | ### The Basic Setup: 6 | 7 | #### 🛠 The `servo:` Component: 8 | ```yaml 9 | servo: 10 | - id: servo1 11 | output: output1 12 | ``` 13 | 14 | #### ❓ Optional Servo settings and calibration: 15 | ```yaml 16 | auto_detach_time: 5s # Optional - Default 0 (Always On) 17 | transition_length: 0s # Optional - Default 0 (Max Speed) 18 | min_level: 2.7% # Optional - Default 3% 19 | idle_level: 7.3% # Optional - Default 7.5% 20 | max_level: 12.2% # Optional - Default 12.5% 21 | ``` 22 | 23 | #### 🛠 The `output:` Component: 24 | ```yaml 25 | output: 26 | - platform: ledc # For ESP8266 use: esp8266_pwm 27 | id: output1 28 | pin: [YOUR GPIO] 29 | frequency: 50 Hz # MUST BE 50Hz 30 | ``` 31 | 32 | #### 🛠 The `number:` (Slider) Component: 33 | ```yaml 34 | number: 35 | - platform: template 36 | id: servoSlider1 37 | name: $name 1 38 | icon: mdi:sync 39 | optimistic: true 40 | min_value: -100 41 | max_value: 100 42 | initial_value: 0 43 | step: 5 44 | on_value: 45 | then: 46 | - lambda: !lambda |- 47 | // Servo Write range is: -1.0 to 1.0, so divide by 100 48 | id(servo1).write(x / 100.0); 49 | ``` 50 | ___ 51 | ### Advanced Code-y Bits 52 | ##### 🎁 ADVANCED Part 1: Use `on_boot:` to set position at Start-up: 53 | ```yaml 54 | esphome: 55 | ... 56 | on_boot: 57 | priority: -100 58 | then: 59 | - lambda: !lambda |- 60 | id(servoSlider1).make_call().set_value(0).perform(); 61 | ``` 62 | 63 | ##### 🎁 ADVANCED Part 2-1: SINE Waving Servo 5: 64 | - Use ``interval:`` to Calculate Sine Values: 65 | ```yaml 66 | globals: 67 | - id: sine_wave_phase 68 | type: float 69 | restore_value: no 70 | initial_value: '0.0' 71 | 72 | - id: sine_wave_range 73 | type: float 74 | restore_value: no 75 | initial_value: '0.0' 76 | 77 | - id: sine_wave_speed 78 | type: float 79 | restore_value: no 80 | initial_value: '1.0' 81 | 82 | interval: 83 | - interval: 40ms 84 | then: 85 | if: 86 | condition: 87 | switch.is_on: sinEnable 88 | then: 89 | - lambda: |- 90 | auto t = id(sine_wave_phase); 91 | auto range = id(sine_wave_range); 92 | auto speed = id(sine_wave_speed); 93 | 94 | // Increment phase 95 | t += (2 * PI) / speed; 96 | if (t >= 2 * PI) t = 0; // Reset the phase after a full cycle 97 | 98 | id(sine_wave_phase) = t; 99 | int value = round(range * sin(t)); 100 | 101 | id(servoSlider5).publish_state(value); 102 | 103 | switch: 104 | - platform: template 105 | id: sinEnable 106 | name: $name 5 Enable Sine 107 | optimistic: true 108 | ``` 109 | 110 | ##### 🎁 ADVANCED Part 2-2: SINE Waving Servo 5: 111 | - The Two Extra `number:` Components to control the SINE Values : 112 | ```yaml 113 | - platform: template 114 | id: servoRange 115 | name: $name 5 Range 116 | min_value: 0 117 | max_value: 100 118 | initial_value: 0 119 | step: 5 120 | optimistic: True 121 | on_value: 122 | then: 123 | - lambda: !lambda |- 124 | id(sine_wave_range) = x; 125 | 126 | - platform: template 127 | id: servoSpeed 128 | name: $name 5 Speed 129 | min_value: 0 130 | max_value: 100 131 | initial_value: 5 132 | step: 1 133 | optimistic: True 134 | on_value: 135 | then: 136 | - lambda: !lambda |- 137 | id(sine_wave_speed) = 5 + (100-x); 138 | ``` 139 | ##### 🎁 ADVANCED Part 2-3: SINE Waving Servo 5: 140 | - Stop Spamming the ``logger:``: 141 | ```yaml 142 | logger: 143 | logs: 144 | number: none 145 | servo: none 146 | ``` 147 | --- 148 | ### 🤝 Found this useful, want to say 'Thanks' and support my efforts. CHEERS🍺 149 | | Buy me a Coffee | PATREON | 150 | |-----------------|---------| 151 | | [![Buy Me A Coffee](https://img.shields.io/badge/Buy%20Me%20A%20Coffee-donate-yellow.svg?style=flat-square&logo=buy-me-a-coffee)](https://www.buymeacoffee.com/3ative) | [![Patreon](https://img.shields.io/badge/Patreon-support-red.svg?style=flat-square&logo=patreon)](https://www.patreon.com/3ative) | 152 | --- 153 | --------------------------------------------------------------------------------