Limit the value of an entity attribute based on event

Hello all.

How can I limit a specific value of an entity that I don’t want to exceed a specific threshold?

My use case would be to limit the heater temperature to a maximum value, even if the user using the remote control sets a higher value than the one I want. So, for example, if by using the IR control the user sets the temperature to 30°, to use an automation to bring it back to a more reasonable 25°.

The idea is to grab this event every time the temperature changes, or even periodically (and of course only when the heater is on.

The entity right now has an attribute named “temperature” which is exactly what I want to target.

Any ideas?

You could set up a template sensor to change it. I set it up with an input helper so you have control over the value but can change from the UI

template:
  - name: "Limit Temp"
    unique_id: "Limit Heat Temperature"
    state: >-
       {% if state_attr('climate.my_thermostat', 'temperature') > input_number.my_temp %}
           {{ input_number.my_temp }}
       {% else %}
           {{ state_attr('climate.my_thermostat', 'temperature') }}
       {% endif %}

Then use an automation

alias: Limit Temp
description: ''
mode: single
trigger:
  - platform: state
    entity_id: sensor.limit_temp
condition: []
action:
  - service: climate.set_temperature
    data:
      temperature: "{{ state('sensor.limit_temp') }}"
    target:
      entity_id: climate.my_thermostat

edit: fixed paren to } in else statement.

2 Likes

Thank you @AllHailJ … your insight is more than valuable for me.
Now with your proposal it finally makes sense … the whole template mechanism.

But I still have a few errors to fix, if you can help :grinning:

First of all, in your proposal there’s a typo, a “)” instead of “}”. This was easy to fix. My actual problem is that I can;t get the value of an entity inside the quotes. And this is not only on the template, but also on the automation.

On the automation I see this error on the logs:

raise TemplateError(err) from err
homeassistant.exceptions.TemplateError: UndefinedError: 'input_number' is undefined
2022-03-20 19:05:20 ERROR (MainThread) [homeassistant.helpers.template] Template variable error: 'input_number' is undefined when rendering '{% if state_attr('climate.bedroom', 'temperature') > input_number.maximum_temperature %}
{{ input_number.maximum_temperature }}
{% else %}
{{ state_attr('climate.bedroom', 'temperature') }}
{% endif %}'
2022-03-20 19:05:20 ERROR (MainThread) [homeassistant.components.template.template_entity] TemplateError('UndefinedError: 'input_number' is undefined') while processing template 'Template("{% if state_attr('climate.bedroom', 'temperature') > input_number.maximum_temperature %}
{{ input_number.maximum_temperature }}
{% else %}
{{ state_attr('climate.bedroom', 'temperature') }}
{% endif %}")' for attribute '_attr_native_value' in entity 'sensor.bedroom_limit_temp'

I bypassed this problem by setting the value hard coded.
But then the automation has a similar ptoblem:

Error: Error rendering data template: UndefinedError: 'state' is undefined

On the template I even tried to use the syntax state('...') but with no luck either.

Any idea what I am doing wrong?

Thank you again!

Have you created input number in the UI? This is done in the helpers. Configuration → Automations & Scenes → helpers. Also have you set the value either by a default or in the UI?

Also you can do this in configuration.yaml

input_number:
  maximum_temperature:
    name: "Maximum Temperature"
    icon: mdi:thermometer
    initial: 24
    min: 15
    max: 25
    step: 0.5
    mode: slider
    unit_of_measurement: *C

you will need to use degree symbol in degree C

Fix the undefined input_number. This should clear up your problems.

Yes I did… from the UI as you said and actually it does have a value.
It is strange for me that I also the automation does not recognize the value of the produced parameter.

Anyway this is a completely different problem; I don’t want to hijack your wiliness to help – you have already put me in the right track and “officially” the question I asked is solved :slight_smile:
Thank you again

Btw, here is the code I use:

template:
  - sensor:
    - name: "Bedroom Limit Temp"
      state: >
        {% if state_attr('climate.bedroom', 'temperature') > state('input_number.maximum_temperature') %}
           {{ state('input_number.maximum_temperature') }}
        {% else %}
           {{ state_attr('climate.bedroom', 'temperature') }}
        {% endif %}

Here the definition:

And here the automation trigger:

service: climate.set_temperature
data:
  temperature: '{{ state(''sensor.bedroom_limit_temp'') }}'
target:
  entity_id: climate.bedroom

Being technically correct is always the best kind ;-). Sorry I missed the state. DUH!