Using input_number value in automations

Hi.

I try to setup automations for my heating system. In that case, I have put the target temperature in an input_number (1st automation) and I want to put the value into the heating thermostat (2nd automation).

This is the code :

- alias: CalculTemp_Vanne-Salon
 trigger:
   platform: time_pattern
   minutes: "/5"
 action:
   service: input_number.set_value
   data:
     entity_id: input_number.temp_prevue_vanne_salon
     value: >-
       {% if state_attr('sensor.exception_vanne_salon', 'temperature') == None -%}
       {{ state_attr('sensor.tempprevue_vanne_salon', 'temperature') }}
       {%- else -%}
       {{ state_attr('sensor.exception_vanne_salon', 'temperature') }}
       {%- endif %}

- alias: Automatisme_Vanne-Salon
 trigger:
   platform: state
   entity_id: input_number.temp_prevue_vanne_salon
 action:
   service: climate.set_temperature
   entity_id: climate.vanne_salon
   temperature: {{ states(input_number.temp_prevue_vanne_salon) }}

I’m fighting with syntax at the last line. I’ve tried a lot of formalism without success. The last error is :
Error loading /config/configuration.yaml: invalid key: "OrderedDict([(‘states(input_number.temp_prevue_vanne_salon)’, None)])

What am I doing wrong ?

Regards

Your indentation is a bit off and the template format needs some adjustment:

- alias: Automatisme_Vanne-Salon
  trigger:
    platform: state
    entity_id: input_number.temp_prevue_vanne_salon
  action:
    service: climate.set_temperature
    data:
      entity_id: climate.vanne_salon
      temperature: "{{ states('input_number.temp_prevue_vanne_salon') }}"

Thanks for your help. Using your syntax, I can restart HA, but execution fails :

Traceback (most recent call last):
File “/usr/src/homeassistant/homeassistant/components/automation/init.py”, line 404, in async_trigger
await self.action_script.async_run(
File “/usr/src/homeassistant/homeassistant/helpers/script.py”, line 1026, in async_run
await asyncio.shield(run.async_run())
File “/usr/src/homeassistant/homeassistant/helpers/script.py”, line 242, in async_run
await self._async_step(log_exceptions=False)
File “/usr/src/homeassistant/homeassistant/helpers/script.py”, line 250, in _async_step
await getattr(
File “/usr/src/homeassistant/homeassistant/helpers/script.py”, line 429, in _async_call_service_step
domain, service_name, service_data = service.async_prepare_call_from_config(
File “/usr/src/homeassistant/homeassistant/helpers/service.py”, line 190, in async_prepare_call_from_config
raise HomeAssistantError(f"Error rendering data template: {ex}") from ex
homeassistant.exceptions.HomeAssistantError: Error rendering data template: UndefinedError: ‘input_number’ is undefined

I’ve checked with Delevoper Tool. Value in the input number is 22.0
What seems strange to a newbye like me is that “temperature” need a float value. I would say syntax you gave would produce a string…

Sorry for asking. Again I’m a newbye at HA…

Regards

{{states('input_number.temp_prevue_vanne_salon')|float}}

or

{{(states.input_number.temp_prevue_vanne_salon.state | int)}}

Always use the states('domain.object_id') form instead of states.domain.object_id.state. See the warning here as to why: https://www.home-assistant.io/docs/configuration/templating/#states

Use|int When the number has no fractional part (decimal places). If it has decimal places use |float.

Hi.
I still get the same error: input_number not defined. To be precise, this is the automation and the error.

- alias: Automatisme_Vanne-Salon
  trigger:
    platform: state
    entity_id: input_number.temp_prevue_vanne_salon
  action:
    service: climate.set_temperature
    data:
      entity_id: climate.vanne_salon
      temperature: "{{ states(input_number.temp_prevue_vanne_salon)|float }}"

2021-01-31 16:19:45 ERROR (MainThread) [homeassistant.components.automation.automatisme_vanne_salon] Automatisme_Vanne-Salon: Error executing script. Unexpected error for call_service at pos 1: Error rendering data template: UndefinedError: ‘input_number’ is undefined
Traceback (most recent call last):
File “/usr/src/homeassistant/homeassistant/helpers/template.py”, line 353, in async_render
render_result = compiled.render(kwargs)
File “/usr/local/lib/python3.8/site-packages/jinja2/environment.py”, line 1090, in render
self.environment.handle_exception()
File “/usr/local/lib/python3.8/site-packages/jinja2/environment.py”, line 832, in handle_exception
reraise(*rewrite_traceback_stack(source=source))
File “/usr/local/lib/python3.8/site-packages/jinja2/_compat.py”, line 28, in reraise
raise value.with_traceback(tb)
File “”, line 1, in top-level template code
File “/usr/local/lib/python3.8/site-packages/jinja2/sandbox.py”, line 407, in getattr
value = getattr(obj, attribute)
jinja2.exceptions.UndefinedError: ‘input_number’ is undefined

Regard

You have no quotes in your template (again).

temperature: "{{ states(input_number.temp_prevue_vanne_salon)|float }}"

Should be:

temperature: "{{ states('input_number.temp_prevue_vanne_salon')|float }}"
1 Like

I’m quite stupid.
You’re perfectly right.

Thanks for help !

Hello,

I take advantage of this topic to ask for your help. I can’t get my script to work properly.

I created an input_number entry with a slider to set the percentage battery I want. Once the script is executed, it must turn on my socket which my phone charger is plugged into, then (the problem is there), I ask to wait until the desired percentage is reached. I ended up turning off the phone socket. My problem, once the percentage is reached, my socket does not turn off. Can anyone help me?

Thanks

alias: Démarrer la recharge (%)
sequence:
  - type: turn_on
    device_id: 927a47351bffade30863ed0f4044a1fe
    entity_id: switch.prise_telephone_ee4f080a_on_off
    domain: switch
  - wait_template: |-
      platform: state
      entity_id:
        - sensor.oneplus_5_battery_level_2
      to: "{{ states('input_number.pourcentage_batterie_souhaite')|int }}"
    continue_on_timeout: true
    enabled: true
  - type: turn_off
    device_id: 927a47351bffade30863ed0f4044a1fe
    entity_id: switch.prise_telephone_ee4f080a_on_off
    domain: switch
mode: single
icon: mdi:power

Your wait_template is not a template, it’s a state trigger… and you can’t use templates within a state trigger. Also, you should define a timeout for your continue_on_timeout.

However, it’s generally best not to rely on waits. Instead, create an automation that triggers on your desired state or event, with conditions as appropriate.