Template sensor that just... won't... work (reliably)

I have one sensor configuration that just refuses to start reliably. It’s pulling the fan speed from a zwave fan control, and only ends up reporting nothing. Periodically after a reboot it works.

Here is the sensor from which it’s reporting:

fan.great_room_fan_level	on	
  speed_list: off, low, medium, high
  speed: low
  node_id: 3
  value_index: 0
  value_instance: 1
  value_id: 72057594093076481
  friendly_name: Great Room Fan Level
  supported_features: 1
  icon: mdi:fan

The template sensor is attempting to extract the fan speed. It has bloated as I’ve tried elements from other threads and to make it seemingly bulletproof, to no avail:

  - platform: template
    sensors:

         ... other sensors ...

      great_room_fan_speed:
        friendly_name: "Great Room Fan Speed Setting"
        value_template: >-
          {% set tu = states.sensor.uptime.state | float %}
          {% if tu > 0.03 %}
            {% if is_state('fan.great_room_fan_level', 'Unknown') %}
              'off'
            {% elif is_state('fan.great_room_fan_level', 'on') %}
              {% if states.fan.great_room_fan_level.attributes.speed == 'off' or
                states.fan.great_room_fan_level.attributes.speed == 'low' or
                states.fan.great_room_fan_level.attributes.speed == 'medium' or
                states.fan.great_room_fan_level.attributes.speed == 'high' %}
                {{ states.fan.great_room_fan_level.attributes.speed }}
              {% else %}
                'low'
              {% endif %}
            {% else %}
              'off'
            {% endif %}
          {% else %}
            'off'
          {% endif %}
        icon_template: mdi:fan
        entity_id:
          - fan.great_room_fan_level

The entity gets created but nothing is ever reported in the “state” field.

I started off just making sure the entity fan.great_room_fan_level existed and added complexity from there. The last attempt was to force it to ‘off’ until home assistant has been running for a few minutes, but I’m guessing the issue is somewhere else.

One other bit of information is that I originally had this zwave fan control running from Vera, which reported this as a “brightness” from 0 to 255. When I first moved this to an Aeotec stick, the entity it created reported the speed as controllable from 0 to 255, but then after some time changed to the ‘off’, ‘low’, etc.

I’m wondering if something happens during startup that’s confusing either openzwave or HA. I can run the above config (and it’s dozen or so predecessors) in the template and it reports correct results.

If I run home_assistant.update_entity to update this sensor, it stays stuck (so to speak) even though the template reports correct results from the fan entity – is there some behind-the-scenes update process that gets stuck at startup and blocks updating of the entity until it completes? I’m somewhat lost how to troubleshoot this further.

No errors or warnings in the log, although I haven’t enabled debug level logging. That will be my next step, I guess, when next I try something else.

Try this:

      great_room_fan_speed:
        friendly_name: "Great Room Fan Speed Setting"
        icon_template: mdi:fan
        entity_id: fan.great_room_fan_level
        value_template: >-
          {% if states('sensor.uptime') | float > 0.03 
             and states('fan.great_room_fan_level') != 'unknown'
             and states('fan.great_room_fan_level') == 'on' %}
               {% set s = state_attr('fan.great_room_fan_level', 'speed') %}
               {{ s if s in ['off', 'low', 'medium', 'high'] else 'low' }}
          {% else %}
            off
          {% endif %}

So in the interests of testing your simplified logic which requires a restart, I changed logging for sensors to debug. Under log:

homeassistant.components.sensor: debug

Lo and behold… I have a duplicate sensor name defined as a leftover from when I was using the Vera controller. (This explains why it was intermittent, it just depends on which sensor gets created first and blocks the other.)

So thank you for suggesting a change and for the new-to-me syntax:

{{ s if s in ['off', 'low', 'medium', 'high'] else 'low' }}

That’s a peculiar situation because Home Assistant normally attempts to disambiguate duplicates by appending _2 to the duplicated entity’s name. For example, this:

  - platform: mqtt
    name: "Duplicate"
    state_topic: "home/none"

  - platform: mqtt
    name: "Duplicate"
    state_topic: "home/none"
    device_class: door

produces two distinct entities:

Screenshot from 2020-04-12 15-23-55

Perhaps when the Vera integration automatically records entities in the entity registry, Home Assistant doesn’t perform any disambiguation for duplicated entity names you create manually.

Just to tie up loose ends, how did you remove the duplicated entity?

Just to tie up loose ends, how did you remove the duplicated entity?

I had an identically named fan speed template sensor in a different yaml package file that I just deleted. It was pointing to the nonexistent entity that was originally created by the vera component, thus the blank stare HA was giving me on the sensor.

I wish I would have saved the actual log message but it was something like “ignoring because it’s a duplicate”. I have seen the _2 type modifications to other entities but in this case HA didn’t attempt that, it just ignored the 2nd instance and logged that it was doing this.

Package files have the ‘double-edged’ sword of organization and obfuscation. Things long-buried, return to haunt us.

/s

That’s one of the reasons I name entities created in a package (all mine are) after the package.
e.g.

sensor:
  - platform: template
    sensors:
      s_occupancy_ladyinresidence:

comes from my occupancy package, it starts with an s_ so is a ‘sensor’ and is always easy to find in an alpha sorted list

Charles Simonyi applauds your choice of naming convention. :slight_smile:

I hear he doesn’t get out of bed for less than 3.5 billion.?