Using a sensor value to control the brightness of a light in HA

Hello!

I would like to build a sort of wifi remote control with an Esp32 (and espHome).
I am using a potentiometer with adc on the esp, I get the value in HA as a sensor, this works fine.
But I want to use the value of the pot to control for exemple the brightness of a light that is connected to HA like a PhilipsHue bulb, or the color (RGB if I have 3 pot for exemple), or the speed of an fastLed effect (adressable led strip).

Is that possible? Can I do that in an automation or a script? If that is possible could someone tell me how to proceed?

Thank you :wink:

See this post which has most of what you need.

You will need an automation to call the turn_on service, just template the brightness using the value of your sensor, making sure it falls in the range of 1-255.

Edit: You can use a single turn_on automation to control your light to be on or off. Just trigger the automation with a change in sensor value, and make it be in the range 0-255. A brightness of zero turns the light off (even though you are calling light.turn_on).

Thank you very much! I’ll try. :wink:

Hello,
I tried this sketch in a script but I get an error.

“light.cuisine” is the light I want to control and “sensor.potentiometre” is my pot value.

Sketch:


alias: potentiometre to control brightness

service: light.turn_on
target:
  entity_id: light.cuisine
data:
  brightness: '{{ states(''sensor.potentiometre'') }}'

Error:


Key malformed: invalid slug L (try l)

It’s my first time playing with variables in HA so I’m a noobie :slight_smile: :sweat_smile:

Thanks for help :wink:

I think you have got it confused with your quotes.

Try like this:

alias: potentiometre to control brightness

service: light.turn_on
target:
  entity_id: light.cuisine
data:
  brightness: "{{ states('sensor.potentiometre') }}"

You have used two single quotation marks instead of the double quote mark.

Also you can test your templates in Developer Tools (the little hammer on the toolbar, then under the Template tab). If you paste "{{ states('sensor.potentiometre') }}" into there it should display the value of the sensor.

Yes I got a bit confused I don’t really know what i’m doing with this sketch :sweat_smile:
I’ve tried with your sketch and I got this error:


Message malformed: expected float for dictionary value @ data['brightness_pct]

By the way I’ve used “brightness_pct” because my value goes between 0 and 100

I think that my value is not the good variable type.

How can I fix this?

Thank you very much

If you paste just the template in the template editor, what is returned?

Assuming it’s fine, try removing the double quotes around the templated value.

Also - if you use from 0-100 as your value, your light will never get to full brightness.

I’ve put it in the template editor and it worked, the value of the pot is displayed! Already a victory for me! :sweat_smile:

But I still get the same error as before:


Message malformed: expected float for dictionary value @ data['brightness_pct]

I think I have to convert the variable type form string to float. But I don’t know how to do that.

Thank you very much! :slight_smile:

Did you get this working? If not post the whole automation for controlling the light, not just snippets.

No I didn’t. I still get errors.

Here is my script:


alias: Brightness using pot
sequence:
  - type: turn_on
    device_id: 91bf21e23839e3659d64534b63611eec
    entity_id: light.cuisine
    domain: light
    brightness_pct: {{ states('sensor.potentiometre') }}
mode: single

And here is the error I get


Message malformed: expected float for dictionary value @ data['brightness_pct]

Ah - a script - ok - this is tested and works:

light_adjust_with_pot:
    alias: Brightness using pot
    sequence:
      - variables:
          new_brightness: "{{ states('sensor.potentiometre') | float }}"
      - service: light.turn_on
        data: 
          device_id: 91bf21e23839e3659d64534b63611eec
          entity_id: light.cuisine
          domain: light
        data_template:
          brightness_pct: "{{ new_brightness }}"
    mode: single

It works! Thank you so much!

I like this forum. Everybody try to help and I think this is beautiful.

If I was living near of you I would pay you a drink.

Thanks again for help :wink: