[SOLVED] Template sensor giving unknown but developer template shows true

Unless you use packages

1 Like

but he’s not using packages

Thanks folks, it is now working. I was pulling my hair out over this one. It is really odd that some sensors worked with the syntax and this one did not. However, it is now SOLVED thanks to all the help.

I am not using packages and am a newbie to HomeAssistant. But I am not new to automation and controls so I often try to do things that are more complicated than the examples.

I appreciate all the help and support.

txNgineer,
do you mind sharing how you fixed the issue?

1 Like

Apparently he followed this advice so can you.

1 Like

Sure, I followed the advice that petro gave in the message and made sure that I used the “sensors” platform line only once in the yaml.

Hi everyone, this is my first post herre!
I just hit the same problem, seemingly out of nowhere, as some template sensors stopped working while others continued to do so. This kind of behavior is very hard to debug!
IMHO this is a bug: such a YAML configuration should not validate, but it does.

I agree with you that this is a bug. It’s extremely frustrating to have a config validate when it is in fact broken.

I’ve got this issue now.

template:
  - sensor:
      - name: alarm_sensor
        device_class: timestamp
        state: "{{ (states('input_datetime.alarm_time')) }}"

Works perfectly in Dev Tools and returns the correct state.

However, within the rest of HA it returns “unknown”.

device_class: timestamp requires a date. Your logs will tell you why it’s not working.

I.e. Your template is fine, where you’re using it is not.

Thanks for your assistance. Much appreciated!

Can you help me with a fix? I need to create a time sensor from an input_datetime

I’ve tried:

template:
  - sensor:
      name: test
      device_class: timestamp
      state: {{ state_attr("input_datetime.alarm_time", "timestamp") | timestamp_custom("%H.%M", False) }}

Again - works in template but configuration.yaml doesn’t like it due to “missed comma between flow collection entries”

you’re missing outside quotes for your template. Use the multiline yaml notation or ensure your outside quotes are wrapped around your template. Also make sure outside quotes and inside quotes are different, i.e. " outside and ' inside.

Template show correct in developer tool template editor, but error in configuration ?

TemplateError('ValueError: Template error: float got invalid input 'unavailable' when rendering template '{{ ((states.sensor.varmepumpe_total_daily_energy.state | float) * (states.sensor.strompris_kwh.state | float)) | round(2) }}' but no default was specified') while processing template 'Template("{{ ((states.sensor.varmepumpe_total_daily_energy.state | float) * (states.sensor.strompris_kwh.state | float)) | round(2) }}")' for attribute '_attr_native_value' in entity 'sensor.varmepumpe_kost_daglig'
TemplateError('ValueError: Template error: float got invalid input 'unavailable' when rendering template '{{ ((states.sensor.varmepumpe_forbruk_mnd.state | float) * (states.sensor.strompris_kwh.state | float)) | round(2) }}' but no default was specified') while processing template 'Template("{{ ((states.sensor.varmepumpe_forbruk_mnd.state | float) * (states.sensor.strompris_kwh.state | float)) | round(2) }}")' for attribute '_attr_native_value' in entity 'sensor.varmepumpe_kost_mnd'
TemplateError('ValueError: Template error: float got invalid input 'unavailable' when rendering template '{{ ((states.sensor.easse_forbruk_kwh_dag.state | float) * (states.sensor.strompris_kwh.state | float)) | round(2) }}' but no default was specified') while processing template 'Template("{{ ((states.sensor.easse_forbruk_kwh_dag.state | float) * (states.sensor.strompris_kwh.state | float)) | round(2) }}")' for attribute '_attr_native_value' in entity 'sensor.ladebil_kost_daily'
TemplateError('ValueError: Template error: float got invalid input 'unavailable' when rendering template '{{ ((states.sensor.easse_forbruk_kwh_mnd.state | float) * (states.sensor.strompris_kwh.state | float)) | round(2) }}' but no default was specified') while processing template 'Template("{{ ((states.sensor.easse_forbruk_kwh_mnd.state | float) * (states.sensor.strompris_kwh.state | float)) | round(2) }}")' for attribute '_attr_native_value' in entity 'sensor.ladebil_kost_mnd'
TemplateError('TypeError: is_state() takes 3 positional arguments but 4 were given') while processing template 'Template("{% if is_state('sensor.washing_machine_status','Running','Finishing') %} Running {% else %} Finishing {% endif %}")' for attribute '_attr_native_value' in entity 'sensor.vaskemaskin_status'

Many filters (including float) require assigned defaults as of 2012.10

From Templating Docs:

WARNING

Avoid using states.sensor.temperature.state , instead use states('sensor.temperature') . It is strongly advised to use the states() , is_state() , state_attr() and is_state_attr() as much as possible, to avoid errors and error message when the entity isn’t ready yet (e.g., during Home Assistant startup).

Thanks , Seem template is little bit correct now as documentation. what you think ?
How can i manage to only 2 digits after decimal.

{{ states( 'sensor.varmepumpe_total_daily_energy' ) | float * states( 'sensor.strompris_kwh' ) | float | round(2) }}

Extra set of brackets. You’re just rounding the kWh figure. Here it is with the defaults added too:

{{ (states('sensor.varmepumpe_total_daily_energy')|float(0)*states('sensor.strompris_kwh')|float(0))|round(2) }}

Thanks, but still error

Configuration invalid!

Error loading /config/configuration.yaml: invalid key: "OrderedDict([("(states('sensor.gangen_varmeovn_cal_energy_hourly')| float(0)*states('sensor.strompris_kwh')|float(0))|round(2)", None)])"
in "/config/packages/Varmeapparater/ganagen_varmeovn.yaml", line 49, column 0

My Template is:

value_template: {{ (states('sensor.gangen_varmeovn_cal_energy_hourly')|float(0)*states('sensor.strompris_kwh')|float(0))|round(2) }}

You need to surround a single-line template in double quotes. See the important rules in this page:

Because your template uses single quotes, surround it with double quotes.

Do you mean like

value_template: {{ (states("sensor.gangen_varmeovn_cal_energy_hourly")|float(0)*states("sensor.strompris_kwh")|float(0))|round(2) }}

No. Like this:

value_template: "{{ (states('sensor.gangen_varmeovn_cal_energy_hourly')|float(0)*states('sensor.strompris_kwh')|float(0))|round(2) }}"