Esp8266, 1 LED Howto

Hello to all,
new to esphome, because I’m in the iobroker/ Tasmota World.
With Tasmota there is no easy way to get an rotary encoder to publish via MQTT
I found ESPHome and this looks very nice.
I got an ESP8266, a rotary encoder and a LED. Yes on a Breadboard.
And Here is my yaml:

captive_portal:

web_server:
  
binary_sensor:
  - platform: gpio
    name: Button Module
    pin:
      inverted: true
      mode:
        input: true
        pullup: true
      number: GPIO12
    id: button_module

sensor:
  - platform: rotary_encoder
    name: Rotary Encoder Sensor
    id: sensor_rotary_encoder_1
    pin_a:
      number: GPIO14
      inverted: true
      mode:
        input: true
        pullup: true
    pin_b:
      number: GPIO13
      inverted: true
      mode:
        input: true
        pullup: true
    on_clockwise:
      then:
        - output.turn_on: output_esp8266_pwm_1
    on_anticlockwise:
      then:
        - output.turn_off: output_esp8266_pwm_1
output:
  - platform: esp8266_pwm
    id: output_esp8266_pwm_1
    pin:
      number: GPIO5

In Web Server there is a Button Module that reacts on button press.
Next Step is to publish the Button Press to my iobroker via MQTT.
There is also a Rotary Encoder Sensor which shows State: NA at Power On.
If i turn the rotary clockwise, the state changes to -1. After that any rotation in any direction dosn’t change anything.
My Goal is to:

  • Capture everything in iobroker
  • Create a script and via the script
  • Switch the LED on and off
  • Dim the brightness of the LED

Any Help was appreciated. And i try to expand this howto for other People.

This is likely not going to work. Gpio15 is pulled down and doesn’t have internal pullup.
Try with 14 instead.

Himmelherrgott…
OK, @Karosm you’re right. Now there are the right Values in Web API.
Only the rotation is the wrong one…

No Problem, thanks.

Direction? Swap the pins on your code.

Any reason you don’t want to do this all within ESPHome? Few more lines of config and you could dim the LED directly.

OK, here is the new yaml:

captive_portal:

web_server:
  
binary_sensor:
  - platform: gpio
    name: Button Module
    pin:
      inverted: true
      mode:
        input: true
        pullup: true
      number: GPIO12
    id: button_module
    on_press:
      - then:
          - switch.toggle: switch_gpio_5
          - logger.log: "pressed"

sensor:
  - platform: rotary_encoder
    name: Rotary Encoder Sensor
    id: sensor_rotary_encoder_1
    pin_a:
      number: GPIO13
      inverted: true
      mode:
        input: true
        pullup: true
    pin_b:
      number: GPIO14
      inverted: true
      mode:
        input: true
        pullup: true
    on_clockwise:
      then:
        - logger.log: "clockwise"
    on_anticlockwise:
        - logger.log: "anticlockwise"

switch:
  - platform: gpio
    name: GPIO Switch
    id: switch_gpio_5
    pin: GPIO5

An green LED on gpio_5 can be switched on an off.
The Value of Rotary Encoder could also be set…
which way do i have to take for dimming the Led and change the brightness of the led?

Lost in Components and automations…

Please Help

And there is the web api:


Rotary turned and button pressed.

More than acceptable as 1 week ago…

You can’t use just gpio switch for LED if you want to dim.
And if you want to dim, you can do it different ways.

Try if this works.

sensor:
  - platform: rotary_encoder
    name: Rotary Encoder Sensor
    id: sensor_rotary_encoder_1
    pin_a:
      number: GPIO13
      inverted: true
      mode:
        input: true
        pullup: true
    pin_b:
      number: GPIO14
      inverted: true
      mode:
        input: true
        pullup: true
    on_clockwise:
      then:
        - logger.log: "clockwise"
    on_anticlockwise:
        - logger.log: "anticlockwise"
    min_value: 0
    max_value: 100
    resolution: 1

    on_value:
      then:
        - output.set_level:
            id: output_esp8266_pwm_1
            level: !lambda "return x / 100.0;"

OK, Working Code:

captive_portal:

web_server:
  
binary_sensor:
  - platform: gpio
    name: Button Module
    pin:
      inverted: true
      mode:
        input: true
        pullup: true
      number: GPIO12
    id: button_module
    on_press:
      - then:
          - if:
              condition:
                binary_sensor.is_on: button_module
              then:
                - output.turn_off: output_esp8266_pwm_1
              else:
                - output.turn_on: output_esp8266_pwm_1
          - logger.log: pressed

sensor:
  - platform: rotary_encoder
    name: Rotary Encoder Sensor
    id: sensor_rotary_encoder_1
    pin_a:
      number: GPIO13
      inverted: true
      mode:
        input: true
        pullup: true
    pin_b:
      number: GPIO14
      inverted: true
      mode:
        input: true
        pullup: true
    on_clockwise:
      then:
        - logger.log: "clockwise"
    on_anticlockwise:
        - logger.log: "anticlockwise"
    min_value: 0
    max_value: 100
    resolution: 1

    on_value:
      then:
        - output.set_level:
            id: output_esp8266_pwm_1
            level: !lambda "return x / 100.0;"

output:
  - platform: esp8266_pwm
    id: output_esp8266_pwm_1
    pin:
      number: GPIO5

Dimming is ok.
Switch off via push button also.
But Switch on via push button dosn’t return to previous value.
Where do i have to store the value?

Global variable can save any value.

OK, i’m lost…
programming in BASIC or Something Else was some 40 years ago.
I set the Variable:

globals:
  - id: dimmer1
    type: int
    initial_value: "0"

Now how do i work with this variable?
Some sort of on_clockwise - then - add 1 to dimmer1 - output_set_level: dimmer1

HELP! Please

Same way more less. Every encoder on_value automation could save the value on globals.
Then on turn_on you read the value and set level. Just like with basic 40 years ago.

The code above doesn’t use on_clockwise for anything else than logging. You can delete it when done.

Here you come to point what I mentioned on post #8. There are many different ways of doing things.
If you need to retain the brightness between on/off you might benefit from using light component as higher level control of output.
If I remember well, light can retain brightness on on/off.

Before you decide your way to go, think all the aspects you need, even HA user interface.

For now encoder has been controlling output.
With light component encoder controls light, and light controls output.

And we don’t know your final setup, I don’t expect you will use it to control LED on breadboard…

OK, the hard way…


This is my setup until now.
The next step is to change the 6 Button enclosures with enclosures including two rotary encoders.
Marked as T>E.
Everything works as expected in iobroker with MQTT. So why should i change to HA and start again for the next 2 Years? Yes, it took 2 Years to built all the hardware and get it to work.
I think, if ESPHome could send also MQTT Messages to iobroker, i could find a way to process the Messages to trigger all i need.
The actual project is for Dimming and powering my white only Desk Lamp on my Computer.
But , when i push the Button Tschüss, which means leaving the House, there is a script, that pauses the Music Server, turn all the Lights off, and switches off some Nous A1t for some other equipment.
If someone pushes Button Papa, the TV, Audio and Music Player will start. Also if it is after SunsetStart some Lights were set to standard values (all via Script).
All of the Working stuff is also covered via vis in iobroker, so the Kids can do things as they need.

As you can see, there was some work to do in the past…
How long does it take, to migrate all of the Stuff and functions to HA and learn how to?
I don’t know. So my decision was, to mix the two worlds and get some benefits from ESPHome for my world.
Otherwise i have to buy all i need from ama…on and all i have to do is to say “hello i’m home” and everything works with the help from an KI that tells me, “why are yo’re so late, are you in the Pub?”.
I Want my Data in my home. With Handy and some Browsers at work, they get enough private data.That’s enough.

Sorry, but this has to be said.

I see no reason if everything works and you are not missing any features.
Esphome supports MQTT and is not jealous if you use Tasmota devices as well.

So let’s take the next step:

Second Try