Template sensor occasionaly stops updating

I’m having a hard time figuring this out. I have a template sensor to get the maximum setting of all my thermostat valves on our radiatiors, defined as follows:

- platform: template
  sensors:
    thermostats_max:
        friendly_name: "Thermostats max"
        unit_of_measurement: '°C'
        value_template: >
            {{
               max(
                   state_attr('climate.FF_kok_trv', 'temperature') or -10001,
                   state_attr('climate.FF_matrum_s_trv', 'temperature') or -10002,
                   state_attr('climate.FF_matrum_o_trv', 'temperature') or -10003,
                   state_attr('climate.FF_vardagsrum_trv', 'temperature') or -10004,
                   state_attr('climate.FF_gym_o_trv', 'temperature') or -10005,
                   state_attr('climate.FF_gym_n_trv', 'temperature') or -10006,
                   state_attr('climate.FF_kontor_n_trv', 'temperature') or -10007,
                   state_attr('climate.FF_kontor_v_trv', 'temperature') or -10008,
                   state_attr('climate.FF_hall_trv', 'temperature') or -10009,
                   state_attr('climate.SF_gastrum_trv', 'temperature') or -10010,
                   state_attr('climate.SF_pinball_trv', 'temperature') or -10011,
                   state_attr('climate.SF_sovrum_trv', 'temperature') or -10012,
                   state_attr('climate.SF_badrum_trv', 'temperature') or -10013,
                   state_attr('climate.SF_studio_trv', 'temperature') or -10014
               )
            }}

The climate.*_trv sensors are all Shelly TRV’s and configured as manual mqtt entities like this:

  - name: "FF_kök_trv"
    min_temp: 4
    max_temp: 31
    temp_step: 0.5
    modes:
    current_temperature_topic: shellies/shellytrv002/info
    current_temperature_template: "{{ value_json.thermostats[0].tmp.value }}"
    temperature_command_topic: shellies/shellytrv002/thermostat/0/command/target_t
    temperature_state_topic: shellies/shellytrv002/info
    temperature_state_template: "{{ value_json.thermostats[0].target_t.value }}"

(note that the name FF_kök_trv results in the id ff_kok_trv for this particular sensor)

When home assistant starts, sensor.thermostats_max correctly follows the thermostat settings, giving me the maximum. However, from time to time, it gets stuck and returns -10001 instead. This would indicate that FF_kök_trv has no temperature value, but when I go into the developer tools and check the state of that sensor, it does show a temperature. For example, right now the sensor.thermostats_max gives me -10001, but climate.ff_kok_trv’s temperature attribute shows 20. If I change the thermostat setting to 21, that is correctly reflected in climate.ff_kok_trv, but sensor.thermostats_max keeps giving me -10001.

What’s more, when the template sensor gets stuck and I copy the template into the Template tool under Developer Tools, it does evaluate to the correct value (20 usually), and if I change the thermostat to 21, the template evaluation immediately updates to show 21. Yet the actual sensor using the template stays stuck at -10001.

Restarting home assistant will get the template sensor to behave again. Sometimes it will get stuck again within a day, sometimes it will be fine for weeks before it gets stuck again.

I’m sure I’m missing something, I doubt it’s a bug in home assistant. But I’m not sure what I’m missing.

I saw this thread which seems to be discusing something similar: Template sensor doesnt update with parent sensor - #13 by MegaNiklas

It has a solution, but I’m not sure I understand what the solution is. In the comment marked as the solution, Petro mentions:

I’m not sure I understand what it means though. And does it even relate to my problem? I do see the template sensor update correctly after a restart, it only occasionally gets stuck.

1 Like