Sensor template problem - expected a dictionary for dictionary value

Thought I was nailing HA, then fell over again.

This gives me a green tick in my config, but when checking the config I get the below error and cannot work out what it means let alone why:

sensor:
  - platform: template
    sensors:
      time_at_work_rounded:
      value_template: "{% set hours = states('sensor.time_at_work') | float %} 
      {% set minutes = ((hours % 1) * 60) | int %}  
      {% set hours = (hours - (hours % 1)) | int %}  
      {{ '%02i:%02i'%(hours, minutes) }}"
 

Error message:

Invalid config for [sensor.template]: expected a dictionary for dictionary value @ data[ā€˜sensorsā€™][ā€˜time_at_work_roundedā€™]. Got None
expected a dictionary for dictionary value @ data[ā€˜sensorsā€™][ā€˜value_templateā€™]. Got ā€œ{% set hours = states(ā€˜sensor.time_at_workā€™) | float %} {% set minutes = ((hours % 1) * 60) | int %} {% set hours = (hours - (hours % 1)) | int %} {{ ā€˜%02i:%02iā€™%(hours, minutes) }}ā€. (See ?, line ?). Please check the docs at https://home-assistant.io/integrations/sensor.template/

I have tried changing " to ā€™ and but with no luck. I have read about needing an entity_id from HA 0.8 onwards but this is not specified as required in the docs that I can see?

No doubt something obvious but its stumped me

Check your indentation. value_template needs to be indeted more.

Try this:

sensor:
  - platform: template
    sensors:
      time_at_work_rounded:
        value_template: "{% set hours = states('sensor.time_at_work') | float %} 
          {% set minutes = ((hours % 1) * 60) | int %}  
          {% set hours = (hours - (hours % 1)) | int %}  
          {{ '%02i:%02i'%(hours, minutes) }}"

oh you are kidding me!

it threw me because I didnt get a red cross error in my config, cannot believe i have failed on spacings again!

thank you

It might be easier to understand a multi-line template in this case.

        value_template: >
          {% set hours = states('sensor.time_at_work') | float %} 
          {% set minutes = ((hours % 1) * 60) | int %}  
          {% set hours = (hours - (hours % 1)) | int %}  
          {{ '%02i:%02i'%(hours, minutes) }}

TIL that a multi-line template can be written like a single-line template (i.e. using outer quotes).

However, Iā€™m not likely to change from using the traditional multi-line convention (as shown in tom_Iā€™s example above).

Yeah it was a bit of a surprise for me too.

I had it as multi line to start with and when trying to get it to work and seeing all the docs as single line I went that route.

infact putting:

sensor:
  - platform: template
    sensors:
      time_at_work_rounded:
        value template: >
          {% set hours = states('sensor.time_at_work') | float %} 
          {% set minutes = ((hours % 1) * 60) | int %}  
          {% set hours = (hours - (hours % 1)) | int %}  
          {{ '%02i:%02i'%(hours, minutes) }}

gives me this error:

Invalid config for [sensor.template]: [value template] is an invalid option for [sensor.template]. Check: sensor.template->sensors->time_at_work_rounded->value template. (See ?, line ?). Please check the docs at https://home-assistant.io/integrations/sensor.template/

1 Like

Because you forgot to put the underscore character between value and template.

[value template] is an invalid option for [sensor.template]

3 Likes

Damnit! Thanks