Template sensor from input_number 'unknown' on HA restart

I have a problem with template sensors that take their value from input_number (a helper). The problem is, on HA restart, sensors change their value to ‘unknown’, and then back to original value. So this behaviour produces repeated value, and the timestamp associated with last value change is then wrong.

My sensor definition:

- name: Bieżnia dystans w km
  unique_id: bieznia_dystans_w_km
  state: >-
    {{ (states('input_number.bieznia_dystans')|float(0) / 1000)|round(2) }}
  unit_of_measurement: "km"

Database view:

Below are the logs of the time of HA restart (it’s the same time as in database: 1 hour diff is because of local time). It does seem related, right? I mean, the time 13:16:16 in the database is just a few miliseconds after this log entry.

2023-02-03 14:16:15.784 WARNING (MainThread) [homeassistant.components.sensor] Setup of sensor platform command_line is taking over 10 seconds.
2023-02-03 14:16:15.806 WARNING (MainThread) [homeassistant.components.sensor] Setup of sensor platform command_line is taking over 10 seconds.
2023-02-03 14:16:15.808 WARNING (MainThread) [homeassistant.setup] Setup of timer is taking over 10 seconds.
2023-02-03 14:16:15.810 WARNING (MainThread) [homeassistant.setup] Setup of input_button is taking over 10 seconds.
2023-02-03 14:16:15.811 WARNING (MainThread) [homeassistant.setup] Setup of input_select is taking over 10 seconds.
2023-02-03 14:16:15.812 WARNING (MainThread) [homeassistant.setup] Setup of input_datetime is taking over 10 seconds.
2023-02-03 14:16:15.814 WARNING (MainThread) [homeassistant.setup] Setup of zone is taking over 10 seconds.
2023-02-03 14:16:15.815 WARNING (MainThread) [homeassistant.setup] Setup of input_number is taking over 10 seconds.
2023-02-03 14:16:15.816 WARNING (MainThread) [homeassistant.setup] Setup of schedule is taking over 10 seconds.
2023-02-03 14:16:15.818 WARNING (MainThread) [homeassistant.setup] Setup of input_text is taking over 10 seconds.
2023-02-03 14:16:15.819 WARNING (MainThread) [homeassistant.setup] Setup of counter is taking over 10 seconds.
2023-02-03 14:16:15.820 WARNING (MainThread) [homeassistant.setup] Setup of input_boolean is taking over 10 seconds.
2023-02-03 14:16:15.821 WARNING (MainThread) [homeassistant.components.sensor] Setup of sensor platform sql is taking over 10 seconds.
2023-02-03 14:16:15.822 WARNING (MainThread) [homeassistant.setup] Setup of application_credentials is taking over 10 seconds.
2023-02-03 14:16:15.823 WARNING (MainThread) [homeassistant.setup] Setup of template is taking over 10 seconds.

Any ideas, how to fix this?

Try this:

- name: Bieżnia dystans w km
  unique_id: bieznia_dystans_w_km
  availability: "{{ is_number('input_number.bieznia_dystans')}}"
  state: >-
    {{ (states('input_number.bieznia_dystans')|float(0) / 1000)|round(2) }}
  unit_of_measurement: "km"

This should make your template sensor unavailable during the startup, while the input number is also unavailable, so it wouldn’t be calculating a state for your template sensor.
I’m not sure if this will have impact on how last change behaves.

1 Like

Thank you, it looks very promising :slight_smile: I’ll update my reply after testing it.

Edit: so I added availability with {{ is_number(states('input_number.bieznia_dystans')) }} to my sensor definition, then restarted HA - but it didn’t help :frowning:

I probably could create SQL sensor to select from ‘states’, and ignore rows with ‘unknown’ and ‘unknown’ parent. Just seems a bit complicated for a simple task :slight_smile: