Help with inputs and variables within blueprint Trigger value template

Hello,

I’m trying to create a blueprint to use for various zigbee TRVs around the house.
The trigger is based on a template as i need to compare both the HVAC mode and the current/set temps of the TRV.

I’ve tried a number of different formats for the template (i’m no expert using templates) but i keep getting an error in the logs when i try to create an automation with it.

This is the whole blueprint yaml:

blueprint:
  name: Automatic Radiator HVAC Mode
  description: 'Set the heating mode of a radiator based on the current and set temperature.'
  domain: automation
  input:
    trv_entity:
      name: Radiator Device
      description: Radiator TRV Device
      selector:
        entity:
          integration: mqtt
          domain: climate
          multiple: false
    cold_tolerance:
      name: Cold Tolerance
      description: Amount of degrees lower than the set temp before TRV turns back on
      selector:      
        number:
          min: 0
          max: 2
          unit_of_measurement: 'degrees C'
    hot_tolerance:
      name: Hot Tolerance
      description: Amount of degrees higher than the set temp before TRV turns back off
      selector:    
        number:
          min: 0
          max: 2
          unit_of_measurement: 'degrees C'
mode: parallel
max_exceeded: silent

trigger_variables:
  var_trv_device: !input trv_entity
  var_hot_tolerance: !input hot_tolerance
  var_cold_tolerance: !input cold_tolerance

trigger:
  - platform: template
    value_template: >
      '
      {% set tempdiff = state_attr('var_trv_device',''current_temperature'') | float - state_attr('var_trv_device',''temperature'') | float %} 
      {% if is_state('var_trv_device', 'heat') and tempdiff > var_hot_tolerance %} 
      true 
      {% else %} 
      false 
      {% endif %}
      '
    id: turnoff
  - platform: template
    value_template: >
      ' 
      {% set tempdiff = state_attr('var_trv_device','current_temperature') | float - state_attr('var_trv_device','temperature') | float %} 
      {% if is_state('var_trv_device', 'auto') and tempdiff < cold_tolerance %} 
      true 
      {% else %} 
      false 
      {% endif %} 
      '
    id: turnon
    condition: []
action:
  - choose:
      - conditions:
          - condition: trigger
            id: turnoff
        sequence:
          - service: climate.set_hvac_mode
            data:
              hvac_mode: auto
            target:
              device_id: trv_entity
      - conditions:
          - condition: trigger
            id: turnon
        sequence:
          - service: climate.set_hvac_mode
            data:
              hvac_mode: heat
            target:
              device_id: trv_entity

The latest error with this version is:

Logger: homeassistant.components.automation
Source: components/automation/__init__.py:285
Integration: Automation (documentation, issues)
First occurred: 11:38:03 (7 occurrences)
Last logged: 11:38:03

Blueprint Automatic Radiator HVAC Mode generated invalid automation with inputs OrderedDict([('cold_tolerance', 1), ('trv_entity', 'climate.office_radiator'), ('hot_tolerance', 1)]): invalid template (TemplateSyntaxError: expected token ',', got 'current_temperature') for dictionary value @ data['value_template']. Got None
Blueprint Automatic Radiator HVAC Mode generated invalid automation with inputs OrderedDict([('trv_entity', 'climate.office_radiator'), ('cold_tolerance', 1), ('hot_tolerance', 1)]): invalid template (TemplateSyntaxError: expected token ',', got 'current_temperature') for dictionary value @ data['value_template']. Got None

Any help would be very much appreciated.

I think i have progressed this but i’m still left with one error.

This is my updated blueprint yaml:

blueprint:
  name: Automatic Radiator HVAC Mode
  description: 'Set the heating mode of a radiator based on the current and set temperature.'
  domain: automation
  input:
    trv_entity:
      name: Radiator Device
      description: Radiator TRV Device
      selector:
        entity:
          integration: mqtt
          domain: climate
          multiple: false
    tolerance_cold:
      name: Cold Tolerance
      description: Amount of degrees lower than the set temp before TRV turns back on
      selector:      
        number:
          min: 0
          max: 2
          unit_of_measurement: 'degrees C'
    tolerance_hot:
      name: Hot Tolerance
      description: Amount of degrees higher than the set temp before TRV turns back off
      selector:    
        number:
          min: 0
          max: 2
          unit_of_measurement: 'degrees C'
mode: single

trigger_variables:
  var_trv_device: '!input trv_entity'
  var_hot_tolerance: !input tolerance_hot
  var_cold_tolerance: !input tolerance_cold

trigger:
  - platform: template
    value_template: >
      '
      {% set tempdiff = state_attr(var_trv_device,'current_temperature') | float - state_attr(var_trv_device,'temperature') | float %} 
      {% if is_state(var_trv_device, 'heat') and tempdiff > var_hot_tolerance %} 
      true 
      {% else %} 
      false 
      {% endif %}
      '
    id: turnoff
  - platform: template
    value_template: >
      ' 
      {% set tempdiff = state_attr(var_trv_device,'current_temperature') | float - state_attr(var_trv_device,'temperature') | float %} 
      {% if is_state(var_trv_device, 'auto') and tempdiff < var_cold_tolerance %} 
      true 
      {% else %} 
      false 
      {% endif %} 
      '
    id: turnon
action:
  - choose:
      - conditions:
          - condition: trigger
            id: turnoff
        sequence:
          - service: climate.set_hvac_mode
            data:
              hvac_mode: auto
            target:
              device_id: !input trv_entity
      - conditions:
          - condition: trigger
            id: turnon
        sequence:
          - service: climate.set_hvac_mode
            data:
              hvac_mode: heat
            target:
              device_id: !input trv_entity

And this is the remaining error:

Logger: homeassistant.config
Source: components/blueprint/models.py:163
First occurred: 14:03:26 (13 occurrences)
Last logged: 14:03:26

Invalid config for [automation]: Missing input tolerance_cold, tolerance_hot (See /config/configuration.yaml, line 11).
Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/components/automation/config.py", line 113, in _try_async_validate_config_item
    validated_config = await async_validate_config_item(hass, config, full_config)
  File "/usr/src/homeassistant/homeassistant/components/automation/config.py", line 76, in async_validate_config_item
    return await blueprints.async_inputs_from_config(config)
  File "/usr/src/homeassistant/homeassistant/components/blueprint/models.py", line 304, in async_inputs_from_config
    inputs.validate()
  File "/usr/src/homeassistant/homeassistant/components/blueprint/models.py", line 163, in validate
    raise MissingInput(self.blueprint.domain, self.blueprint.name, missing)
homeassistant.components.blueprint.errors.MissingInput: Missing input tolerance_cold, tolerance_hot

An automation is now created when using the blueprint, but having a look at the traces when it runs shows that the trigger variables are not being converted back to their actual values in the trigger template, they are just being added as the variable names themselves.

Is this a valid use of inputs/trigger variables in a template trigger? Or are they not supported?

They are supported, but you have your first !input in quotes. Why are you using a template trigger? Why not use normal triggers and make a condition?

Thanks, I’ve since removed that but I still get the same problem.

My reason for using template triggers was that i wanted multiple conditions in the triggers all tied into 1 blueprint automation rather than multiple automations per TRV. It would also allow me to add further options into the blueprint and apply that quickly to all TRVs at once rather than adding new automations or editing all the existing ones.

The scenarios I’m trying to accomplish are:

  1. Trigger = HVAC mode set to heat and current temp is higher than set temp (taking into account the tolerance)
    Action = Set TRV to auto
  2. Trigger = HVAC mode set to auto and set temp is higher than current temp (taking into account the tolerance)
    Action = Set TRV to heat

Am I trying to make it too complicated?