Brightness Slider - Set Input_number to zero when light is off

Allmost got it the way i meant, maybe someone could help me with last step :grin:

I want to control the xiaomi gateway light.
Below the light switch i have a slider for brightness.
I set and sync the slider, even if it is changed outside HA.

When the light is off it has no attribute ‘brightness’ (got me an error, that it couldn’t retrieve brightness)
Resolved that error by adding a condition to only sync when state of the light is ‘on’.

Only thing that i haven’t figured out, and possibly is something obvious, is;

When i turn off the light, by switch through HA or with the Mi app, i want the slider set to value zero.
I possibly have a conflicting automation. Because when i now turn the switch to off, the slider goes to zero, but then the light switch turns back on.
I.e. I turn off light, slider goes to zero, but this also triggers the sync part to turn the light on (with brightness to zero).

Is there a way to have both?
That i can really turn off the light (switch stays to off) and have full control with slider (set & sync).

homeassistant:

  ################################################################################
  ## CUSTOMIZE
  ################################################################################

  customize:
  
    light.gateway_light_7811dcb8c9ba:
      friendly_name: Gateway

################################################################################
## INPUT NUMBER
################################################################################

input_number:

  ################################################################################
  ## BRIGHTNESS SLIDER
  ################################################################################

  brightness_gateway:
    name: Brightness
    initial: 100
    min: 0
    max: 100
    step: 5
    icon: mdi:brightness-auto


################################################################################
## SENSOR
################################################################################

sensor:

  ################################################################################
  ## BRIGHTNESS GATEWAY
  ################################################################################

  - platform: template
    sensors:
      light_gateway_brightness:
        friendly_name: 'Gateway Brightness'
        value_template: >-
          {%- if is_state('light.gateway_light_7811dcb8c9ba', 'on') -%}
            {{ states.light.gateway_light_7811dcb8c9ba.attributes.brightness | int / 2.55 }}
          {%- endif %}
        unit_of_measurement: '%'


################################################################################
## AUTOMATIONS
################################################################################

automation:

  ################################################################################
  ## BRIGHTNESS SET
  ################################################################################

  - alias: 'Gateway Brightness (Set)'
    initial_state: on
    trigger:
      platform: state
      entity_id: input_number.brightness_gateway
    action:
    - service: light.turn_on
      data_template:
        entity_id: light.gateway_light_7811dcb8c9ba
        brightness_pct: "{{ trigger.to_state.state | int }}"
    - delay:
        seconds: 3

  ################################################################################
  ## BRIGHTNESS SYNC
  ################################################################################

  - alias: 'Gateway Brightness (Sync)'
    trigger:
      platform: state
      entity_id: sensor.light_gateway_brightness
    condition:
      condition: state
      entity_id: light.gateway_light_7811dcb8c9ba
      state: 'on'
    action:
    - service: input_number.set_value
      data_template:
        entity_id: input_number.brightness_gateway
        value: "{{ trigger.to_state.state | int }}"
    - delay:
        seconds: 3

I’ve added this automation to try and set slider to zero when light is off:

################################################################################
  ## BRIGHTNESS LIGHT OFF SET SLIDER TO 0
  ################################################################################

  - alias: Light Off Set Slider to 0
    trigger:
      platform: state
      entity_id: light.gateway_light_7811dcb8c9ba
      to: 'off'
    action:
    - service: input_number.set_value
      data_template:
        entity_id: input_number.brightness_gateway
        value: 0

Maybe as an suggestion / idea;
Sync only if the value of the slider is between 1-100, and slider = 0 equals light is off :thinking:

why not try and take care of it in your sensor definition?
Something along the lines:

        value_template: >-
          {%- if is_state('light.gateway_light_7811dcb8c9ba', 'on') -%}
            {{ states.light.gateway_light_7811dcb8c9ba.attributes.brightness | int / 2.55 }}
          {%- else %}0
          {%- endif %}

Thanks for the reply.
Had the same thought, but sadly it doesn’t work.

I don’t get any errors / warnings, but when turning the switch off, the slider doesn’t go to zero / nothing happens to the slider.
I thought maybe because i need to define it to brightness.
So i tried:

    value_template: >-
      {%- if is_state('light.gateway_light_7811dcb8c9ba', 'on') -%}
        {{ states.light.gateway_light_7811dcb8c9ba.attributes.brightness | int / 2.55 }}
      {% else %}
        - service: input_number.set_value
          data_template:
            entity_id: input_number.brightness_gateway
            value: 0
      {%- endif %}

But that doesn’t work either.
I think because when the light is ‘off’, the attribute ‘brightness’ doesn’t exist.

that should have zero impact on what you’re trying to achieve: when light is off, you should be able to set an input_number’s value to 0

I’m not sure this would work: you can’t assign an action to an entity’s value.
The value_template I provided earlier on defo works for me… At least in the dev-tool code checker.
Maybe add a dash after else and endif?

        value_template: >-
          {%- if is_state('light.gateway_light_7811dcb8c9ba', 'on') -%}
            {{ states.light.gateway_light_7811dcb8c9ba.attributes.brightness | int / 2.55 }}
          {%- else -%}0
          {%- endif -%}

Exactly :relieved: just want to set the input number for the slider.

Thanks for clearing that up!

That’s strange, somehow it doesn’t work for me yet.
Tried with the dashes after else and endif.

Don’t i have to set the value of the input_number after ‘else’?
Something with input_number.set_value.
Or is just calling ‘0’ enough?

just 0 is enough,the template allows you to run some logical checks and return a value to assign

1 Like
sensor:

  ################################################################################
  ## BRIGHTNESS GATEWAY
  ################################################################################

  - platform: template
    sensors:
      light_gateway_brightness:
        friendly_name: 'Gateway Brightness'
        value_template: >-
          {%- if is_state('light.gateway_light_7811dcb8c9ba', 'on') -%}
            {{ states.light.gateway_light_7811dcb8c9ba.attributes.brightness | int / 2.55 }}
          {%- else -%}0
          {%- endif -%}
        unit_of_measurement: '%'

Config of my sensor.
But no effect on the slider when i turn the light off :thinking:

I have the feeling your value_template may not be the area to focus on.
I see you have an automation (‘Gateway Brightness (Sync)’) to set the input_number value when your light is on.
Of course when the light is off, that doesn’t work as there’s no brightness attribute.
So maybe this is where we need to put our efforts and forget the value_template?
Can you try this? (allegedly not tested so check your config first)

  - alias: 'Gateway Brightness (Sync)'
    trigger:
    - platform: state
      entity_id: sensor.light_gateway_brightness
    - platform: state
      entity_id: light.gateway_light_7811dcb8c9ba
    action:
    - service: input_number.set_value
      data_template:
        entity_id: input_number.brightness_gateway
        value: >-
          {% if states.light.gateway_light_7811dcb8c9ba.attributes.brightness is defined %}
            {{trigger.to_state.state | int }}
          {% else %}0
          {% endif %}
    - delay:
        seconds: 3

Thanks for that suggestion! Really helpful.
Didn’t know you could call ‘is defined’.
Your code gives no errors, i.e. is correct.

It works in the same way that my last automation from topic start works;

  • Toggle switch to ‘off’
  • It changes the value of the slider to ‘0’
  • Almost immediately after it turns the switch back ‘on’

Idea; Could it be that the automation ‘Gateway Brightness (Set)’ is conflicting?
Maybe add a condition to only set the slider if the light is ‘on’.

check in your logbook if another automation runs after this one, you’ll then know where you need to look/work

Yeah, you’re right. As shown in the logbook, slider gets set to ‘0’, then ‘gateway brightness set’ turns back on the light.

Now you know where to look :wink:
I’ll let try a figure it out first. Let me know if you need more help

1 Like

Yeah, got it working!
‘Brightness Set’ turns on the light and then sets the brightness accordingly.
Triggered by the input slider, even if value is ‘0’.
So added a condition to only set when slider is above ‘0’
Also added an automation so that it is two-way.

Attributes of slider / light

  • light off -> slider to ‘0’
  • slider to ‘0’ -> light off
  • when turning light back on, it resumes previous brightness value.
  • brightness sync, also when changed outside of HA
  • Initial value set to 0 / lights off

Now finally got a working template for all future lights.
Complete code below. Still very rookie, so convinced code could be more clean :sweat_smile:
Ooh and still need to play around with the delays, see those as examples.
Big thanks to lolouk for providing code, solutions, helping me get on the right track!

homeassistant:

  ################################################################################
  ## CUSTOMIZE
  ################################################################################

  customize:

    light.gateway_light_7811dcb8c9ba:
      friendly_name: Gateway

################################################################################
## INPUT NUMBER
################################################################################

input_number:

  ################################################################################
  ## BRIGHTNESS SLIDER
  ################################################################################

  brightness_gateway:
    name: Brightness
    initial: 0
    min: 0
    max: 100
    step: 5
    icon: mdi:brightness-auto


################################################################################
## SENSOR
################################################################################

sensor:

  ################################################################################
  ## BRIGHTNESS SENSOR
  ################################################################################

  - platform: template
    sensors:
      light_gateway_brightness:
        friendly_name: 'Gateway Brightness'
        value_template: >-
          {%- if is_state('light.gateway_light_7811dcb8c9ba', 'on') -%}
            {{ states.light.gateway_light_7811dcb8c9ba.attributes.brightness | int / 2.55 }}
          {%- endif -%}
        unit_of_measurement: '%'


################################################################################
## AUTOMATIONS
################################################################################

automation:

  ###############################################################################
  # LIGHT OFF SLIDER TO 0
  ###############################################################################

  - alias: Light Off Slider to 0
    trigger:
      platform: state
      entity_id: light.gateway_light_7811dcb8c9ba
      to: 'off'
    action:
    - service: input_number.set_value
      data_template:
        entity_id: input_number.brightness_gateway
        value: 0

  ################################################################################
  ## BRIGHTNESS SYNC
  ################################################################################

  - alias: Brightness Sync
    trigger:
      platform: state
      entity_id: sensor.light_gateway_brightness
    action:
    - service: input_number.set_value
      data_template:
        entity_id: input_number.brightness_gateway
        value: >-
          {% if states.light.gateway_light_7811dcb8c9ba.attributes.brightness is defined %}
            {{trigger.to_state.state | int }}
          {% else %}0
          {% endif %}
    - delay:
        seconds: 3

  ################################################################################
  ## BRIGHTNESS SET
  ################################################################################

  - alias: Brightness Set
    initial_state: on
    trigger:
      platform: state
      entity_id: input_number.brightness_gateway
    condition:
      condition: numeric_state
      entity_id: input_number.brightness_gateway
      above: 0
    action:
    - service: light.turn_on
      data_template:
        entity_id: light.gateway_light_7811dcb8c9ba
        brightness_pct: "{{ trigger.to_state.state | int }}"
    - delay:
        seconds: 3

  ################################################################################
  ## SLIDER TO 0 LIGHT OFF
  ################################################################################

  - alias: Slider to 0 Light Off
    trigger:
      platform: numeric_state
      entity_id: input_number.brightness_gateway
      below: 1
    action:
    - service: light.turn_off
      data_template:
        entity_id: light.gateway_light_7811dcb8c9ba
    - delay:
        seconds: 1

2 Likes

HiHow you changed the code since? I used your code as a base, and still have to play with different delays and was wondering if you came up with something new.

Has anyone got something like this brightness sync to work with a group of lights?