Simple little thing, but I love it.
And here it is again, a bit more dressed for the occasion.
*Edit: Updated code to make use of the Push Button on the Encoder as a Play / Pause toggle for a media player. Involves one extra wire (the white wire in the photo) in addition to the wiring guide below. All other instructions the same though. I am getting some false double-pushes every now and again, code might need some tweaking.
The rotary encoder was one of the pieces in an electronics kit I got from China. All fairly standard I guess.
The Pi Zero is just Raspbian Lite, with Home Assistant installed for the python api module.
I followed the wiring guide here:
https://www.modmypi.com/blog/how-to-use-a-rotary-encoder-with-the-raspberry-pi
Code:
http://www.14core.com/wiring-the-360-rotary-encoder-on-c-code-python/
Re-used the code, added some hass specific bits, and now control my stereo with it
Here are the code bits:
First an input_number.yaml entry to hold the rotary value, it won’t be for display:
rotary_encoder_value:
name: Rotary
min: 0
max: 100
step: 1
mode: box
And some sensor.yaml entries:
- platform: template
sensors:
rotary_value:
value_template: "{{ states.input_number.rotary_encoder_value.state }}"
- platform: template
sensors:
hifipi_volume:
value_template: "{{ states.media_player.hifi_pi.attributes.volume_level * 100}}"
Change the “media_player.hifi_pi” part to match your player, and in the bit below.
The automation.yaml entry to change the volume when the encoder’s turned:
- id: Adjust Volume on Rotary Encoder Move
alias: "Rotary Volume Change"
trigger:
- platform: state
entity_id: sensor.rotary_value
action:
- service: media_player.volume_set
entity_id: media_player.hifi_pi
data_template:
volume_level: "{{ states.sensor.rotary_value.state | float / 100 }}"
For the Pi Zero itself, run this script. It’s python3. Change the api IP address to match your Hass install’s, and the “switch_name” to your media_player.
#!/usr/bin/env python3
import RPi.GPIO as GPIO
import time
import homeassistant.remote as remote
api = remote.API('192.168.178.113', '')
domain = 'media_player'
switch_name = 'media_player.hifi_pi'
media_link = remote.get_state(api, 'sensor.hifipi_volume')
if media_link.state == "unknown": media_link.state = "40"
counter = int(float(media_link.state))
remote.set_state(api, 'sensor.rotary_encoder_value', new_state=counter)
# print("ok ", counter)
RoAPin = 11 # pin11
RoBPin = 12 # pin12
RoSPin = 13 # pin13
globalCounter = counter
flag = 0
Last_RoB_Status = 0
Current_RoB_Status = 0
def setup():
GPIO.setmode(GPIO.BOARD) # Numbers GPIOs by physical location
GPIO.setup(RoAPin, GPIO.IN) # input mode
GPIO.setup(RoBPin, GPIO.IN)
GPIO.setup(RoSPin,GPIO.IN,pull_up_down=GPIO.PUD_UP)
rotaryButton()
def rotary_counter_return_value(ro_count):
remote.set_state(api, 'input_number.rotary_encoder_value', new_state=ro_count)
return()
def rotaryButton():
GPIO.add_event_detect(RoSPin, GPIO.FALLING, callback=button_press) # wait for falling
def button_press(ev=None):
remote.call_service(api, domain, 'toggle', {'entity_id': '{}'.format(switch_name)}, timeout=7)
time.sleep(1)
def rotaryDeal():
global flag
global Last_RoB_Status
global Current_RoB_Status
global globalCounter
Last_RoB_Status = GPIO.input(RoBPin)
while(not GPIO.input(RoAPin)):
Current_RoB_Status = GPIO.input(RoBPin)
flag = 1
if flag == 1:
flag = 0
if (Last_RoB_Status == 0) and (Current_RoB_Status == 1):
globalCounter = globalCounter + 2
rotary_counter_return_value(globalCounter)
if (Last_RoB_Status == 1) and (Current_RoB_Status == 0):
globalCounter = globalCounter - 2
rotary_counter_return_value(globalCounter)
def loop():
global globalCounter
while True:
rotaryDeal()
def destroy():
GPIO.cleanup() # Release resource
if __name__ == '__main__': # Program start from here
setup()
try:
loop()
except KeyboardInterrupt: # When 'Ctrl+C' is pressed, the child program destroy() will be executed.
destroy()
A group.yaml
Rotary:
view: no
entities:
- sensor.rotary_value
and customize.yaml for convenience:
sensor.rotary_value:
friendly_name: Rotary Encoder Value
icon: mdi:debug-step-over
Hope I haven’t left anything out.
Could be used for light dimming and other like things I suppose.