Reduce temperature by two degrees in absence

I would like the temperature of all radiators to be lowered by 2 degrees when no person is left in the house.

How can I lower the value I read from the follow variable by two degrees in an automation?

value: ‘{{ states.climate.thermostat_1.attributes.temperature | float }}’

After someone is in the house again, I want the temperature to be set to the previous value, for this I either have the variable input_number.old_temp_xxxx which is updated whenever a radiator is set automatically. Or I just say “value: ‘{{ states.climate.thermostat_1.attributes.temperature | float }}’ + 2 degrees”.

I just don’t know how to pass the command to be set down or up two degrees by using the above variable.

Please use the state_attr(…) notation instead of states.xxxx, as explained here.

Then just subtract 2 at the end.

value: "{{ state_attr('climate.thermostat_1', 'temperature') | float - 2 }}"
1 Like

Thank you, I didn’t know, that I have to avoid states.sensor.temperature, when possible.

Hi @OlliVa, you should share your full automation here or as a blueprint. :blush:

I have a special way…
Because I use window open/close sensors, I have in my configuration.yaml such entries:

input_number:
  old_temp_badezimmer:
    name: current desired temperature with closed window
    min: 7
    max: 35
    step: 0.5
    mode: slider

I set the value for this variable in every of my heating automations, i.e.:

- id: '16xxxxx141803'
  alias: Heizung Badezimmer Morgen Wochenende 07.10
  description: ''
  trigger:
  - platform: time
    at: 07:10
  condition:
  - condition: state
    entity_id: binary_sensor.workday_sensor
    state: 'off'
  action:
  - service: climate.set_temperature
    data:
      temperature: 25
    entity_id: climate.thermostat_2
  - delay: 00:00:06
  - service: input_number.set_value
    data:
      value: '{{ state_attr(''climate.thermostat_2'', ''temperature'') | float }}'
    entity_id: input_number.old_temp_badezimmer
  mode: single

(When I open the window of the bathroom, I set the temperature to 7°C and when I close it, I use the variable input_number.old_temp_badezimmer to set the temperature back).

Since this variable, which shows me the currently valid set temperature, exists anyway, I also use it as a setting for an automation when someone arrives back home:

- id: '16xxxx8062611'
alias: Heizung Badezimmer bei Abwesenheit
  description: ''
  trigger:
  - platform: state
    entity_id: binary_sensor.people_home
    to: 'off'
    for: 00:05:00
    from: 'on'
  condition: []
  action:
  - service: climate.set_temperature
    entity_id: climate.thermostat_2
    data:
      temperature: '{{ state_attr(''climate.thermostat_2'', ''temperature'') | float
        - 2 }}'
  mode: single

- id: '1608xxxx306167'
  alias: Heizung Wohnzimmer bei ANwesenheit
  description: ''
  trigger:
  - platform: state
    entity_id: binary_sensor.people_home
    from: 'off'
    to: 'on'
  condition: []
  action:
  - service: climate.set_temperature
    entity_id: climate.thermostat_2
    data:
      temperature: '{{ states.input_number.old_temp_badezimmer.state }}'
  mode: single

BTW: i set the sensor “binary_sensor.people_home” in my configuration yaml:

- platform: template
   sensors:
     people_home:
       friendly_name: 'Jemand zuhause?'
       device_class: presence
       value_template: >
         {{ is_state('person.joachim_s', 'home') or
            is_state('person.joachim_s', 'Zu Hause') or
            is_state('person.joachim_s', 'zuhaus') or
            is_state('person.angela_m', 'home') or
            is_state('person.angela_m', 'Zu Hause') or
            is_state('person.angela_m', 'zuhaus') }}

Did I miss this? My templates (defined in configuration.yaml, not in automations) were created just following others’ examples. I’m not sure I even understand what the linked document is trying to say. Here are two templates I use. Is there a better way?

  - platform: template
    sensors
      nws_currenttemp:
        value_template: "{{states.weather.wxyz_hourly.attributes.temperature}}"
        unit_of_measurement: '°F'

      switch_east_sump_pump_amps:
        friendly_name_template: "{{ states.switch.sump_pump.name}} Current"
        value_template: '{{ states.switch.sump_pump.attributes["current_a"] | float }}'
        unit_of_measurement: 'A'

Did you read the warning in the docs I linked? It says that states.xxx can lead to errors on startup and instead use states(…) to access the state of the entity or state_attr(…) to access the state of an attribute of the entity.

This:

" {{ states.weather.wxyz_hourly.attributes.temperature }}"

Becomes (you access the attribute temperature of the entity weather.wxyz_hourly:

"{{ state_attr('weather.wxyz_hourly', 'temperature') }}

Both notations are valid, but the second one doesn’t lead to errors when the entity is not available.

1 Like

Thank you, yes, I got that part. And I’ve seen the errors it mentions. I just wanted to make sure I understood the syntax, since I’m still trying to grasp the underlying meaning of these two different methods.

I tried the “new” notation, and it seems to work great, thank you!

1 Like

I’m going through all my templates, and found some more which used states. instead of state_attr. Here’s one example:

{{ states.switch.sump_pump.attributes["current_a"] | float }}

I changed it to this:

{{ state_attr('switch.sump_pump', 'current_a') | float }}

That works in Developer Tools / Templates, so I put it in configuration.yaml like this:

  - platform: template
    sensors:
      switch_test_sump_pump_amps:
        value_template: '{{ state_attr('switch.sump_pump', 'current_a') | float }}'
        unit_of_measurement: 'A'

This returns an error when I use Check Configuration:

Error loading /config/configuration.yaml: while parsing a block mapping
in "/config/configuration.yaml", line 133, column 9
expected <block end>, but found '<scalar>'
in "/config/configuration.yaml", line 133, column 41

I verified there are no errant tabs and there are the correct number of spaces. I replaced just the “new” template (between the curly braces) with the “old” one and that checks OK, so it’s got to be just that one part, nothing before or after.

Thank you again for your patience, I’m sure it’s something obvious I’m not seeing.

Replace the sinqle quotes outside the curly brackets with double quotes.

Either use single quotes outside the cury brackets and double quotes inside the curly brackets or vice versa. Otherwise it will not work.

1 Like

Doh! That’s embarrassing. I must have stared at that for 10 minutes and never saw it.

Works now, thank you!!!

Also, we should probably stop hijacking this thread and move future discussions over to one that was created specifically for this question: