Threshold helper with number helper limit

I would like to define the threshold limit to be the value of the number helper -1. For example, if the number value is set to 80, i would like the limit to be set at 79. Is this possible? If not, is there a methodology that would allow me to achieve the same functionality?

Thanks for any help.

None of the configuration variables for the Threshold integration support templating, so it's not possible using Threshold.

It can most likely be done with a Template binary sensor, but we'll need more details to be able to tell how it would need to be set up.

In Home Assistant, I have an AC unit and a remote temperature sensor in a room (call the sensor value TS). I also want to define a number value using a number helper (call this A).

The desired behavior is:

• When TS > A + 1, set the AC setpoint to A − 2

• When TS < A − 1, set the AC setpoint to A + 2

That's more of an automation than a binary sensor...

alias: Climate Threshold 
description: |
  Change Thermostat setpoint when external thermometer passes above or below value
  of a numeric helper with hysteresis.
trigger_variables:
  A: input_number.example
  TS: sensor.example_temp
triggers:
  - trigger: template
    value_template: |
      {{ states(TS) | float > states(A) | float + 1 }}
    variables:
      setpoint: "{{ states(A) | float - 2 }}"
  - trigger: template
    value_template: |
      {{ states(TS) | float < states(A) | float - 1 }}
    variables:
      setpoint: "{{ states(A) | float + 2 }}"
conditions: []
actions:
  - action: climate.set_temperature
    target:
      entity_id: climate.example
    data:
      temperature: "{{ setpoint }}"

Though it's not likely with the hysteresis these triggers give, I would still probably add a for to each of those triggers to avoid bouncing.. just in case.

What is the “for” for? Sorry brand new to this level of coding.

Thanks for the replies!

The for variable in Template triggers sets a requirement that the template has to continue rendering "true" for a defined length of time.

This is the original trigger from above. It fires as soon as TS is greater than A+1:

trigger: template
value_template: |
  {{ states(TS) | float > states(A) | float + 1 }}
variables:
  setpoint: "{{ states(A) | float - 2 }}"

This is the same trigger, but with for defined. It fires when TS has been greater than A+1 for 10 seconds:

trigger: template
value_template: |
  {{ states(TS) | float > states(A) | float + 1 }}
for: "00:00:10"
variables:
  setpoint: "{{ states(A) | float - 2 }}"

My understanding of how to implement this on my systerm:

  1. Create a new automation, go into YAML editor, paste your initial code.
  2. Add the ‘for’ statements.
  3. Do I add my number helper and sensor entity information under “trigger variables” ? Exactly what part of your code do I replace?

Have I got that right? Are there any other edits required on my end?

Yes, you will need to put the entity ID of your entities for both the trigger variables and in the climate action where I used "climate.example".

There are 3 different conditions this automation will run under: work from home, unoccupied and sleep.

The sleep condition is from 9pm to 8am 7 days a week. The WFH condition is from 8am to 5pm Monday and Tuesday. The unoccupied condition is any time that is not sleep or WFH.

How could I have the automation reference 1 of 3 different number helpers (‘A’ in my previous example) based on the above schedule?

How will the "condition" affect the automation?

Will this condition's value be needed outside this automation?

I want to have 3 separate number helper sliders on my dashboard. I will set them as follows:

Sleep: 76°
WFH: 80°
Unoccupied: 82°

Based on the schedule I previously posted I want the automation to reference the appropriate helper number. For example, from 9pm to 8am 7 days a week I want the ‘A’ value to be 76 and from 8am to 5pm Monday and Tuesday I want ‘A’ to be 80°

I can’t think of a reason why these conditions would be needed outside the automation.

In order for the previous automation to work, we can't change the value of the entity IDs stored in A, trigger variables are fixed at restart/reload.

What I would do would be to use a Template sensor that derives the desired setpoint value, and use that sensor's ID for A in the automation.

template:
  - sensor:
      - name: HVAC Active Setpoint
        default_entity_id: sensor.hvac_active_setpoint
        unique_id: hvac_active_setpoint_0001
        state: |
          {% if now() >= today_at("21:00") or now() < today_at("8:00") %}
            {% set type = 'sleep' %}
          {% elif now().isoweekday() in [1,2] and (  today_at("8:00") < now() <= today_at("17:00") ) %}
            {% set type = 'wfh' %}
          {% else %}
            {% set type = 'unoccupied' %}
          {% endif %}
          {# The next part will need to be changed to work with your number helpers' entity IDs #}
          {% set helper_id =  'input_number.' ~ type %}
          {{ states(helper_id) }}

Since this is just a state-based template sensor, you can set it up in the UI from the Helpers menu if you don't want to do it in YAML. If you do it that way, only paste the lines below state: | into the field titled "State*".


Then, in the automation you would use:

  A: sensor.hvac_active_setpoint

I’m going to have 3 different number helpers. How do I incorporate those into the code you provided?

In the last two lines, the template "constructs" the entity ID from the results of the previous if/then statement... then returns the state of the helper.

 {% set helper_id =  'input_number.' ~ type %}
 {{ states(helper_id) }}

As it is configured in the previous post, the entity IDs would need to be input_number.sleep, input_number.wfh, and input_number.unoccupied. Those can be whatever you want as long as you update the template to match your entities.

If you can't figure it out, post the entity IDs you will be using, and we can update the sensor config to match.

I know HA has some native tools to help with times and schedules. Is there any way to define the schedules for Sleep and WFH in one of those tools so that i can adjust them in a more user friendly way than editing the automation?

Maybe I can define the schedule by creating events on the HA Calendar.

Or maybe I can use a schedule helper?

In HA there are almost always at least 3 ways to do anything... So, sure, you could use a Schedule helper or a Calendar.

To do it with a Schedule helper, you can add data to each schedule block under the "Advanced Settings". That would look something like:

Doing it that way would simplify the Template sensor to something like:

template:
  - sensor:
      - name: HVAC Active Setpoint
        default_entity_id: sensor.hvac_active_setpoint
        unique_id: hvac_active_setpoint_0001
        state: |
          {% set type = state_attr('schedule.EXAMPLE', 'type') or 'unoccupied' %} 
          {% set helper_id =  'input_number.' ~ type %}
          {{ states(helper_id) }}

What about a method with the calendar? It might be more convenient to create events on the calendar to define the schedule.

For that you need to answer a couple questions.. It can be done no matter what you answer, but the approach is a little different....

Will this calendar be used for anything else?

What part of the calendar data do you want to use to store the type data in?

I don't have plans to use the calendar for anything else. But i might want to use it for other stuff in the future.

In that case you need to decide how you want to structure the data so that we can pick out calendar events that belong to this process and retrieve the type data from them.

For example, you could have the title of the calendar events all be the same (like "HVAC Setting") and store the setpoint type in the event's description (Sleep). Or, you could have both pieces of data in the event title like "HVAC Setting: Sleep".