I would like my dimmers to remember the last dim level. When I physically switch them on, they should not go to 100%, but return to the previous brightness.
My idea is:
configure a sensor to follow the brightness of the light
configure an automation to store the sensor state value in an input_number on change. But only do this, if the to_state is above zero, to avoid storing the zero brightness when switching off
configure an automation to restore the brightness when the light is switched on from from_state = off
So far the theory. Unfortunately, I’m stuck somehwere around step 2. My sensor looks like this and reports correct value from 0…255 in HA:
I’ve also defined a helper in the UI called input_number.dimmwert_deckenlampe_leo. Lovelace shows this as slider.
When I try to store the changing sensor value, it doesn’t work without any errors. I suspect, the trigger is not firing, but I don’t understand why:
alias: Dimmer Leo
description: Dimmwert für die Deckenlampe von Leo beim einschalten wieder herstellen
trigger:
- platform: state
entity_id: sensor.light_leo_brightness
action:
- service: input_number.set_value
data_template:
entity_id: input_number.dimmwert_deckenlampe_leo
value: '{{ trigger.to_state.state | int }}'
mode: single
I’m one step further. The following script stores the current dimmer brightness in an input_number. It makes sure that it does this only when the brightness doesn’t go down to zero and also not when it goes to 255:
alias: Dimmer Leo merken
description: Dimmwert für die Deckenlampe von Leo merken
trigger:
- platform: state
entity_id: sensor.light_leo_brightness
condition:
- condition: numeric_state
above: '0'
entity_id: sensor.light_leo_brightness
below: '255'
action:
- service: input_number.set_value
data_template:
entity_id: input_number.dimmwert_deckenlampe_leo
value: '{{ trigger.to_state.state | int }}'
mode: single
This seems to work reliably and I see the slider moving in Lovelace also when I use the physical switch.
The next step would be to restore the brightness level when the dimmer is switched on from off state.
alias: Dimmer Leo setzen
description: Dimmwert beim einschalten auf alten Wert setzen
trigger:
- platform: state
entity_id: light.homematicip_dimmer_leo
from: 'off'
condition: []
action:
- service: light.turn_on
data_template:
entity_id: light.homematicip_dimmer_leo
brightness: '{{ states("input_number.dimmwert_deckenlampe_leo") }}'
mode: single
This already works in principle. Now, when I switch the light on by dimming up and stop somewhere else than the stored input_number, the automation restores the previous state nevertheless. So I guess I miss a condition that only restores the value when the light tries to go from 0 to 255 in brightness from the off state, which is what this switch does by default when tapping it.
So my idea was the following, but that again doesn’t work and seems to be without effect:
alias: Dimmer Leo setzen
description: Dimmwert beim einschalten auf alten Wert setzen
trigger:
- platform: state
entity_id: light.homematicip_dimmer_leo
from: 'off'
condition:
- condition: state
state: '255'
entity_id: sensor.light_leo_brightness
action:
- service: light.turn_on
data_template:
entity_id: light.homematicip_dimmer_leo
brightness: '{{ states("input_number.dimmwert_deckenlampe_leo") }}'
mode: single