Sensor for Previous State of a Lamp

Hello everyone,

I have made a sensor to know the previous state of a lamp.

- trigger:
    - platform: state
      entity_id:
        - light.slaapkamer_mb_bed_groep_alles
    sensor:
      - name: Vorige Waarde Bedlicht
        state: |
            {{ trigger.from_state.state }}

Now my problem is that after starting HA, the state is ‘unknown’. How do I solve this?

Thank you for your reply.

Exclude it from the trigger:

template:
  - triggers:
      - trigger: state
        entity_id:
          - light.slaapkamer_mb_bed_groep_alles
        not_to:
          - unknown
          - unavailable
    sensor:
      - name: Vorige Waarde Bedlicht
        state: "{{ trigger.from_state.state }}"

EDIT: Normalized and adjusted for current config variables.

Thank you for your reply. I had already tried this. But it didn’t work. I will try again.

Oh dear, strange, it works now. Sorry to say this, but your indentation is indeed wrong; I have adjusted it.
But in any case, thank you very much for the help again. :+1: :innocent:

That was just copied directly from your original post, but I’ll fix it :smile:

  - trigger:
      - platform: state
        entity_id:
          - light.slaapkamer_mb_bed_groep_alles
        not_to:
          - unknown
          - unavailable
    sensor:
      - name: Vorige Waarde Bedlicht
        state: |
            {{ trigger.from_state.state }}

  - trigger:
    - platform: state
      entity_id:
        - light.slaapkamer_mb_naaktbar_groep_alles
      not_to:
          - unknown
          - unavailable
    sensor:
      - name: Vorige Waarde Naaktbar
        state: |
            {{ trigger.from_state.state }}

Haha, okay, but something still isn’t right. I have restarted now with the following result.

Have you changed the light states since reloading the edited and/or new sensor config…? It can’t know something that happened prior to it being configured. This is the expected behavior any time you edit the config of a trigger-based sensor.

Here’s an example of one I use to know the last state of my thermostat. It avoids all other conditions but heat and cool and it survives restarts.

  - triggers:
      - trigger: state
        entity_id: climate.thermostat
    conditions:
      - condition: template
        value_template: >
          {{ states('climate.thermostat') in ['heat', 'cool'] }}
    sensor:
      - name: Thermostat Last Mode
        unique_id: thermostat_last_mode
        state: >
          {{ states('climate.thermostat') if states('climate.thermostat') in ['heat', 'cool'] else this.state }}

I turned the lights off and on and then restarted. Then they are unknown again!

Thank you for your response. Then I have the current value, not the previous value!

Isn’t the previous state of a light the opposite of the current state?

Yes, normally

I am a little confused here.

If you are using a sensor, I don’t think its possible to record the entire state of the light since the state class of sensors is different to lights (you will just get the on/off state).

Given you only want to update the sensor if the state actually changes.

And that you don’t want unknown or unavailable states.

The only time that the last state would be different to “NOT current state” would be if someone changed the light state while HA is turned down/restarting.

Is that the use case you are trying to handle or are you trying to achieve something different?

Thank you for your response. The intention is simply that the state of the sensor remains the same after startup as before startup.
So if the state of the sensor is ‘on’ when shutting down HA, I would like it to be ‘on’ after startup as well. And I am unable to get this to work. It is possible that this is not possible!

You need a sensor that is restored after restart, which requires a sensor with a unit of measurement.
A light does not have a unit of measurement, so you need to be a bit creative and maybe use a voltage template sensor and have 1 as on and 0 as off.

Beware that the sensor will still be unavailable a few seconds in the boot up, until the restoring occurs, so any automations need to take this into account.

Triggered template entities are also restored.

Drew’s solution above should work, once the light is changed the state will be restored.

Drew’s solution needs to use not_from instead of not_to:

template:
  - triggers:
      - trigger: state
        entity_id:
          - light.slaapkamer_mb_bed_groep_alles
        not_from:
          - unknown
          - unavailable
    sensor:
      - name: Vorige Waarde Bedlicht
        state: "{{ trigger.from_state.state }}"

Nope. They don’t want the state to be recorded when it changes to unknown/unavailable. Changing from unknown/unavailable to a valid state should be recorded.

I realize it’s counter-intuitive, but this is how it needs to be done when using trigger.from_state.state

Otherwise, in the original code, the sensor triggers when it changes from unknown to on and then the template is evaluated which will render as unknown. That is why this template sensor is not working.

I suppose the alternative is to leave it as not_to but then replace the template to trigger.to_state.state

1 Like

Ah. I see, yeah this would be the way I’d do it: