WTH is there no last_state for templating/ automations?

I know there is states.sensor.entity.last_changed but sometimes it would be nice to get the actual state the entity had before the change or the last time an entity changed to a specific state.

Example:

  • An awesome 3d printer integration exposes the remaining time of a currently running 3d print in minutes (e.g. sensor.printer_time_remaining).
  • There is also sensor.printer_state with the printers current state (some of the possible states are failed, finish, idle and running)

Idea:

  • Every now and then a 3d printer needs maintenance (cleaning the printing chamber, cleaning and greasing rods, bearings, etc.), which heavily depends on usage.
  • Therefore it would be nice to track the total printing time since the last maintenance.
  • To active this, one could create an automation which adds the sensor.printer_time_remaining to an input_number helper whenever sensor.printer_state changes to running.

Problem:

  • When the print fails (e.g. sensor.printer_state changes to failed) the previously added time has to be reduced again.
  • As soon as the print fails the necessary information isn’t available anymore since the remaining time changed to 0.
  • If sensor.printer_time_remaining.last_state was available, one could use this to instead of crating an additional helper to temporarily store said time.
  • Since the information needed is already available (e.g. in the sensors history), creating helpers seems even more… well… what the heck?!

{{ this.state }} is the state of the entity before the change.

There’s a WTH open for the other thing. WTH there is no new last_* attribute that retains restart

Closing as your first request is already available and your second is a duplicate. Also as per the WTH FAQ please do not request more than one thing per WTH.

And in an automation, provided you use the correct triggers, {{ trigger.from_state }} is the prior state object.