Rotary encoder controlled light-how to control other light?

Hi, I have built EspHome device which control the lights with rotary encoder and it works fine. But I have another light nearby and I would like to control it with this encoder also, to be copy of this light. It doesn’t matter if it can be done with code in Esp or with HA automation, just want it to work :grin:

Share your current yaml. Don’t forget to include it in preformatted text tags.

This is a part of the working light code.

light:
  - platform: monochromatic
    name: "Bazen svijela"
    output: PWMbazen
    id: lights_bazen
  
    
sensor:
  - platform: rotary_encoder
    name: "bazen dimmer"
    min_value: 16
    max_value: 100
    pin_a: 
      number: 7
      mode:
        input: true
        pullup: true
    pin_b: 
      number: 5
      mode:
        input: true
        pullup: true
    restore_mode: ALWAYS_ZERO
    on_clockwise:
         - light.dim_relative:
            id: lights_bazen
            relative_brightness: 2%
            transition_length: 0.1s
            brightness_limits:
              min_brightness: 16%
         
            
    on_anticlockwise:
      - light.dim_relative:
          id: lights_bazen
          
          relative_brightness: -2%
          transition_length: 0.1s
          brightness_limits:
            min_brightness: 16%
      
  
  
output:
  - platform: ledc
    pin: 11
    id: PWMbazen
    power_supply: bazenPS
power_supply:
  - id: 'bazenPS'
    pin:
      number: 3
    enable_time: 1sec
binary_sensor:
  - platform: gpio
    pin: 
      number: 9
      inverted: true
      mode:
        input: true
        pullup: true
    name: "Button bazen"
    id: buttonbazen
    on_press: 
      then:
       - light.toggle: lights_bazen
    

The simplest way is probably to create a binary sensor or switch - with a button or similar. Then change your yaml:

    on_clockwise:
      if:
        condition:
            switch.is_on: your_switch
        then:
          - light.dim_relative:
              id: lights_bazen
              relative_brightness: 2%
              transition_length: 0.1s
              brightness_limits:
                min_brightness: 16%
        else:
          - light.dim_relative:
              id: lights_otherlight
              relative_brightness: 2%
              transition_length: 0.1s
              brightness_limits:
                min_brightness: 16%          

# etc...

This will work if the other light is controlled by that ESPHome device.

If not - you can do this:

    on_clockwise:
      if:
        condition:
            switch.is_on: your_switch
        then:
          - light.dim_relative:
              id: lights_bazen
              relative_brightness: 2%
              transition_length: 0.1s
              brightness_limits:
                min_brightness: 16%
        else:
          - home assistant.action:
              action: light.turn_on
              data_template:
                entity_id: your_other_light
                brightness_step_pct: 2
                transition: 0.1
   
# etc...

Not tested - but you can see the docs yourself here:

and here:

one light is controlled with one Esp and other with other Esp :slight_smile:
On esp where is a rotary encoder it works like this: light is off and when I rotate it clockwise it goes on and turn bightness up. On counter clockwise it decrease brightness until it shutdown the light. There is a push button on rotary encoder “Button bazen” which toggle light on/off. I want other light works the same, but controlled with encoder on this Esp unit. Is it possible to add automation on HA to adjust other light based on turning encoder on this ESP?

I used device group, you don’t have to rely on HA automation to sync the lights

That is what my second example above does…

You don’t need an automation on HA - the ESP can do it all.

If you want them to change at the same time, just remove the condition check.

I like this idea :slight_smile:
Before I start with this, is it important in what Esp to put this code?

Is this only for two devices on one ESP or can be used for two different ESP controllers? I can’t put ESPHome entity id from another device in code because YAML says it couldn’t find this device ID.

I try this:

on_clockwise:
       then:
         - light.dim_relative:
            id: lights_bazen
            relative_brightness: 2%
            transition_length: 0.1s
            brightness_limits:
              min_brightness: 16%
        then:
         - home assistant.action:
              action: light.turn_on
              data_template:
                entity_id: light.hallway_string_lights
                brightness_step_pct: 2
                transition: 0.1

but my YAML knowledge is not so good and can’t upload it to ESP

For both or more devices running esphome.
You basically just assign the same group for the light switch (with the rotary encoder) and lights. You can check the yaml examples on the github

Remove the second then:

Have put it like this:

on_clockwise:
       then:
         - light.dim_relative:
            id: lights_bazen
            relative_brightness: 2%
            transition_length: 0.1s
            brightness_limits:
              min_brightness: 16%
         - home assistant.action:
              action: light.turn_on
              data_template:
                entity_id: light.hallway_string_lights
                brightness_step_pct: 2
                transition: 0.1

but now it says:

on_clockwise: 
    then: 
      - light.dim_relative: 
          id: lights_bazen
          relative_brightness: 2%
          transition_length: 0.1s
          brightness_limits: 
            min_brightness: 16%
      - 
        Unable to find action with the name 'home assistant.action'.
        home assistant.action: 
          action: light.turn_on
          data_template: 
            entity_id: light.hallway_string_lights
            brightness_step_pct: 2
            transition: 0.1
  - homeassistant.action:

A typo - sorry. I did say it wasn’t tested AND provided a link to the docs - where you would have spotted the problem. :slight_smile:

I have added only this to the code:

external_components:
  - source: github://cossid/tasmotadevicegroupsforesphome@main
    components: [ device_groups ]
    refresh: 10 min

device_groups:
  - group_name: "testgroup1" 

And now I get this when try to upload to my ESP: 


Compiling .pioenvs/bazen-light/src/esphome/components/device_groups/device_groups.cpp.o
In file included from src/esphome/components/device_groups/device_groups.h:11,
                 from src/esphome/components/device_groups/device_groups.cpp:1:
src/esphome/components/device_groups/device_groups_WiFiUdp.h:15:7: error: redefinition of 'class IPAddress'
 class IPAddress {
       ^~~~~~~~~
In file included from /data/cache/platformio/packages/framework-arduinoespressif32/cores/esp32/Arduino.h:170,
                 from src/esphome/core/macros.h:7,
                 from src/esphome/core/defines.h:2,
                 from src/esphome/core/application.h:6,
                 from src/esphome/components/device_groups/device_groups.h:3,
                 from src/esphome/components/device_groups/device_groups.cpp:1:
/data/cache/platformio/packages/framework-arduinoespressif32/cores/esp32/IPAddress.h:29:7: note: previous definition of 'class IPAddress'
 class IPAddress: public Printable
       ^~~~~~~~~
Compiling .pioenvs/bazen-light/src/main.cpp.o
*** [.pioenvs/bazen-light/src/esphome/components/device_groups/device_groups.cpp.o] Error 1
In file included from src/esphome/components/device_groups/device_groups.h:11,
                 from src/esphome.h:21,
                 from src/main.cpp:3:
src/esphome/components/device_groups/device_groups_WiFiUdp.h:15:7: error: redefinition of 'class IPAddress'
 class IPAddress {
       ^~~~~~~~~
In file included from /data/cache/platformio/packages/framework-arduinoespressif32/cores/esp32/Arduino.h:170,
                 from src/esphome/core/macros.h:7,
                 from src/esphome.h:2,
                 from src/main.cpp:3:
/data/cache/platformio/packages/framework-arduinoespressif32/cores/esp32/IPAddress.h:29:7: note: previous definition of 'class IPAddress'
 class IPAddress: public Printable
       ^~~~~~~~~
*** [.pioenvs/bazen-light/src/main.cpp.o] Error 1
========================= [FAILED] Took 14.41 seconds =========================

Can you share your yaml?

Sorry for delay of my reply but after a various try’s I came to only acceptable solution. It is here, it is a blueprint for automation that sync one light to another. It works just I want to :wink:

Might seem like an acceptable solution at first but, you’ll be disappointed eventually. The first time HA goes down then so does your quick fix blueprint with all the options for controlling the light or lights. Your best option is to always have a way to use Esphome by itself and then you can still use the blueprint if youd like but, doing the way you’re doing it now is not something id suggest.

Isn’t it strange that on HomeAssistant forum you suggest someone not to use HA automation?

It only seems strange because you’re in way over your head and dont know much yet. Secondly, this is an Esphome forum and in the Esphome and other specific forums, people typically come here because they are looking for an answer related to Esphome specifically and if not then they would ask it in the HA general discussion forum.

Maybe you made a wrong turn back there and got lost??