Allmost got it the way i meant, maybe someone could help me with last step
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