Setting thermostat values via automation, possible to have if statements in the action?

Hi guys – I’m trying to set an automation that watches the outside temperature at night and sets my bedroom temp accordingly. However, my girlfriend and I travel for work, and I wanted to have a simple way to set a different temperature for when she or I are away. She gives off a lot of heat at night (set to low temp of 68 when we are both home). I tried the below, but it doesn’t work … is there a better approach? Trying to keep things simple! Thank you…

- alias: "Master - Temperature at Night (Winter Normal)"
  trigger:
    platform: time
    from: "20:00:00"
    to: "07:00:00"
  condition:
    - condition: numeric_state
      entity_id: 'weather.dark_sky'
      value_template: '{{ state.attributes.temperature | int }}'
      below: '50'
  action:
    - service: climate.set_temperature
      data:
        entity_id: climate.Master
        temperature: >
        {% if is_state('binary_sensor.girl', 'away') and is_state('binary_sensor.boy', 'home') %}
        72
        {% elseif is_state('binary_sensor.girl', 'home') and is_state('binary_sensor.boy', 'away')%}
        73
        {% elseif is_state('binary_sensor.girl', 'home') and is_state('binary_sensor.boy', 'home')%}
        68
        {% elseif is_state('binary_sensor.occupiedstatus', 'away') %}
        66
        {% else %}
        60
        {% endif %}
        operation_mode: heat

I highly recommend that you format your code to make it easier for others to analyze it.

I don’t believe you can use to and from to specify a range for a time trigger.

Triggers are documented here:

I suggest setting the trigger to the outdoor temperature when it decreases below a threshold value (50 F). In addition, it should remain below the threshold for a short period of time (5 minutes) to avoid needless triggering when the temperature hovers around the threshold (meaning when it fluctuates above and below the threshold).

The condition should specify the time range when the automation is desired. Home Assistant uses midnight as boundary therefore, to specify a range of 20:00 to 7:00, we use two periods. The first period is between 20:00 and midnight. The second period is between midnight and 7:00. The time must fall in the first period OR the second period.

The action calls the climate component’s set_temperature service. The desired temperature depends on who (if anyone) is home.

automation:
  trigger:
    platform: numeric_state
    entity_id: weather.darksky
    value_template: '{{ state.attributes.temperature | int }}'
    below: 50
    for:
      minutes: 5
  condition:
    condition: or
    conditions:
      - condition: time
        after: '20:00'
      - condition: time
        before: '07:00'
  action:
    - service: climate.set_temperature
      entity_id: climate.master
      data_template: >
        {% set girlHome = is_state('binary_sensor.girl', 'home') %}
        {% set boyHome = is_state('binary_sensor.boy', 'home') %}
        {% if boyHome and not girlHome %}
          72
        {% elif girlHome and not boyHome %}
          73
        {% elif girlHome and boyHome %}
          68
        {% elif not girlHome and not boyHome %}
          66
        {% else %}
          60
        {% endif %}

I have not tested this automation so it may require additional adjustments to make it work properly.


EDIT
Corrected the automation using the instructions provided by @finity.
EDIT 2
Final corrected version is posted below.

you need to also make sure you use “data_template:” instead of “data:”.

And the “else if” isn’t “elseif” it’s “elif”.

If you make those changes to your action template and using the advice by @123 for the trigger and conditions then your original code in the “action:” section may work.

Thanks. I made the adjustments, but receive this errror:

Error loading /config/configuration.yaml: while scanning for the next token
found character '%' that cannot start any token
  in "/config/automations.yaml", line 22, column 10

Here is my current automation rule:

automation:
  trigger:
    platform: numeric_state
    entity_id: weather.darksky
    value_template: '{{ state.attributes.temperature | int }}'
    below: 50
    for:
      minutes: 5
  condition:
    condition: or
    conditions:
      - condition: time
        after: '20:00'
      - condition: time
        before: '07:00'
  action:
    - service: climate.set_temperature
      entity_id: climate.master
      data_template:
        {% set girlHome = is_state('binary_sensor.girl', 'home') %}
        {% set boyHome = is_state('binary_sensor.boy', 'home') %}
        {% if boyHome and not girlHome %}
          72
        {% elif girlHome and not boyHome %}
          73
        {% elif girlHome and boyHome %}
          68
        {% elif not girlHome and not boyHome %}
          66
        {% else %}
          60
        {% endif %}

you have to put a > after the data_template: line:

data_template: >
  {% set girlHome = is_state('binary_sensor.girl', 'home') %}

Thanks for the corrections! I’ve not mastered this yet and appreciate the feedback.

Getting closer! I now get this error:

Invalid config for [automation]: expected a dictionary for dictionary value @ data['action'][0]['data_template']. Got None.

I also noticed that you are using a binary sensor and comparing that to be in the state “home”. That can’t work. a binary sensor can only be off or on. either change the sensor or change the state comparison.

yeah no problem.

neither have I! :smile:

@ajgnet please try this version. It may still not be perfect but I think it will, at the very least, pass the configuration check (I hope).

I’ve changed the data_template section. The template applies exclusively to the temperature parameter.

I also changed the is_state test. As per finity’s observation you are testing a binary_sensor for states it does not have (home and away). I changed it to test for on assuming this is the state representing someone is at home. If this is incorrect, you will need to change it.

I also added alias: to the automation and operation_mode: to the data_template. I’m not sure if 'heat' needs to be delimited with quotes.

automation:
- alias: 'Temperature Adjuster'
  trigger:
    platform: numeric_state
    entity_id: weather.dark_sky
    value_template: '{{ state.attributes.temperature | int }}'
    below: 50
    for:
      minutes: 5
  condition:
    condition: or
    conditions:
      - condition: time
        after: '20:00'
      - condition: time
        before: '07:00'
  action:
    - service: climate.set_temperature
      entity_id: climate.master
      data_template:
        temperature: >
          {% set girlHome = is_state('binary_sensor.girl', 'on') %}
          {% set boyHome = is_state('binary_sensor.boy', 'on') %}
          {% if boyHome and not girlHome %}
            72
          {% elif girlHome and not boyHome %}
            73
          {% elif girlHome and boyHome %}
            68
          {% elif not girlHome and not boyHome %}
            66
          {% else %}
            60
          {% endif %}
          operation_mode: 'heat'

EDIT
Corrected sensor.darksky to sensor.dark_sky as per @ajgnet post below.

That worked! I had to rename “weather.darksky” to “weather.dark_sky” and we were good to go. Thanks so much!

Great! Glad to hear it works (and it was 99.9% correct)! :slight_smile:

I updated the code in my original post with your correction for sensor.dark_sky. Hopefully the example will help others who have a similar requirement.