Dimming and switching ESP with Monostable switch

Hello,
I would like to request some help of my project. I’ve also tried my GPT friend, but it was not able to solve my problem, I always get into a message loop. :smiley:

I would like to control my light with PWM on GPIO4 (and parallely on GPIO14 as well). I have monostable switch on GPIO2.
Requirements would be:
-brightening/dimming from webgui manually and from HA manually+script. (as of now, it is working)

  • if Ha started the automatic dimming/brightening it should be overruled manually (it is also working)

  • for a short click it should toggle the light: turn on (100%) and turn off (0%) the light, it is not important what value was there before when it is different then 0%-> turn off. if it was 0% then it should changed to 100%.

  • for a long press it should increase the value (if it was 0%) OR previous action was dimming.

  • for a long press it should decrease the value (if it was different then 0%) AND previous action was brightening.

Unfortunately, I’ve encountered a problem where, after the lights are turned off and I attempt to brighten them, they start blinking or sometimes do not respond to the first few actions for several minutes after flashing and boots up correctly.

(I’ve tried to invert the code, then it never dimmed, and fully shined.)

  • I bet the startup unressponsiveness is another issue, so first of all let’s focus to the actions.

Please let me know what I’ve missed!

light:
  - platform: monochromatic
    name: ${friendly_name} Indoor Lighting
    id: ${IdPrefix}_light
    output: ${IdPrefix}_light_indoor_out
    default_transition_length: 1.0s
    icon: "mdi:castle"
    on_turn_off:
      then:
        - lambda: 'id(button_released_after_brightening) = false;'
        - lambda: 'id(button_released_after_dimming) = true;'
    on_turn_on:
      then:
        - lambda: 'id(button_released_after_brightening) = true;'
        - lambda: 'id(button_released_after_dimming) = false;'
  - platform: monochromatic
    name: ${friendly_name} Outdoor Lighting
    id: ${IdPrefix}_light_outdoor
    output: ${IdPrefix}_light_outdoor_out
    default_transition_length: 1.0s
    icon: "mdi:chess-rook"
    on_turn_off:
      then:
        - lambda: 'id(button_released_after_brightening) = false;'
        - lambda: 'id(button_released_after_dimming) = true;'
    on_turn_on:
      then:
        - lambda: 'id(button_released_after_brightening) = true;'
        - lambda: 'id(button_released_after_dimming) = false;'

# Example output entry
output:
  - platform: esp8266_pwm
    id: ${IdPrefix}_light_indoor_out
    pin: GPIO4
    frequency: 200 Hz
    #inverted: false
    zero_means_zero: true
    min_power: 0.00
    max_power: 1.00
  - platform: esp8266_pwm
    id: ${IdPrefix}_light_outdoor_out
    pin: GPIO14
    frequency: 200 Hz
    #inverted: false
    zero_means_zero: true
    min_power: 0.00
    max_power: 1.00
  - platform: gpio
    pin: GPIO15
    id: voltage_meter_power


binary_sensor:
  - platform: template
    name: "Dimming in Progress"
    id: ${IdPrefix}_dimming_in_progress
    lambda: |-
      return id(dimming_in_progress);
  - platform: template
    name: "Manual control mode"
    id: ${IdPrefix}_manual_control
    lambda: |-
      return id(manual_control);
      
  - platform: gpio
    pin:
      number: GPIO2
      mode: INPUT_PULLUP
      inverted: true
    icon: "mdi:castle"
    id: ${IdPrefix}_light_touch
    filters:
      - delayed_on_off: 50ms
    on_multi_click:
      - timing:
          - ON for at most 1s
          - OFF for at least 0.5s
        then:
          - if:
              condition:
                and:
                  - lambda: 'return id(button_released_after_brightening);'
                  - or: 
                    - light.is_on: ${IdPrefix}_light
                    - light.is_on: ${IdPrefix}_light_outdoor
              then:
                - light.turn_off:
                    id: ${IdPrefix}_light
                    transition_length: 2.5s
                - light.turn_off:
                    id: ${IdPrefix}_light_outdoor
                    transition_length: 2.5s
              else:
                - light.turn_on:
                    id: ${IdPrefix}_light
                    transition_length: 2.5s
                    brightness: 100%
                - light.turn_on:
                    id: ${IdPrefix}_light_outdoor
                    transition_length: 2.5s
                    brightness: 100%

      - timing:
          - ON for at least 1s
        then:
          - while:
              condition:
                binary_sensor.is_on: ${IdPrefix}_light_touch
              then:
                - if:
                    condition:
                      and:
                        - lambda: 'return id(button_released_after_brightening);'
                        - or: 
                          - light.is_on: ${IdPrefix}_light
                          - light.is_on: ${IdPrefix}_light_outdoor
                    then:
                      - light.dim_relative:
                          id: ${IdPrefix}_light
                          relative_brightness: -4%
                          transition_length: 0.2s
                      - light.dim_relative:
                          id: ${IdPrefix}_light_outdoor
                          relative_brightness: -4%
                          transition_length: 0.2s
                    else:
                    - light.dim_relative:
                        id: ${IdPrefix}_light
                        relative_brightness: 4%
                        transition_length: 0.2s
                    - light.dim_relative:
                        id: ${IdPrefix}_light_outdoor
                        relative_brightness: 4%
                        transition_length: 0.2s
                - delay: 0.2s
    on_release:
      then:
        - if:
            condition:
              lambda: 'return id(dimming_in_progress);'
            then:
              - lambda: 'id(manual_control) = true;'
        - lambda: 'id(button_released_after_brightening) = true;'
        - lambda: 'id(button_released_after_dimming) = true;'
    on_press:
      then:
        - lambda: 'id(button_released_after_brightening) = false;'
        - lambda: 'id(button_released_after_dimming) = false;'


globals:
  - id: manual_control
    type: bool
    restore_value: no
    initial_value: 'false'
  - id: dimming_in_progress
    type: bool
    restore_value: no
    initial_value: 'false'
  - id: button_released_after_brightening
    type: bool
    restore_value: no
    initial_value: 'false'
  - id: button_released_after_dimming
    type: bool
    restore_value: no
    initial_value: 'false'

Thank’s in advance!

I use a moes dimmer (reflashed with esphome).
It also uses a pwm signal.
Maybe this yaml can be of help?

1 Like

Its probably from the transition_length your using all over the place.

I would suggest just trying to set up your lights by following the documentation and then ask if you need help. Chatgpt has gotten better at esphome yaml but, its still a little wonky. Also, a lot of people dont like helping people who just pull some randomness off gpt and then want the community here to fix it and im in that boat too.

Someone else may want to mess with it but, I think youd have more helpful people if they see someone trying and needing help rather than fixing gpt.

That’s precisely why I’m here! :slight_smile: Perhaps there has been a misunderstanding. The code isn’t a product of randomness. :wink: It’s been carefully constructed and created by me to meet my ?specific? needs.

I sought assistance from GPT to identify the mistake (even in the logic) and not to produce it, but it hasn’t helped. (That’s a common mistake in GPT: what the managers thinks is it and what actually is use for - but it is another topic…)

Nevertheless, if you feel that the question isn’t directed at you, then you’re not obliged to answer - just go on the reading and don’t mess the board. (even with the reply what you expect.)

Thank you! It looks promising! The code uses lambda expressions quite frequently. Nonetheless, it seems to meet most of my requirements, so I will review it and try to adapt to my needs in the coming days!

Thank’s!

1 Like