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.