Automatic brightness

I need to increase the brightness of some lights when the environment is dark.

Considering that my brightness sensor registers me a maximum brightness of 300 lux, I created this automation:

automation:

  • alias: “Auto regolazione punto luce”
    trigger:
    • platform: state
      entity_id: sensor.luminosita, light.cucina
      condition:
    • condition: and
      conditions:
      • condition: state
        entity_id: group.famiglia
        state: ‘home’
      • condition: or
        conditions:
        • condition: template
          value_template: “{{ (trigger.entity_id == ‘light.cucina’) and (trigger.from_state.state == ‘off’) }}”
        • condition: template
          value_template: “{{ (trigger.entity_id == ‘sensor.luminosita’) and (states.light.cucina.state == ‘on’) }}”
          action:
          service: light.turn_on
          data_template:
          entity_id: light.cucina
          brightness_pct: “{{ (((((states.sensor.luminosita.state * (100/300)) - 100)-((states.sensor.luminosita.state * (100/300)) - 100)|abs)/2)+100)|int }}”

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

Can you help me understand the problem or find alternative solutions? Thank you

An entity’s state value is always a string value. That means even if it looks a number, it’s a string.

Your template attempts to multiply the state value of sensor.luminosita by a number. That means it is attempting to multiply a string by a number and that’s not possible.

You first have to convert the string to a number using either the int function (integer) or float function (floating point). Then you can multiply the converted value by a number.

{{ (((((states.sensor.luminosita.state|int(0) * (100/300)) - 100)-((states.sensor.luminosita.state|int(0) * (100/300)) - 100)|abs)/2)+100)|int(0) }}

Ideally, you should use this method of acquiring an entity’s state value:

states('sensor.luminosita')

instead of this method:

states.sensor.luminosita.state

The reason is explained in the documentation (refer to the Warning here).