Blueprint can't read input

Hello,

I can’t find out why my blueprint doesn’t work.

The main goal of this automation is sun protection when it’s shining through a window.

blueprint:
  name: sun protecion
  description: close cover when sun is shining and open when stops
  domain: automation
  input:
    cover_entity:
      name: cover
      selector:
        entity:
          domain: cover
    sun_position_start:
      name: sun position start
      selector:
        number:
          min: 0
          max: 360
          unit_of_measurement: angle
    sun_position_end:
      name: sun position end
      selector:
        number:
          min: 0
          max: 360
          unit_of_measurement: angle
      
variables:
  cover_e: !input cover_entity
  var_e: 'var.' + cover_e[6:-4]
  protection_begin: !input sun_position_start
  protection_end: !input sun_position_end
  
mode: parallel

trigger:
- platform: template
  value_template: "{{ state_attr('sun.sun', 'azimuth') > protection_begin and state_attr('sun.sun', 'azimuth') < protection_end and states(var_e) != 1 }}"
  id: aktywuj
  for: '00:01:00'

- platform: template
  value_template: "{{ (state_attr('sun.sun', 'azimuth') < protection_begin or state_attr('sun.sun', 'azimuth') > protection_end) and states(var_e) == 1 }}"
  id: dezaktywuj
  for: '00:01:00'

action:
- choose:
##########################################################
 # close cover
########################################################## 
  - conditions:
      - condition: template
        value_template: "{{- states('binary_sensor.slonce_za_mocno_swieci') -}}"
    sequence:
      - data_template:
          entity_id: "{{ cover_e }}"
          position: 20
        service: cover.set_cover_position
      - data_template:
          entity_id: "{{ var_e }}"
          value: 1
        service: var.set
      - service: notify.mobile_app_mi11
        data_template:
          message: "{{ ON - cover_e }}"
          title: SunProtectionDebug
##########################################################
 # open cover
########################################################## 
  - conditions:
      - condition: template
        value_template: "{{- is_state('binary_sensor.slonce_za_mocno_swieci', 'off') -}}"
    sequence:
      - data_template:
          entity_id: "{{ cover_e }}"
        service: cover.open_cover
      - data_template:
          entity_id: "{{ var_e }}"
          value: 0
        service: var.set
      - service: notify.mobile_app_mi11
        data_template:
          message: "{{ OFF - cover_e }}"
          title: SunProtectionDebug

Automation created on this blueprint:

alias: sun protection - gym
description: ''
use_blueprint:
  path: homeassistant/sun_protection.yaml
  input:
    sun_position_end: 270
    sun_position_start: 100
    cover_entity: cover.gym_cover_121

I use a global variable var_e (var custom integration) to indicate if protection is active or not.

An error I received is unknown protection_begin and protection_end but they are defined in the variables section. What I did wrong?

I think it’s because the Template Trigger employs a regular variable as opposed to a trigger_variable. Change the variables used within the template to trigger_variables (but I have never tried it in a blueprint where the trigger_variable is defined with ! input).

For more information, see this Feature Request:

You are right, I’ve changed it and it’s working fine, thanks!

One more thing about entity dynamic construction.
I changed

var_e: 'var.' + cover_e[6:-4]

to

  var_e: "{{ 'var.' + cover_e[6:-4] }}"

There’s a subtle but important difference between using + and ~ to concatenate strings.

  • The tilde (~) handles everything as a string value. That means even if one of the values is numeric, it will be concatenated.
  • The plus (+) can perform addition or concatenation, it depends on the value’s type. Where things become problematic is if one value’s type is numeric and the other is string.

In the following example, the plus operator detects two integers so it adds them whereas the tilde operator handles them as strings and concatenates them.


The plus operator reports an error when it encounters a mix of integers and strings.


The tilde operator succeeds because it treats everything as strings.


If you want to ensure the template concatenates strings, even if one of them happens to be numeric, use the tilde operator. Reserve the use of the plus operator for addition (not concatenation).

  var_e: "{{ 'var.' ~ cover_e[6:-4] }}"

However, in this situation, you don’t even need to use either operator and can simply rely on Jinja2’s native ability to template only desired parts of a string.

  var_e: "var.{{ cover_e[6:-4] }}"

Another good lesson for me, thanks!