you should use float(0) and not default(0) or delete "default(0) | " and use round(2, default=0)
Hi, can someone tell me what I’m doing wrong? I still get no default errors for this:
TemplateError('ValueError: Template error: float got invalid input 'unknown' when rendering template '{{ (((float(states('sensor.f_st5366sd_kib_s_received')) * 8192) / 1000000) | round(3, default=0)) }}' but no default was specified')
name: "Mbps Received"
unit_of_measurement: "Mbps"
state: "{{ (((float(states('sensor.f_st5366sd_kib_s_received')) * 8192) / 1000000) | round(3, default=0)) }}"
availability: "{{ states('sensor.f_st5366sd_kib_s_received')|float(2) > 1 }}"
replace it with
state: "{{ states('sensor.f_st5366sd_kib_s_received')|float(0) * 8192 / 1000000 | round(3) }}"
You can leave your state as-is, but change your availability to verify that sensor.f_st5366sd_kib_s_received
is a number.
availability: "{{ states('sensor.f_st5366sd_kib_s_received') | is_number }}"
I had a similar issues when starting up Home Assistant and the sensors data is not yet available. It then spammed the log with errors like these:
ValueError: Template error: float got invalid input ‘unavailable’ when rendering template ‘{{ ((states(‘sensor.envoy_current_power_consumption’) | float * 0.001) - (states(‘sensor.envoy_current_power_production’) | float * 0.001))|round(2) }}’ but no default was specified
The template sensor that was causing it:
template:
sensor:
- name: Netto electriciteitsverbruik
unique_id: netto_electriciteitsverbruik
state: "{{ ((states('sensor.envoy_122239064314_current_power_consumption') | float * 0.001) - (states('sensor.envoy_122239064314_current_power_production') | float * 0.001))|round(2) }}"
unit_of_measurement: "Kw"
After reading the replay from Taras, I changed it to:
"{{ ((states('sensor.envoy_122239064314_current_power_consumption') | float(0) * 0.001) - (states('sensor.envoy_122239064314_current_power_production') | float(0) * 0.001))|round(2) }}"
Thanks for that!
Recently I have updated to from 2023.2.1 to 2024.2.2.
Since this update I get the messages:
[homeassistant.components.energy.sensor] Found unexpected state_class None for sensor.tasmota_sml_total_kwh
[homeassistant.components.energy.sensor] Found unexpected state_class None for sensor.gasmota_counter_c1_3
With further information for the second warning, that explains
ValueError: Sensor sensor.gasmota_counter_c1_3 has device class 'gas', state class 'None' unit 'm³' and suggested precision 'None' thus indicating it has a numeric value; however, it has the non-
numeric value: '' (<class 'str'>)
Since now, the code provided above from @bertreb worked perfect:
„This way you skip ‘unknown’ states.
value_template: >
{% if states('sensor.gasmota_counter_c1') | float(0) > 0 %}
{{ states('sensor.gasmota_counter_c1') | float(0) * 0.01 }}
{% endif %}
„
Now, both entries are obviously not „unknown“, but „non-numeric“ at start
What needs to be changed?
This error is unrelated to the no default provided errror.
This error is telling you that you didn’t provide a state_class
but you did provide a device_class
.
This can only be done on modern template sensors not legacy, you appear to be using a legacy template sensor.
Your options are:
- Remove
device_class: gas
.
or
- Convert to new template style and provide a
state_class
Opps, so I missed the modernization?
Ok, when I remove device_class, will it still be available in the Energy dashboard?
Otherwise, can I simply replace „Device_
“ by „State_
“
I am afraid, that it is not that simple
State_class is required for the energy dashboard. So option 1 is out. You need to move to the modern format.
I had a quick look into the template and found
SensorDeviceClass.GAS
So, my configuration.yaml entry looks like
sensor:
- platform: template
sensors:
gasmota_counter_c1_3:
friendly_name: "Gas-Zähler"
unit_of_measurement: "m³"
SensorDeviceClass.GAS: m³
unique_id: uniqueid__sensor_gasmota_counter_c1_3
value_template: >
{% if states('sensor.gasmota_counter_c1') | float(0) > 0 %}
{{ states('sensor.gasmota_counter_c1') | float(0) * 0.01 }}
{% endif %}
Maybe I could omit the unit_of_measurement
. Actually it does not harm, or not?
In addition, I have „detected“ more entries in customize.yaml:
state_class: total_increasing
May I have to replace these entries also? And, if, what instead?
That’s the old template sensor style, you need to switch to the new style.
A more intensive look into templates indicates, that the modern style is made with Jinja, right?
This looks completely unknown to me and will need some time to get into it. Is there a way, to „transform“ yaml into jinja?
Sigh……
Jinja is the templating language. It has nothing to do with the yaml format of the template integration.
Your yaml format is using the legacy template sensor format. You need to swap to the new yaml format. This is all covered in the documentation for the integration.
It took some time, so I finally got this version:
template:
sensor:
- name: "gasmota_counter_c1_3"
unit_of_measurement: "m³"
unique_id: uniqueid__sensor_gasmota_counter_c1_3
device_class: gas
state_class: total_increasing
state: >
{% if states('sensor.gasmota_counter_c1') | float(0) > 0 %}
{{ states('sensor.gasmota_counter_c1') | float(0) * 0.01 }}
{% endif %}
Hope, that will do it. At least, reloading without any errors.
yaml looks good but your template is a odd though. What are the intentions of the template?
As said on top, my gasometer goes with an analog counter, which has a reed contact build in. I catch the contact, which counts one per 0.01 m³. This template converts into „m³“ with two decimals and displays this value in the energy dashboard.
For the energy dashboard, the device_class seems to be mandatory. At least, I could not find, where to add SensorDeviceClass.GAS: m³
properly.
And, as is should not count backwards, I need the state_class total_increasing
.
And, in addition, I have to clear my Raspberry once per night, and restart HA. So, when Home-Assistant starts, the first entry of the gasometer is „zero“, which has first to be filtered out. Now, after updating, it seems, that the first entry is not „zero“, but „none“, which may be not yet corrected.
Yes but your template only outputs if the source sensor is positive, is that your intentions? Or are you just trying to make a safe template sensor that always has a value? If yes, you want this instead.
template:
sensor:
- name: "gasmota_counter_c1_3"
unit_of_measurement: "m³"
unique_id: uniqueid__sensor_gasmota_counter_c1_3
device_class: gas
state_class: total_increasing
state: >
{{ states('sensor.gasmota_counter_c1') | float * 0.01 }}
availability: >
{{ states('sensor.gasmota_counter_c1') | is_number }}
I see, you have left device_class untouched, so i hope, it is ok.
And, I have thought about „availability“, but could not guess, where to place it.
I will give it a try!
Thanks
Yeah, this works! No errors or warnings
Now, I have to clean up the other one
Logger: homeassistant.components.energy.sensor
Source: components/energy/sensor.py:289
Integration: Energie (documentation, issues)
First occurred: 20:48:52 (1 occurrences)
Last logged: 20:48:52
Found unexpected state_class None for sensor.tasmota_sml_total_kwh
This entry is among many others in the up to date untouched customize.yaml:
sensor.tasmota_sml_total_kwh:
unit_of_measurement: "kWh"
device_class: energy
state_class: total_increasing
last_reset: '2022-11-01T00:00:00+00:00'
sensor.tasmota_sml_export_total_kwh:
unit_of_measurement: "kWh"
device_class: energy
state_class: total_increasing
last_reset: '2022-11-01T00:00:00+00:00'
sensor.tasmota_sml_curr_w:
unit_of_measurement: "W"
device_class: power
state_class: measurement
last_reset: '2022-11-01T00:00:00+00:00'
As we see, only the first entry is mentioned in the log.
Is there an easy way to convert the complete customize.yaml to the modern template?