Setup a dimmer to remember that last brightness when toggled on/off

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:

  1. configure a sensor to follow the brightness of the light
  2. 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
  3. 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:

configuration.yaml:

  sensor:
    - platform: template
        light_leo_brightness:
          friendly_name: Helligkeit von Leo's Deckenlampe
          value_template: '{{ state_attr("light.homematicip_dimmer_leo", "brightness") | int}}'

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

So I’ve stopped here. Any advice?

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

Any ideas?

Any luck getting this to work? Super interested.

Not from HomeAssistant but I seem to remember that my Shelly devices allow a setting of such a behaviour directly on the device.