Generic Thermostat, how restore original temperature after n minutes?

Hello

On generic thermostat, how can restore original temperature (target_temp) after n minutes after change temperature?

Exist an event raised from change temperature (target_temp) ?

How to know when the target_temp changed?

Although I have a climate entity, it’s using a different platform (Nest), so it would be helpful if you could post what attributes your climate entity has. But assuming it has one named target_temp…

Do you want to handle the target temp being changed by an automation, or via the UI, or both?

Yes, every state change (even if it’s just an attribute that changed) of every entity (well, almost every entity) causes a state_changed event. It is possible to create an automation that triggers on a particular state_changed event. The easiest way to do that is to use a state trigger. E.g.:

trigger:
  platform: state
  entity_id: climate.my_thermostat

Will trigger whenever climate.my_thermostat’s state, or any of its attributes, change. You could then use a condition that filters out all but changes in the target_temp attribute. Then, assuming that has changed, you’ll have the old value in the automatically created trigger variable that you can store in an input_number, which you can use later to restore target_temp.

A complication is, what should happen if, during the n minute wait, target_temp changes again? I’ll assume the simpler case of ignoring that, and n minutes after the original change, set target_temp back to what it was.

So, with all that in mind, here is one way you could do this:

input_number:
  prev_target_temp:
    name: Previous target temperature
    min: -100
    max: 200

timer:
  target_temp:
    duration: '00:05:00'
  
automation:
  - alias: Target temp changed, save previous value and start timer
    trigger:
      platform: state
      entity_id: climate.my_thermostat
    condition:
      - condition: template
        value_template: >
          {{ trigger.to_state.attributes.target_temp !=
             trigger.from_state.attributes.target_temp }}
      - condition: state
        entity_id: timer.target_temp
        state: idle
    action:
      - service: timer.start
        entity_id: timer.target_temp
      - service: input_number.set_value
        entity_id: input_number.prev_target_temp
        data_template:
          value: "{{ state_attr('climate.my_thermostat', 'target_temp') }}"

  - alias: Timer finished, restore target_temp
    trigger:
      platform: event
      event_type: timer.finished
      event_data:
        entity_id: timer.target_temp
    action:
      service: climate.set_temperature
      entity_id: climate.my_thermostat
      data_template:
        temperature: "{{ states('input_number.prev_target_temp') }}"

@pnbruckner thank you, but do not work

- alias: Target temp changed, save previous value and start timer
  trigger:
    platform: state
    entity_id: climate.camera
  condition:
    - condition: template
      value_template: >
        {{ trigger.to_state.attributes.temperature !=
           trigger.from_state.attributes.temperature }}
    - condition: state
      entity_id: timer.target_temp
      state: idle
  action:
    - service: timer.start
      entity_id: timer.target_temp
    - service: input_number.set_value
      data_template:
        entity_id: input_number.prev_target_temp
        value: "{{ state_attr('climate.camera', 'temperature') }}"

because the automation set the current tempertature of climate and not the initial
value: “{{ state_attr(‘climate.camera’, ‘temperature’) }}”

As I said, I don’t use this climate platform so I’m not familiar with its attributes. You originally said you wanted to save and restore the target_temp attribute, but you changed the automation to use the temperature attribute. Which one is right, or is it yet a different attribute?

If you’ve figured out how to make it work, great. If not, please provide details on the entity’s attributes, what they mean, and which one or ones you want to save and restore.

Ok, I looked at the climate component code and the generic thermostat platform code. The attribute for target temperature is indeed ‘temperature’, whereas the attribute for current temperature is ‘current_temperature’. (I guess it’s the same as for my Nest thermostat.)

So after you changed the name of the attribute, do the automations work for you? Or do you still need help?

What I would like to do and:
The thermostat is set at 19 ° C, I start changing the value and the set the temperature at 20 ° C, what I would like to do is that after x minutes it would return to 19 ° C.

Thanks to you I solved the problem with the use of the timer and state trigger, the reference value I set statically

I did make a mistake in my original solution (besides getting the name of the attribute wrong.) In the first automation’s action, I set input_number.prev_target_temp to the current value of climate.my_thermostat. That is actually the new value. I should have set it to trigger.from_state.attributes.temperature (which is what I meant to do), which is the old value (before it was changed.)

So, taking that into account, and the correct entity_id’s, I think the solution should be:

input_number:
  prev_temperature:
    name: Previous target temperature
    min: -100
    max: 200

timer:
  temperature:
    duration: '00:05:00'
  
automation:
  - alias: Target temp changed, save previous value and start timer
    trigger:
      platform: state
      entity_id: climate.camera
    condition:
      - condition: template
        value_template: >
          {{ trigger.to_state.attributes.temperature !=
             trigger.from_state.attributes.temperature }}
      - condition: state
        entity_id: timer.temperature
        state: idle
    action:
      - service: timer.start
        entity_id: timer.temperature
      - service: input_number.set_value
        entity_id: input_number.prev_temperature
        data_template:
          value: "{{ trigger.from_state.attributes.temperature }}"

  - alias: Timer finished, restore target temp
    trigger:
      platform: event
      event_type: timer.finished
      event_data:
        entity_id: timer.temperature
    action:
      service: climate.set_temperature
      entity_id: climate.camera
      data_template:
        temperature: "{{ states('input_number.prev_temperature') }}"

Now the question arises, how to set the target temperature in the first place, since every time it is changed, this automation will change it back to what it was?

The easiest way is to temporarily turn the first automation off, set the desired target temperature, then turn the automation back on.

Another way would be to add an input_boolean and add a condition to the first automation that that input_boolean must be on. This would do the same thing as the previous suggestion, but might be nicer from the UI standpoint.

Another way would be to have an input_number as the desired target temp, and then change the first automation to instead trigger whenever climate.camera’s temperature attribute changed to a value that was not equal to the input_number.

Anyway, just some things to consider. Let me know if you’d any additional help.

I have 2 automatisms, one for the day and one for one night, both have a temperature and a trigger time.
I use that temperature (depending on when the climate temperature changes) as a return value.

So I will create an input_boolean to avoid that the automation that return the original value trigger again on his action.

When i complete the code, i’ll post here.

@pnbruckner hello again

this is my solution