Analog value from sensor to control a light

Hello,

for reading a value and sent this via jason to an automation, I could need you help, please.
As newbie I tried serveral things but I couldn’t get it to run…

I have a potentiometer, which runs and deliver a value between 1 and 255:

    - name: "Poti_BRT"
      #device_class: 
      #unit_of_measurement: "%"
      state_topic: "Bathroom/Light/BRT"
      value_template: "{{ value_json.brightness }}"
      #json_attributes_topic: "Bathroom/Light/BRT"
      #json_attributes_template: "{{ value_json.brightness | tojson }}"
      #value_template: "{{ value_json.state }}"

I want to use this value for an automation to control a light:

- id: '1000003'
  alias: S Lampe BRT
  description: ''
  trigger:
  - platform: mqtt
    topic: Bathroom/Light/BRT
  condition: []
  action:
  - service: light.turn_on
    #target:
     #entity_id: light.s
    data:
    #data_template:
      #brightness: "{{ value_json.brightness | from_json }}"
      #brightness: '{{ value_json.brightness | from_json }}'
      #brightness: '{{ states(''sensor.Poti_BRT'') }}'
      brightness: "{{ states('sensor.Poti_BRT') }}"
      #brightness: '{{ states('input_number.brightness')|int }}'
      #brightness: "{{ states('input_number.brightness')|int }}"
    device_id: 69c6babc1512d237ae2f374041751a0b
    entity_id: light.s
    domain: light
  mode: single

As I understood the documentation for sensor, automation and so on,
I thing I need the value from the sensor in a jason format to control the light.
I tried a lot of different ways but I had no success.

Can someone help please, to get this run?

Many thanks,
Phil

Assuming your sensor is working try this:

- id: '1000003'
  alias: S Lampe BRT
  description: ''
  trigger:
  - platform: state
    entity_id: sensor.poti_brt
    to:
  condition: []
  action:
  - service: light.turn_on
    target:
      entity_id: light.s
    data:
      brightness: "{{ states('sensor.poti_brt') }}"
  mode: single

Hello Tom,

many thanks for you help!
With your suggestion it works!

The configuration.yaml

- name: "Poti_BRT"
  #device_class: 
  #unit_of_measurement:
  state_topic: "Bathroom/Light/BRT"

Do you habe any suggestions for device_class and unit_of_measurement for
an analog porentiometer?

The automations.yaml works like as you wrote.
I was somehow not aware to use the entity.id for the json brighness…

- id: '1000003'
  alias: S Lampe BRT
  description: ''
  trigger:
  - platform: state
    entity_id: sensor.poti_brt
    to:
  condition: []
  action:
  - service: light.turn_on
    target:
      entity_id: light.s
    data:
      brightness: "{{ states('sensor.poti_brt') }}"
  mode: single

Anyway, the behaviour of the light is strange.
The values changing from 1 to 255 quickly (as I can see in a card of the dashboard)
but the light did not response all the time.
When the light is responsing then it sometimes seems in the opposite direction
and after a delay of a lot of seconds.
It is a Philips Hue bulb.
Do you have any suggestions?

May I ask you 1 more questions:
Just for my understanding: why is the following code not working? Is it because of mqtt
delivers strings and brightness is waiting for integer?
The on/off switch is working immediately but not the brightness…

I found this in the documentation at the head line “Brightness and no RGB support”:

(I tried as well “…” for brightness, instead of ‘…’)

  light:
    - name: "S Light"
      state_topic: "Bathroom/Light/Upper/hu"
      #payload "ON" / "OFF":
      command_topic: "Bathroom/Light/Upper"
      brightness_state_topic: 'Bathroom/Light/BRT/st'
      #payload 1 to 255:
      brightness_command_topic: 'Bathroom/Light/BRT'
      qos: 0
      payload_on: "ON"
      payload_off: "OFF"
      optimistic: false

Cheers,
Phil