Problem creating sensor template nest thermostat

Hello everyone, I have a Nest thermostat connected to my gas boiler, I have integrated the nest under HA but I have a problem, I would like to create a sensor to know how long the boiler is running per day and put after this in the form of a graph.
So I created these sensors in my sensor.yaml but it doesn’t want to work.
I don’t understand because in the models it works, but when I check the configuration it gives me an error…

- platform: template
    sensors:
      Nest Preset Mode:
        unique_id: nest_preset_mode
        state: "{{ state_attr('climate.salon','preset_mode') }}"
        attribute_templates:
          none: "{% if (state_attr('climate.salon','preset_mode') == 'none') %} ✔ {% else %} ­{% endif %}"
          eco:  "{% if (state_attr('climate.salon','preset_mode') ==  'eco') %} ✔ {% else %} ­{% endif %}"

- platform: template
    sensors:
      Nest Heating Runtime:
         unit_of_measurement: 'seconds'
         state: "{{state_attr('climate.salon','elapsed_seconds')|float(0) }}"

I also tried this to no avail…

- platform: template
    sensors:
      Nest Preset Mode:
        unique_id: nest_preset_mode
        state: "{{ state_attr('climate.salon','preset_mode') }}"
        value_template:
          none: "{% if (state_attr('climate.salon','preset_mode') == 'none') %} ✔ {% else %} ­{% endif %}"
          eco:  "{% if (state_attr('climate.salon','preset_mode') ==  'eco') %} ✔ {% else %} ­{% endif %}"

- platform: template
    sensors:
      Nest Heating Runtime:
         unit_of_measurement: 'seconds'
         value_template: "{{state_attr('climate.salon','elapsed_seconds')|float(0) }}"

Configuration non valide !

Invalid config for [sensor.template]: invalid slug Nest Heating Runtime (try nest_heating_runtime) for dictionary value @ data[‹ sensors ›]. Got OrderedDict([(‹ Nest Heating Runtime ›, OrderedDict([(‹ unit_of_measurement ›, ‹ seconds ›), (‹ value_template ›, « {{state_attr(‹ climate.salon ›,‹ elapsed_seconds ›)|float(0) }} »)]))]). (See ?, line ?).

It is complaining about your choice to use Nest Heating Runtime as part of the sensor’s entity_id. That’s invalid because an entity_id must be in lower-case and have no spaces (and no accented characters). That’s why it suggested you use nest_heating_runtime instead of Nest Heating Runtime.

Replace
Nest Heating Runtime:
with
nest_heating_runtime:

Replace
Nest Preset Mode:
with
nest_preset_mode:

Still an error even without capitals.

Invalid config for [sensor.template]: invalid slug nest preset Mode (try nest_preset_mode) for dictionary value @ data[‘sensors’]. Got OrderedDict([(‘nest preset Mode’, OrderedDict([(‘unique_id’, ‘nest_preset_mode’), (‘state’, “{{ state_attr(‘climate.salon’,‘preset_mode’) }}”), (‘value_template’, OrderedDict([(‘none’, “{% if (state_attr(‘climate.salon’,‘preset_mode’) == ‘none’) %} :heavy_check_mark: {% else %} \xad{% endif %}”), (‘eco’, “{% if (state_attr(‘climate.salon’,‘preset_mode’) == ‘eco’) %} :heavy_check_mark: {% else %} \xad{% endif %}”)]))]))]). (See ?, line ?).
Invalid config for [sensor.template]: invalid slug nest heating runtime (try nest_heating_runtime) for dictionary value @ data[‘sensors’]. Got OrderedDict([(‘nest heating runtime’, OrderedDict([(‘unit_of_measurement’, ‘seconds’), (‘value_template’, “{{state_attr(‘climate.salon’,‘elapsed_seconds’)|float(0) }}”)]))]). (See ?, line ?).

Still an error because it still contains spaces.

Invalid config for [sensor.template]: [state] is an invalid option for [sensor.template]. Check: sensor.template->sensors->nest_preset_mode->state. (See ?, line ?).

Correct, I fixed it, but I still have this error that persists.

That error is because there are two ways to configure a Template Sensor and you mixed the two formats.

Do you want modern or legacy format?

What you created starts with legacy then changes to modern.

state is an option supported by modern format. The equivalent in legacy format is value_template.

Change state to value_template.

- platform: template
    sensors:
      nest_preset_mode:
        unique_id: nest_preset_mode
        value_template: "{{ state_attr('climate.salon','preset_mode') }}"
        attribute_templates:
          none: "{% if (state_attr('climate.salon','preset_mode') == 'none') %} ✔ {% else %} ­{% endif %}"
          eco:  "{% if (state_attr('climate.salon','preset_mode') ==  'eco') %} ✔ {% else %} ­{% endif %}"

      nest_heating_runtime:
         unit_of_measurement: 'seconds'
         value_template: "{{state_attr('climate.salon','elapsed_seconds')|float(0) }}"

No more errors and the sensors present but the nest_heating_runtime sensor does not count the seconds and would it be possible to have minutes or hours ?

Which means 'problem creating sensor template ’ has been resolved.

Please consider marking my first post above with the Solution tag. It will automatically place a check-mark next to the topic’s title which signals to other users that this topic is resolved. This helps other users find answers to similar questions.

For more information about the Solution tag, refer to guideline 21 in the FAQ.

But the sensor does not count?
Related to another issue?

  1. Go to Developer Tools > States
  2. Find climate.salon
  3. Look at its attributes for elapsed_seconds.
  4. Whatever is the value of that attribute is what will be reported in the Template Sensor you created (sensor.nest_heating_runtime).

If you can’t find an attribute named elapsed_seconds (or if it’s value isn’t purely numeric) then it explains why the value of sensor.nest_heating_runtime is always 0.

indeed elapsed_seconds does not appear, how could I count the hours of operation of my boiler otherwise ? Is another method possible or do you have to create a sensor ?

Create a Template Sensor that reports the activity of climate.salon.

  - platform: template
    sensors:
      salon_activity:
        friendly_name: 'Salon Activity'
        value_template: "{{ state_attr('climate.salon', 'hvac_action') }}"

Create a History Stats sensor that records the amount of time the state of sensor.salon_activity is heating.

  - platform: history_stats
    name: Heating Today
    entity_id: sensor.salon_activity
    state: 'heating'
    type: time
    start: '{{ today_at() }}'
    end: '{{ now() }}'

The value of sensor.heating_today will be the number of hours that climate.salon is heating. The value will reset at midnight.

Thank you, I created these sensors, can’t wait until tomorrow to see if everything works correctly. I will come back to make a return.