Template to set brightness with illuminance sensor

Hey,

so I got an illuminance sensor outdoors and some lamps indoors. As the brightness changes I want to adjust my lamps so that if it’s 1800lx outside the brightness should be set to 0 and with an illuminance of 15lx they should be at max brightness (255).
The mathematical operation would be y(br) = -0.143 * x(lx) + 257.143.
One lamp got the entity id light.deckenlampe and the illuminance sensor sensor.illuminance_b.

How could I turn this into a template? It’s my first time working with templates and I can’t get the equation to work.

Thanks for your help!

Can you post your attempt? As in, the full automation and related entities.

There was no full attempt, I tried it in the developer tools section:
{{ ((-0,143) * states.sensor.illuminance_b.state + 257.143) | int }}

But I get this error: TypeError: can't multiply sequence by non-int of type 'str'

It should be -0.143, no?

Yeah of course, sorry.

Now it says: TypeError: can't multiply sequence by non-int of type 'float'

You need to convert the state to a number. Try {{ ((-0.143) * (states("sensor.illuminance_b") | float(0)) + 257.143) | int }}.

Thanks, that worked!

Now I entered this to my configuration.yaml:

  - platform: template
    sensors:
      brightness:
        friendly_name: Helligkeit
        entity_id: sensor.illuminance_b
        value template: >-
          {{ ((-0.143) * (states("sensor.illuminance_b") | float(0)) + 257.143) | int }}

And get this error message when checking the config:
Invalid config for [sensor.template]: [value template] is an invalid option for [sensor.template]. Check: sensor.template->sensors->brightness->value template. (See ?, line ?).

Any idea on how to fix this?
Or is this even necessary? If not, how can I use the outcoming value as a brightness parameter if it’s less or equal to 255?

I mean, it’s pretty explicit about the problem. value template isn’t a config variable, it’s value_template.

Also, you are using the old sensor configuration, which is not recommended. See the docs for the updated format. You should also just keep the docs handy, giving them a read through can answer most of your questions :slight_smile:

If you do it like this it will be bounded to between 0 and 255 (inclusive), to prevent setting an illegal brightness value.

template:
  - sensor:
      - name: 'Helligkeit'
        unit_of_measurement: "Int"
        state: >
          {{ ( [0, (-0.143 * states('sensor.illuminance_b')|float(0) + 257.143 )|int(0), 255]|sort )[1] }}

Thanks!
Before your help I was using a condition, so that it would only use these values if they are between 0 and 255. But that’s way better!

But somehow I can’t use these values. That’s what I tried:

service: light.turn_on
target:
  device_id: d0a15f0b67762a5b9ec702exxxxxxxxx
data:
  brightness: {{ states.sensor.helligkeit.state }}
  color_temp: 200

But I get this error when I try to run it:
Fehler beim Aufrufen des Diensts script/1617121793358. expected int for dictionary value @ data['helligkeit']

So how can I use it as an int value?
Thanks in advance!

Quote your single line template:

data:
  brightness: "{{ states.sensor.helligkeit.state }}"

Also from the docs:

So:

data:
  brightness: "{{ states('sensor.helligkeit') }}"

Thanks, now it all works!

Full config:
config.yaml:

template:
    sensor:
      - name: "Brightness"
        unit_of_measurement: "Int"
        state: >
          {{ ( [0, (-0.143 * states('sensor.illuminance_b')|float(0) + 257.143 )|int(0), 255]|sort )[1] }}

script:

service: light.turn_on
target:
  device_id:
    - d0a15f0b67762a5xxxxxxxxxxxxxx
    - 254c1f716b32dxxxxxxxxxxxxxxx
data:
  color_temp: 200
  brightness: '{{ states(''sensor.brightness'') }}'