Restore previous value of an entity

For several states of some entities I have used an input_select. Now for some lights there exists a state “aus” which means off. Other states may for instance be dunkel, rot, hell (dark, red, bright). The exact list of possible states depend on the specific light.

Now after turning the (any!) light off, I want it to restore previous state when turning on.

I have implemented a fitting SQL query

SELECT s.state
FROM states_meta m, states s
WHERE 1=1
AND m.metadata_id=s.metadata_id
AND LOWER(m.entity_id) = 'input_select.lichtname_zustand'
AND s.state != 'aus'
ORDER BY s.last_changed_ts DESC
LIMIT 1;

which I’d want to call in a script to activate the right (previous) state when turning the light on.

How can I call some script/template/sensor/whatever giving an entity name of the input_select.lichtname_zustand as parameter, execute that query for the given entity and return the result, meaning last state which is to be restored?

Or is there an easier way to implement this? But I do not want to explicitly store the last value which is to be restored, which would mean I would have to create this storage for every entity which may have several states.

Save the light state in a “Scene on the fly”. The restore the scene when you need it later. See: https://www.home-assistant.io/integrations/scene/#creating-scenes-on-the-fly

Hello Tom,
thank you for the hint.

You mean, I should use the name of the entity which should be stored as part of the name of the scene to be stored before turning the light off? Is it possible to provide the name of the scene by a template?

Just to add, I’m often using ZigBee bulbs behind a switch which is controlled by Shelly in detached mode. This is the reason why the standard methods usually aren’t appropriate in my case. One is supposed to do things using that switch. Not only turn light on and off, but also change modes.

For these I’m searching how to remember that state. But as mentioned not by managing by entity, but in a general way where you don’t have to do things and create more helpers if you add a light.

Currently if you turn on one of my lights by switching the switch, it will go to default mode which is the first state in the input_select list for that light. For every mode there is a scene. If you press it multiple times in less than a second, you can choose which state it should have. The wish is to restore the previous state if the switch was last toggled more than one second ago.

I’d like to generalize my logic for changing the switch state to be unique for all lights, like I did with activation. It makes no sense to have multiple copies of the same script or automation dealing with the light - and if you change one of them, you have to copy it to all of these, or you’ll search bugs forever.

As sample, here my automations for Flur EG:

alias: Flur EG Schalter
description: ""
triggers:
  - trigger: state
    entity_id:
      - binary_sensor.lichtschalter_flur_eg_input_0_input
    from: "on"
    to: "off"
  - trigger: state
    entity_id:
      - binary_sensor.lichtschalter_flur_eg_input_0_input
    from: "off"
    to: "on"
conditions: []
actions:
  - if:
      - condition: template
        value_template: >-
          {{ now() > states.input_select.flur_eg_zustand.last_changed +
          timedelta(seconds=1) }}
        enabled: true
      - condition: not
        conditions:
          - condition: state
            entity_id: input_select.flur_eg_zustand
            state: aus
        enabled: true
    then:
      - action: input_select.select_first
        data: {}
        target:
          entity_id: input_select.flur_eg_zustand
    else:
      - action: input_select.select_next
        data:
          cycle: true
        target:
          entity_id: input_select.flur_eg_zustand
mode: single

alias: Flur EG aktivieren
description: ""
triggers:
  - trigger: state
    entity_id:
      - input_select.flur_eg_zustand
  - trigger: state
    entity_id:
      - light.eg_flur_light_2
    from: unavailable
conditions: []
actions:
  - action: script.licht_zustand_aktivieren
    metadata: {}
    data:
      schalter: switch.lichtschalter_flur_eg_switch_0
      lampe: light.eg_flur_light_2
      liste: flur_eg
mode: restart

and the script “zustand aktivieren”

alias: Licht Zustand aktivieren
description: ""
sequence:
  - if:
      - condition: template
        value_template: "{{ is_state(lampe, 'unavailable') }}"
      - condition: template
        value_template: "{{ is_state(schalter, 'on') }}"
    then:
      - sequence:
          - action: switch.turn_off
            metadata: {}
            data_template: {}
            target:
              entity_id: "{{ schalter }}"
          - delay:
              hours: 0
              minutes: 0
              seconds: 2
              milliseconds: 500
          - action: switch.turn_on
            metadata: {}
            data_template: {}
            target:
              entity_id: "{{ schalter }}"
          - delay:
              hours: 0
              minutes: 0
              seconds: 0
              milliseconds: 250
    enabled: true
  - if:
      - condition: template
        value_template: "{{ is_state(schalter, 'off') }}"
    then:
      - action: switch.turn_on
        metadata: {}
        data_template: {}
        target:
          entity_id: "{{ schalter }}"
  - action: scene.turn_on
    target:
      entity_id: >-
        {{ 'scene.' ~ liste ~ '_' ~ states('input_select.' ~ liste ~ '_zustand')
        }}
fields:
  schalter:
    required: true
    description: Steuert Strom für ZigBee-Licht
  lampe:
    required: true
  liste:
     required: true