Change value of rotary_encoder when light is changed in HA

Hello,

I am trying to create a dimmable light that I can change over my phone AND using a rotary encoder. It all works fine, except the fact, that my encoder always keeps the last state and doesnt update to the current value of the light. The result is that (If I changed something on my phone) was soon as I turn the knob, the light jumps to the last state of the rotary encoder instead of adding/subtracting to the current light state.

So my question is: How can I read the current state of brightness of my light component in order to pass it to the encoder via:

light:
  - platform: binary # or any other platform
    # ...
    on_state:
     - sensor.rotary_encoder.set_value:
      id: my_rotary_encoder
      value: *??VALUEOFLIGHT??*

What do I need to put in for ??VALUEOFLIGHT?? ?

Thank you in advance! :slight_smile:

My whole code:

esphome:
  name: d1-lichtleiste-schlafzimmer

esp8266:
  board: d1_mini

# Enable logging
logger:

# Enable Home Assistant API
api:
  encryption:
    key: "3sh6HmIFsLs+12r/HOZo44VHjrFe0BGNLPDKmG0ERZw="

ota:
  password: "a6dfa5e175c5db6aaeb97190cfdb5f2f"

wifi:
  ssid: !secret wifi_ssid
  password: !secret wifi_password

  # Enable fallback hotspot (captive portal) in case wifi connection fails
  ap:
    ssid: "D1-Lichtleiste-Schlafzimmer"
    password: "GWOnDwJwq5vi"

captive_portal:
    
  

# Example configuration entry
light:
  - platform: monochromatic
    name: "Lichtleiste Bett"
    id: lichtleiste_bett
    output: output_pwm_1


# Output für Lichtleiste
output:
  - platform: esp8266_pwm
    id: output_pwm_1
    min_power: 0.03
    zero_means_zero: true 
    pin: 
      number: D8
      inverted: false

# Example configuration entry
sensor:
  - platform: rotary_encoder
    id: my_rotary_encoder
    name: "Rotary Encoder"
    min_value: 0
    max_value: 100
    resolution: 2
    publish_initial_value: true
    pin_a: 
      number: D1
      inverted: true
      mode:
        input: true
        pullup: true
    pin_b: 
      number: D2
      inverted: true
      mode:
        input: true
        pullup: true
    on_value:
      then:
        - light.turn_on:
            id: lichtleiste_bett
            brightness: !lambda "return x/100;"
            transition_length: 31ms
            
      value: "{{ state_attr('light.xyz', 'brightness') | int(0) }}"

Not in esphome.

Thank you for your replay. Unfortunately, I get this error:

Expected integer, but cannot parse state_attr(‘light.lichtleiste_bett’, ‘brightness’| int(0))); as an integer.

Any ideas?

Check for typo’s. The error ends in 3 normal braces. Did you use 2 curly’s at the end? And there’s no brace after ‘brightness’ but that shoud be there.

Anyway, the best way to find out is gradally building the template up in developer tools to see what comes out, so start with:

{{ state_attr(‘light.lichtleiste_bett’, ‘brightness’) }}

and check if that represents an integer number. Maybe if your light puts out a decimal value you should use float(0)

Where in the esphome docs do you see this syntax?

Oh sorry, I saw the ESPHome Yaml, but I thought the code block above was a template in Home Assistant.

Thank you for your reply. I am pretty sure that I don’t have typos. I also tried the casting methods I know in c++.

But if I read correctly the snippet doesn’t work for esphome right? Do you have any Idea how to approach the problem in esphome?

No, I don’t know how to do it in ESPHome - I thought it was a normal template in Home assistant. Sorry for the confusion.

Ok. But thank you anyway :slight_smile:

You can get any ha attribute into esphome via a homesssistant sensor. Home Assistant Sensor — ESPHome

Thank you!

I solved it using this yaml:

sensor:
  - platform: homeassistant
    id: licht_in_ho
    entity_id: light.lichtleiste_bett_2
    attribute: brightness
    accuracy_decimals: 0
    filters:
      - calibrate_linear:
          # Map values from 0-255 to 0-100
          - 0.0 -> 0.0
          - 255 -> 100
      - throttle: 3s #prevent loop
    on_value:
     - sensor.rotary_encoder.set_value:
        id: my_rotary_encoder
        value: !lambda |-
          return id(licht_in_ho).state;