Blueprint variables use a another variable to define an !input

Looking for some assistance for my first attempt at a blueprint

I am trying to define a variable using a previously defined variable to make up a name of an !input
(the previous variable calculates the time period and this is appended to the the next variable to work out what temperature is going to be used)

Here is the part of the code I am failing on:

variables:
  morning_start_time: !input morning_start_time
  day_start_time: !input day_start_time
  evening_start_time: !input evening_start_time
  night_start_time: !input night_start_time
  night_low_temp: !input night_low_temp

#calculate what part of the day it is
  time_period: >
    {% if now() > today_at(morning_start_time) and now() < today_at(day_start_time) %} morning
    {% elif now() > today_at(day_start_time) and now() < today_at(evening_start_time) %} day
    {% elif now() > today_at(evening_start_time) and now() < today_at(night_start_time) %} evening
    {% elif now() > today_at(night_start_time) and now() < today_at(morning_start_time) %} night
    {% else %} unknown {% endif %}

#use day period to set high and low temp target
  low_temp: !input {{time_period}}_low_temp

It is the final line that is failing. It is meant to end up with the temperature from the relevant input (e.g. day_low_temp or night_low_temp)

I have tied putting it in “” but that just means that it is literally passed through as the variable rather than using the input value.

Is this possible?

Here is the entire code in context. Grateful for any pointers.

I could do a load if ‘if’ statements but that doesn’t seem efficient/elegant!

Summary
blueprint:
  name: HVAC automation
  description: Adjust HVAC (utilising remote sensor) setting acceptable ranges varying at time of day
  domain: automation

  input:
    hvac_unit:
      name: HVAC device
      description: Select desired climate decive
      selector:
        device:
          entity:
            - domain: climate
    temp_sensor:
      name: Temperature sensor
      description: Select desired temperature sensor (could be the from the HVAC or a wireless one elsewhere in the room e.g. zigbee)
      selector:
        entity:
          filter:
            - domain: sensor

    morning_start_time:
      name:  ' '
      description: '# ☀️ Morining start time ☀️'
      default: '07:00:00'
      selector:
        time: {}
    morning_low_temp:
      name: ' '
      description: '🥶 Morning low temperature 🥶'
      default: 20
      selector:
        number:
          min: 12
          max: 24
          step: 0.5
          unit_of_measurement: "°C"
          mode: slider
    morning_high_temp:
      name: ' '
      description: '🥵 Morning high temperature 🥵'
      default: 24
      selector:
        number:
          min: 18
          max: 28
          step: 0.5
          unit_of_measurement: "°C"
          mode: slider

    day_start_time:
      name:  ' '
      description: '# 🏢 Day start time 🏢'
      default: '09:00:00'
      selector:
        time: {}
    day_low_temp:
      name: ' '
      description: '🥶 Day low temperature 🥶'
      default: 18
      selector:
        number:
          min: 12
          max: 24
          step: 0.5
          unit_of_measurement: "°C"
          mode: slider
    day_high_temp:
      name: ' '
      description: '🥵 Day high temperature 🥵'
      default: 26
      selector:
        number:
          min: 18
          max: 28
          step: 0.5
          unit_of_measurement: "°C"
          mode: slider

    evening_start_time:
      name: ' '
      description: '# 🌇 Evening start time 🌇'
      default: '18:00:00'      
      selector:
        time: {}
    evening_low_temp:
      name: ' '
      description: '🥶 Evening low temperature 🥶'
      default: 20
      selector:
        number:
          min: 12
          max: 24
          step: 0.5
          unit_of_measurement: "°C"
          mode: slider
    evening_high_temp:
      name: ' '
      description: '🥵 Evening high temperature 🥵'
      default: 24
      selector:
        number:
          min: 18
          max: 28
          step: 0.5
          unit_of_measurement: "°C"
          mode: slider

    night_start_time:
      name: ' '
      description: '# 🌙 Night start time 🌙'
      default: '23:30:00'
      selector:
        time: {}
    night_low_temp:
      name: ' '
      description: '🥶 Night low temperature 🥶'
      default: 18.5
      selector:
        number:
          min: 12
          max: 24
          step: 0.5
          unit_of_measurement: "°C"
          mode: slider
    night_high_temp:
      name: ' '
      description: '🥵 Night high temperature 🥵'
      default: 25
      selector:
        number:
          min: 18
          max: 28
          step: 0.5
          unit_of_measurement: "°C"
          mode: slider

    low_temp_target_offset:
      name: ' '
      description: |
        # 🥶 Offset for low target temp 🥶
        e.g. if the low temperature is set to 20 and this is set to -2 then the HVAC will not kick in until 18°C 
      default: '-0.5'
      selector:
        number:
          min: -3
          max: 0
          step: 0.5
          unit_of_measurement: "°C"
          mode: slider
    high_temp_target_offset:
      name: ' '
      description: |
        # 🥵 Offset for high target temp 🥵
        e.g. if the high temperature is set to 24 and this is set to 2 then the HVAC will not kick in until 26°C 
      default: '0.5'
      selector:
        number:
          min: 0
          max: 3
          step: 0.5
          unit_of_measurement: "°C"
          mode: slider

trigger:
  - platform: time_pattern
    minutes: 59

variables:
  morning_start_time: !input morning_start_time
  day_start_time: !input day_start_time
  evening_start_time: !input evening_start_time
  night_start_time: !input night_start_time
  morning_low_temp: !input night_low_temp
  day_low_temp: !input night_low_temp
  evening_low_temp: !input night_low_temp
  night_low_temp: !input night_low_temp

#calculate what part of the day it is
  time_period: >
    {% if now() > today_at(morning_start_time) and now() < today_at(day_start_time) %} morning 
    {% elif now() > today_at(day_start_time) and now() < today_at(evening_start_time) %} day 
    {% elif now() > today_at(evening_start_time) and now() < today_at(night_start_time) %} evening 
    {% elif now() > today_at(night_start_time) and now() < today_at(morning_start_time) %} night 
    {% else %} unknown {% endif %}
  
#use day period to set high and low temp target
  low_temp: !input {{time_period}}_low_temp

#calcualte if sensor temperature is lower than the low_temp_targer or higher than the high_temp_target or within range
#  within_target: {% if %}


action:
#calculate time_period
  - service: notify.persistent_notification
    data: 
      message: it is {{ time_period }} and the low temp is set as {{ low_temp }}

You wouldn’t use the !input here, you have all those ‘_low_temp’ values already in variables. Just use a template to construct that and have one variable = another variable.

also I would use the developer tools template scratchpad to work out the exact template.

Thanks for the input

I have tried:

  low_temp: '{{time_period}}_low_temp'

However this passes through morning_low_temp rather than the underlying variage (which should be 20)

Any ideas?

time_period: >
  {% set a = today_at(morning_start_time) %}
  {% set b = today_at(day_start_time) %}
  {% set c = today_at(evening_start_time) %}
  {% set d = today_at(night_start_time) %}
  {% set now = now() %}

  {% set map = {
  a < now <= b: ['morning', morning_low_temp],
  b < now <= c: [ 'day', day_low_temp],
  c < now <= d: ['evening', evening_low_temp],
  d < now or a > now: ['night', night_low_temp] } %}
  {{ map.get(True) }}

action:
  - service: notify.persistent_notification
    data: 
      message: it is {{ time_period[0] }} and the low temp is set as {{ time_period[1] }}

EDIT: Corrected missing %}

Thank you very much - this works perfectly with a little adjustment (and I can see how to put the xxx_high_temp in there too)

Just in case anyone else is using this I had to add a % } to each of the quoted line

(interestingly HA has suddenly stopped giving error message when an automation has errors - it simply says ‘automation unavailable’ - not sure if this is a 2023.9.0 upgrade issue)

1 Like