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

After i use double quotes, an other error popup

Invalid config for [sensor.template]: invalid template (TemplateSyntaxError: unexpected char "'" at 71) for dictionary value @ data['sensors']['gangen_varmeovn_kost_idag']['value_template']. Got "{{ states('sensor.gangen_varmeovn_cal_energy_daily')| float(0)*(states('sensor.strompris_kwh | float(0)) | round(2) }}". (See ?, line ?).
Invalid config for [sensor.template]: invalid template (TemplateSyntaxError: unexpected ')') for dictionary value @ data['sensors']['varmepumpe_time_pris']['value_template']. Got "{{ states('sensor.gangen_varmeovn_cal_energy_hourly')| float(0)*states('sensor.strompris_kwh')|float(0))|round(2) }}". (See ?, line ?).

I dont know why i have to use 2 different syntax in same type template ? one is valid with float(0)) and other is valid with float(0)


if i delete extra ) like float(0) then then template is valid

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

ERROR

Invalid config for [sensor.template]: invalid template (TemplateSyntaxError: unexpected ')') for dictionary value @ data['sensors']['varmepumpe_kost_mnd']['value_template']. Got "{{ states('sensor.varmepumpe_forbruk_mnd')|float(0)*states('sensor.strompris_kwh')|float(0))|round(2) }}". (See ?, line ?).

if i add extra ) like float(0)) then then template is valid

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

ERROR

Invalid config for [sensor.template]: invalid template (TemplateSyntaxError: unexpected '}', expected ')') for dictionary value @ data['sensors']['gangen_varmeovn_time_pris']['value_template']. Got "{{ (states('sensor.gangen_varmeovn_cal_energy_hourly')|float(0)*states('sensor.strompris_kwh')|float(0)|round(2) }}". (See ?, line ?).

Please post the entire code of the sensor. If this is a template sensor using the modern format, you should use state not value_template — but without the whole code we’re wasting time guessing.
Have a look at the excellent documentation (here, for example).

For the template itself, the brackets aren’t just something you randomly sprinkle in to see if it works. It can help to space things out a bit to understand what’s going on. Here’s the template from my prior post, and then again spaced out:

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

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

Each sensor is converted to a float then multiplied together, then the result, contained in the outermost brackets, is rounded.

In your second post above (#36), you were missing those outermost brackets, so the round was only applied to the price sensor.

1 Like

So after giving up on this, I thought I’d have another go. Wrote some errors in to see what the issue is.

What state do I still get? “unknown”

So painful. Any clues?

template:
  - sensor:
      name: test
      device_class: timestamp
      state: >
        {% set datetime_attr = state_attr('input_datetime.time', 'timestamp') %}
        {% if datetime_attr is defined %}
          {% set timestamp = datetime_attr | float %}
          {% if timestamp %}
            {% set formatted_time = timestamp | timestamp_custom('%H:%M', False) %}
            {{ formatted_time }}
          {% else %}
            {{ 'Error: Invalid timestamp' }}
          {% endif %}
        {% else %}
          {{ 'Error: input_datetime.time not defined' }}
        {% endif %}

Edit.

I thought I’d rewrite this using strftime instead. Results? “unknown”

template:
  - sensor:
      name: test
      device_class: timestamp
      state: "{{ state_attr('input_datetime.time', 'timestamp') | float | timestamp | strftime('%H:%M') }}"

What is your goal with this? Both template sensors have errors, but they are also pointless because you’re using a datetime entity which already mirrors a timestamp sensor in regards to automations.

Because this blueprint only allows sensor inputs for alarm time. The other option is a manual time which means editing the automation every day.

Ok,

{{ today_at(states('input_datetime.time')) }}

Thank you! I now get a result that isn’t “unknown”.

Should today_at use my system time or local time? It’s showing an hour earlier than the input which would be GMT. I’m in London and we’re now at BST, one hour ahead.

My system is set to London and it shows the correct current time.

I’ve tried adding local and as_local but neither resolve it.

today_at does use local time. If you’re timestamp isn’t showing up correctly then you aren’t using the correct time somewhere on your system. There were bugs in older versions of home assistant related to today_at, but they were fixed. So make sure you’re on the latest HA.

It does indeed display correctly in anything UI:

But when you check what it’s showing in the backend, via Developer / States it’s wrong:

Based on another post from you @petro (clearly very in demand) I believe the +0000 at the end of the sensor is showing that it’s ignoring my local time zone and is reporting UTC.

Shouldn’t today_at handle that? If not, is there something I can add to your code above to fix?

The backend is UTC. If you’re in a +1 timezone then 22:36 +00:00 is correct as that’s the same as 23:36+01:00

Today at only works with “00:00:00” or “00:00” time formats. You should use

{{ states('input_datetime.time') | as_datetime | as_local }}

because you have a fully fleshed out timestamp, not just time.

This did it. Thanks very much for your help.