Saving and restoring temperatures

Hi,
I was looking for a configuration that saves temperature settings, lowers temperature for the night and the weekend (in an office building) and restores it in the morning. And most importantly: retains saved temperatures after HA restarts (so is different than using scene create/restore).

I found a solution here that is almost perfect (creating sensor template that saves temperatures of all devices having the same label):

template:
  - trigger:
      - platform: event
        event_type: save_7_7_8_20
    sensor:
    - unique_id: d29aa7e2-62e1-4836-b94c-657666c9c4bb
      name: save_7_7_8_20
      state: data
      attributes:
        data: > 
          {% set entities = 
                  label_devices('7_7_8_20') 
                  | map('device_entities')
                  | sum(start=[]) 
                  | select('match', 'climate') 
                  | list  
                 %}
          {% set ns = namespace(items=[], attrs=[]) %}
          {% for obj in entities | expand %}
            {% set ns.attrs = [('state', obj.state)] %}
            {% if obj.domain == 'climate' %}
              {% set ns.attrs = ns.attrs + [('temperature', obj.attributes.temperature)] %}
            {% endif %}
            {% set ns.items = ns.items + [ (obj.entity_id, dict.from_keys(ns.attrs)) ] %}
          {% endfor %}
          {{ dict.from_keys(ns.items) }}

Then I save in an automation using:

          - event: save_7_7_8_20

and restore them using:

          - action: scene.create
            data:
              scene_id: restore_7_7_8_20
              entities: |
                {{ state_attr('sensor.save_7_7_8_20', 'data') }}
          - action: scene.turn_on
            target:
              entity_id: scene.restore_7_7_8_20

Everything worked until one of the sensors was unreachable. Instead of following state:

data: >-
  {'climate.p_203': {'state': 'heat', 'temperature': 23.0},
  'climate.hall_2': {'state': 'heat', 'temperature': 15.0},
  'climate.hall': {'state': 'heat', 'temperature': 16.5}}

I got:

data: >-
  {'climate.p_203': {'state': 'heat', 'temperature': 23.0},
  'climate.hall_2': {'state': 'unavailable', 'temperature': undefined},
  'climate.hall': {'state': 'heat', 'temperature': 16.5}}

And the restore function didn’t restore any of the climate settings in that group. Subsequent ‘save’ event overwrote that state with night temperatures (because all thermostats stayed in that state).

And now my question - how to rewrite the save function so that instead of unavailable/undefined there would be temperatures saved on the day before?

P.

Can’t you just have a sensor with

If weekend 
  12 
Elif workday and 7-18
   20

Then trigger on this sensor, and action set temperature?

I want to save the changes people made manually using thermostat panel, so I don’t want to set a constant value for the day.

How about saving it to input numbers?
That survives restarts and can be set by automating the set temperature change

This saving in states is sufficient I think - it works so far. My main problem is that I don’t know that scripting language well enough to write appropriate code - to check for ‘unavailable’ values and replace them by values extracted from previous states

If you use an input number then it can’t be unavailable. The action to set it to unavailable will fail and it will keep the previous state

I see. OK, I’ll give it a try, thanks :slight_smile: