Found solution for Lutron Caseta dimmers to remember last value

Hello,
I have a lot of lutron caseta in-wall dimmers and i was surprised to find that HA as it is cant remember the last dimmer value of the dimmer… every time you turn it off and on it resets to 100%. I have found a solution that is working for me so i wanted to share it in case others hit the same issue.

This is not the cleanest solution but it works. In a nutshell i am using an integration that enables variables in home assistant. Im sure you could change this to just some some sort of built-in input function but i went the variables route.

The idea is that i have a script that is called each time a button-cards pushed… again you could use a different trigger. When that button is pushed, the script checks to see if the light is on, if it is on, then it first saves the dimmer value in a variable named after the dimmer ‘light’ element itself and then it turns off the light (service call). If the light was off, then it turns the light on using that dimmer value that is stored in the variable.

This solution does require you to setup variables for each dimmer you have in your configuration.yaml file… so there is a bit of work there. I also still want to somehome templatize the name of the variable in the script which is what i need to do next. So for example, if i have a light called light.upstairs then i would use a variable called var.upstairs_level and in the script be able to derive that variable name from the entity_id of the light itself. Still need to do that. However i wanted to share what i have working so far.

FIrst off im using this integration for variables. You can install it thru HACS.

https://github.com/snarky-snark/home-assistant-variables#:~:text=The%20var%20component%20is%20a,whenever%20a%20specified%20event%20fires.

I will then show an example for a single dimmer.

In the configuration.yaml file i define a variable for the dimmer. You’d need one of these for each of your variables. Too bad the integration didnt allow for dynamic creation… maybe down the road :slight_smile:

var:
  office_main_lights_level:
    initial_value: 100
    icon: mdi:light

Then i have an action that could be tied to a switch or a button or an automation etc. I have it as a tap-action inside a button-card. Im dynamically sending the entity id.

    tap_action:
      action: call-service
      service: script.lutron_test
      service_data:
        dimmer: >
          [[[ return variables.entity_id ]]]

Then here is my script:

lutron_test:
  fields:
    dimmer:
      description: Lutron dimmer to store and restore dimming value
  sequence:
    - choose:
      - conditions:
        - condition: template
          value_template: '{{ is_state( dimmer, "off") }}'
        sequence:
        - service: light.turn_on
          data:
            brightness: "{{ states('var.office_main_lights_level') }}"
          target:
            entity_id: '{{ dimmer }}'
      default:
        - service: var.set
          target:
            entity_id: var.tom_s_office_main_lights_level
          data:
            value: "{{ state_attr(dimmer , 'brightness') }}"
        - service: light.turn_off
          data: {}
          target:
            entity_id: '{{ dimmer }}'

Again, i want to provide a way to make the variable name a template so i dont have one script per dimmer :slight_smile: but this seems to work as intended! Now my lights remember their dimmer values.

hope this helps someone else out there… feedback welcome… once i have the variable name template figured out ill post that too.

1 Like

Ok i managed to get the variable templating to work. Again, there may be a better way to do this but this is working for me.

First off im creating a series of variables ahead of time and i just gave them numeric names:

var:
  one:
    initial_value: 100
  one:
    initial_value: 100
  two:
    initial_value: 100
  three:
    initial_value: 100
  four:
    initial_value: 100
  five:
    initial_value: 100
  six:
    initial_value: 100
  seven:
    initial_value: 100
  eight:
    initial_value: 100
  nine:
    initial_value: 100
  ten:
    initial_value: 100
  eleven:
    initial_value: 100
  twelve:
    initial_value: 100
  thirteen:
    initial_value: 100
  fourteen:
    initial_value: 100
  fifteen:
    initial_value: 100
  sixteen:
    initial_value: 100
  seventeen:
    initial_value: 100
  eighteen:
    initial_value: 100
  nineteen:
    initial_value: 100
  twenty:
    initial_value: 100

Then i added a variable name to the variables of the card itself. So here’s a sample:

        - type: custom:button-card
          entity: light.tom_s_office_main_lights
          name: Office Main Lights
          icon: custom:ha-ceiling-lamp
          template:
            - dimmer_light_tom
          variables:
            secondary_info: "Lutron Caseta"
            variable_name: "ten"

The tap action then has that variable in it:

    tap_action:
      action: call-service
      service: script.lutron_dimmer
      service_data:
        dimmer: >
          [[[ return variables.entity_id ]]]
        variable_name: >
          [[[ return variables.variable_name ]]]

And the script now takes that variable name and uses it:

lutron_dimmer:
  fields:
    dimmer:
      description: Lutron dimmer to store and restore dimming value
    variable_name:
      description: Name of variable to use for dimmer
  sequence:
    - choose:
      - conditions:
        - condition: template
          value_template: '{{ is_state( dimmer, "off") }}'
        sequence:
        - service: light.turn_on
          data:
            brightness: "{{ states('var.' + variable_name) }}"
          target:
            entity_id: '{{ dimmer }}'
      default:
        - service: var.set
          target:
            entity_id: '{{ "var." + variable_name }}'
          data:
            value: "{{ state_attr(dimmer , 'brightness') }}"
        - service: light.turn_off
          data: {}
          target:
            entity_id: '{{ dimmer }}'

Its working as expected.

Again it would be great to be able to dynamically create variables on the fly and use them but hey, at least this is working.

hey, thanks for this, i am new to home-assistant, so was wondering, when you say

“When that button is pushed”

you mean the light button on the lutron ?

Thanks
S