How to create a Input_Number without decimals

Dear all

I was trying to creat a input_number without decimals.

input_number:
  box1:
    name: Lüftung Partytimer Konfigurieren
    initial: 200
    min: 0
    max: 3000
    step: 1
    mode: box

issue

The reason I need the number without decimal is because the input_number will be exposed to the knx bus. And the knx is validating the number and will fail with decimals.

Is there a way to do this?
Thank you very much for your help. Best regards

How? Via a service call maybe?

Generally you would do something like this:

- service: SOME.SERVICE
  data_template:
    SOME_DATA_FIELD: "{{ states('input_number.box1')|int }}"
2 Likes

I use the knx integration with the following configuration:

knx:
  tunneling:
    host: '192.168.XXX.XXX'
    port: 3671
    local_ip: '192.168.XXX.XXX'
  expose:
    - type: 'time_period_sec'
      entity_id: 'input_number.luftung_partytimer_konfigurieren'
      address: '0/5/21'

but the error i get is:
(I know its just because of the decimal, because i also created a input_text with string value 700 and this works -> but it’s ugly i think )

Logger: homeassistant.core
Source: components/knx/__init__.py:400
First occurred: 20:23:43 (1 occurrences)
Last logged: 20:23:43

Error doing job: Task exception was never retrieved
Traceback (most recent call last):
  File "/usr/local/lib/python3.7/site-packages/xknx/dpt/dpt_2byte_uint.py", line 33, in to_knx
    knx_value = int(value)
ValueError: invalid literal for int() with base 10: '700.0'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/components/knx/__init__.py", line 385, in _async_entity_changed
    await self._async_set_knx_value(new_state.state)
  File "/usr/src/homeassistant/homeassistant/components/knx/__init__.py", line 400, in _async_set_knx_value
    await self.device.set(value)
  File "/usr/local/lib/python3.7/site-packages/xknx/devices/expose_sensor.py", line 74, in set
    await self.sensor_value.set(value)
  File "/usr/local/lib/python3.7/site-packages/xknx/remote_value/remote_value.py", line 120, in set
    payload = self.to_knx(value)  # pylint: disable=assignment-from-no-return
  File "/usr/local/lib/python3.7/site-packages/xknx/remote_value/remote_value_sensor.py", line 230, in to_knx
    return DPTArray(self.DPTMAP[self.value_type].to_knx(value))
  File "/usr/local/lib/python3.7/site-packages/xknx/dpt/dpt_2byte_uint.py", line 38, in to_knx
    raise ConversionError("Cant serialize %s" % cls.__name__, value=value)
xknx.exceptions.exception.ConversionError: <ConversionError description="Cant serialize DPTTimePeriodSec" value="700.0"/>

Hmm, that seems pretty limited.

I’d suggest creating a template sensor and using that as the exposed entity:

sensor:
- platform: template
  sensors:
    luftung_partytimer_konfigurieren:
      value_template: "{{ states('input_number.luftung_partytimer_konfigurieren')|int }}"

The sensor’s entity_id would be: sensor.luftung_partytimer_konfigurieren

2 Likes

@pnbruckner

It works perfect -> Thank you very much for your help. I didn’t expect such a fast answer.
To be honest i don’t understand what the |int is doing. Is it convertign to a integer?

You may want to read up on Templating.

EDIT: Basically, yes.

You’ll notice in the error you shared:

Traceback (most recent call last):
  File "/usr/local/lib/python3.7/site-packages/xknx/dpt/dpt_2byte_uint.py", line 33, in to_knx
    knx_value = int(value)
ValueError: invalid literal for int() with base 10: '700.0'

That’s using Python’s int (integer) type to convert a string to an integer. Unfortunately that doesn’t work with a string containing a decimal (i.e., a string representation of a float.)

But the |int part in the Jinja template is a filter, specifically the int filter. It does pretty much the same as the Python function, but it can accept a string representation of a float. (Remember, all states in HA are strings.)

1 Like

Thank you very much for this explanation. :+1:

This seems odd that an INPUT_NUMBER set to STEP: 1 would display a .0 by default - seems it should reference the STEP when displayed. Realize you can create template sensor for it - but this seems duplicative.

14 Likes