Set all generic_thermostats to sleep mode at from a single automation

I am trying to create an automation that, triggered by time, will set all my generic_thermostats that currently has preset_mode set to ‘none’ to preset_mode ‘sleep’.

I don’t can’t to create an automation for every generic_thermostat.

I thought that the below snippet would do the trick, but I seems like I can’t update attributes from templates. That is at least what I get from googling this for some hours now. So I am thinking that there might be some more clever way to get to what I want. Please enlighten me. :slight_smile:

I do for instance have guest rooms in my house that will have their generic_thermostats preset_mode set to ‘away’ when I don’t have guests. Those shouldn’t be changed. And of course ‘sleep’ should not be set when the no one is at home (all preset_modes are set to ‘away’).

{% for thermostat in states.climate %}
  {% if is_state_attr(thermostat.entity_id, 'preset_mode', 'none') %}
    {% set thermostat.attributes['preset_mode'] = 'sleep' %}
  {% endif %}
{% endfor %}