Control LED strip brightness through MQTT with automation

Hi,

First of all Merry Christmas to everyone who celebrates it!
These forums have helped me a numerous times, but I can’t seem to find a solution for my current problem. I’m starting to think that I’m overthinking something, so just tell me if this is the case.
I have a LED strip connected to an arduino nano, which has mysensors (fastled library) sketch flashed. The arduino is connected to a Pi with a USB cable. Pi is running HA in a python venv. This works fine.
I also have mqttstatestream set up so I can control all my HAs running a separate Pis (one per each room)
mqttstatestream in general also works fine.
I can turn the LED strip on/off, can set the color. The issue is only with setting the brightness of the LED strip from the master HA.
Now the code:
Light config on the master HA:

  - platform: mqtt
    name: TK Lampa
    command_topic: "beosztottak/szulotv/mqttstates/light/tklampa_0_15/set_status"
    state_topic: "beosztottak/szulotv/mqttstates/light/tklampa_0_15/V_STATUS"
    state_value_template: "{{ value }}"
    brightness_command_topic: "beosztottak/szulotv/mqttstates/light/tklampa_0_15/set_percent"
    brightness_state_topic: "beosztottak/szulotv/mqttstates/light/tklampa_0_15/V_PERCENTAGE"
    brightness_value_template: "{{ ((value | float / 100) * 255) | int }}"
    rgb_command_topic: "beosztottak/szulotv/mqttstates/light/tklampa_0_15/set_rgb"
    rgb_state_topic: "beosztottak/szulotv/mqttstates/light/tklampa_0_15/V_RGB"
    rgb_value_template: "{{ value }}"
    payload_on: "ON"
    payload_off: "OFF"
    optimistic: true
    qos: 2

Automations on the slave HA:

- id: '1619194286185'
  alias: TK_lampa_fel
  description: ''
  trigger:
  - platform: mqtt
    topic: beosztottak/szulotv/mqttstates/light/tklampa_0_15/set_status
    payload: 'ON'
  condition: []
  action:
  - service: homeassistant.turn_on
    data: {}
    entity_id: light.tklampa_0_15
  mode: single

- id: '1619201591539'
  alias: TK_lampa_BRGHT
  description: ''
  trigger:
  - platform: mqtt
    topic: beosztottak/szulotv/mqttstates/light/tklampa_0_15/set_percent
  condition: []
  action:
  - service: homeassistant.turn_on
    data: {}
    entity_id: light.tklampa_0_15
  - service: light.turn_on
    target:
      entity_id: light.tklampa_0_15
    data:
      brightness: '{{ states.sensor.sen_tk_lampa_brght_korr.state }}'
  mode: single

- id: '1619202719976'
  alias: TK_lampa_color
  description: ''
  trigger:
  - platform: mqtt
    topic: beosztottak/szulotv/mqttstates/light/tklampa_0_15/set_rgb
  condition: []
  action:
  - service: light.turn_on
    target:
      entity_id: light.tklampa_0_15
    data:
      rgb_color: '{{ states.sensor.tk_lampa_set_rgb.state }}'
  mode: single

- id: '1619203224641'
  alias: TK_lampa_LE
  description: ''
  trigger:
  - platform: mqtt
    topic: beosztottak/szulotv/mqttstates/light/tklampa_0_15/set_status
    payload: 'OFF'
  condition: []
  action:
  - service: homeassistant.turn_off
    data: {}
    entity_id: light.tklampa_0_15
  mode: single

and the config for the MQTT sensors set up on the slave:

  - platform: mqtt
    name: TK_lampa_set_percent
    state_topic: "beosztottak/szulotv/mqttstates/light/tklampa_0_15/set_percent"
    #value_template: "{{ ((value | float / 255) * 100) | int }}"
    value_template: "{{ value }}"
    qos: 0

  - platform: mqtt
    name: TK_lampa_set_rgb
    state_topic: "beosztottak/szulotv/mqttstates/light/tklampa_0_15/set_rgb"
    qos: 0

  - platform: mqtt
    name: TK_lampa_set_status
    state_topic: "beosztottak/szulotv/mqttstates/light/tklampa_0_15/set_status"
    qos: 0

  - platform: template
    sensors:
      sen_tk_lampa_brght_korr:
        value_template: '{{ ((states("sensor.tk_lampa_set_percent") | float / 255) * 100) | round(0) }}'

I know that the other automations are not needed for the issue, I just pasted them to show that I’m trying to do something similar to them.
I think I understand the issue: HA is sending a value between 0-255 and the V_PERCENTAGE in mysensors is a 0-100 value.
I have found some posts regarding similar issues, that’s where I’ve got the formula form for sen_tk_lampa_brght_korr, but none of them seems to help me.

What I know until now.

  • On the master you need to have separate MQTT topics for command and state
  • These separate topics need to use the same value range (eg.:0-255), otherwise the brightness slider “jumps” around when changing it.
  • Any math in the “brightness” field of the automation on the slave does not work. That’s why I’ve created sen_tk_lampa_brght_korr sensor
  • I’ve also tried to put an attribute to the “brightness” field of the automation on the slave, like “states.light.tklampa_0_15.attributes.brightness” without luck.

The only thing I don’t understand is why doesn’t the value change in sen_tk_lampa_brght_korr change the brightness of the LEDs.

I’m not sure if any logs are needed in this case, because in general things work, so it’s only my way of thinking that needs to be adjusted…

Thanks for the help!