Automation Troubleshooting

I have a pair of automations to control my thermostat set temp, one triggers when a group of devices switches from home to not_home and the other triggers when the same group goes from not_home to home. The problem is, even though I can see it fire it is no longer setting the temperature. I need some help figuring out what the issue is.

Here is the automation when the devices leave.

alias: Temp Set Away
trigger:
  platform: state
  entity_id: group.thermostatdevices
  from: 'home'
  to: 'not_home'
action:
  - service: climate.set_temperature
    data_template:
      entity_id: all
      temperature: >-
        {% if is_state_attr('climate.sensi', 'operation_mode', 'heat') -%}
          {{ state_attr('climate.sensi', 'temperature')|int(71) - 3 }}
        {% elif is_state_attr('climate.sensi', 'operation_mode', 'cool') -%}
          {{ state_attr('climate.sensi', 'temperature')|int(73) + 3 }}
        {%- else -%}
          {{ state_attr('climate.sensi', 'temperature')|int(73) }}
        {%- endif %}
      target_temp_low: >-
        {% if is_state_attr('climate.sensi', 'operation_mode', 'auto') -%}
          {{ state_attr('climate.sensi', 'target_temp_low')|int(71) - 3 }}
        {%- else -%}
          {{ state_attr('climate.sensi', 'target_temp_low')|int(71) }}
        {%- endif %}
      target_temp_high: >-
        {% if is_state_attr('climate.sensi', 'operation_mode', 'auto') -%}
          {{ state_attr('climate.sensi', 'target_temp_high')|int(73) + 3 }}
        {%- else -%}
         {{ state_attr('climate.sensi', 'target_temp_low')|int(73) }}
        {%- endif %}
  - service: notify.pushbullet
    data:
      title: 'Thermostat set temp changed due to devices leaving home'
      message: ''

Here is the automation when they return

alias: Temp Set Home
trigger:
  platform: state
  entity_id: group.thermostatdevices
  from: 'not_home'
  to: 'home'
action:
  - service: climate.set_temperature
    data_template:
      entity_id: all
      temperature: >-
        {% if is_state_attr('climate.sensi', 'operation_mode', 'heat') -%}
          {{ state_attr('climate.sensi', 'temperature')|int(71) + 3 }}
        {% elif is_state_attr('climate.sensi', 'operation_mode', 'cool') -%}
          {{ state_attr('climate.sensi', 'temperature')|int(73) - 3 }}
        {%- else -%}
          {{ state_attr('climate.sensi', 'temperature')|int(73) }}
        {%- endif %}
      target_temp_low: >-
        {% if is_state_attr('climate.sensi', 'operation_mode', 'auto') -%}
          {{ state_attr('climate.sensi', 'target_temp_low')|int(71) + 3 }}
        {%- else -%}
          {{ state_attr('climate.sensi', 'target_temp_low')|int(71) }}
        {%- endif %}
      target_temp_high: >-
        {% if is_state_attr('climate.sensi', 'operation_mode', 'auto') -%}
          {{ state_attr('climate.sensi', 'target_temp_high')|int(73) - 3 }}
        {%- else -%}
         {{ state_attr('climate.sensi', 'target_temp_low')|int(73) }}
        {%- endif %}
  - service: notify.pushbullet
    data:
      title: 'Thermostat set temp changed due to devices arriving home'
      message: '' 

If you are using Home assistant version 0.96.X then the attribute is no longer called operation_mode. It’s now called hvac_mode.

It’s mentioned in the Release Notes:

operation_mode has been renamed to hvac_mode to emphasize what the mode is for.

Yet it didn’t thrown an error. :frowning:

A few things jump out to me:

  • entity_id: all needs to be entity_id: climate.sensi.
  • heat, cool, and auto are typically states so is_state_attr can just be is_state, and , 'operation_mode' can be omitted.
  • And since temperature: / target_temp_low: / target_temp_high: are already defined as the attributes being manipulated within the data_template: there’s no need for them to be included in the if statements.
  • Additionally, since the integer provided (int(x)) is not a variable there’s no need for math to be performed within the template.
  • Given the previous reasons, there’s no need for the template results to also be a template.

IE:

...
action:
  - service: climate.set_temperature
    data_template:
      entity_id: climate.sensi
      temperature: >-
        {% if is_state('climate.sensi', 'heat') -%}
          74
        {% elif is_state('climate.sensi', 'cool') -%}
          70
        {%- else -%}
          73
        {%- endif %}
...

Edit: Fixed typo

It was actually setup to calculate the set temps when leaving home or coming home in case we changed the default set temp.

I have broken it apart into 6 individual automations which for right now are working as expected.

Oh, OK. Then you’d want:

...
action:
  - service: climate.set_temperature
    data_template:
      entity_id: climate.sensi
      temperature: >-
        {% if is_state('climate.sensi', 'heat') -%}
          {{ state_attr('climate.sensi', 'temperature')|int() +3 }}
        {% elif is_state('climate.sensi', 'cool') -%}
          {{ state_attr('climate.sensi', 'temperature')|int() -3 }}
        {%- else -%}
          {{ state_attr('climate.sensi', 'temperature')|int() }}
        {%- endif %}
...

Why should it throw an error? The is_state_attr function gracefully handles non-existent entities and attributes.

For example, my system doesn’t contain an entity called climate.sensi (with a fictitious attribute called zilch) yet is_state_attr doesn’t throw an error and returns False.

Screenshot%20from%202019-08-01%2022-08-50

So if you are using 0.96, your automation is referencing an attribute (operation_mode) whose name has changed (hvac_mode).

Quick hijack - how are integrating Sensi? ST? Wink? Something else? Thanks!