Climate alert sensor won't work

This is probably pretty basic but I’m a noob regarding the template stuff…

I’m trying to create a “climate alerts” sensor that basically reports if the thermostat is on or off (1/0), for some reason, this code is working for someone else but I can’t get it to work for me… pls help.

In sensor.yaml:

# Climate alert sensor
- platform: template
  sensors:
    climate_on:
      friendly_name: "Climate on"
      unit_of_measurement: ''
      value_template: >
        {% set climate = [
          states.climate.3family_room,
          ] %}
        {{ climate | selectattr('state','eq','heat') | list | count + climate | selectattr('state','eq','cool') | list | count + climate | selectattr('state','eq','dry') | list | count + climate | selectattr('state','eq','fan_only') | list | count }}

You should use the new template format for new sensors.

Lists with one member don’t have a trailing comma. I assume you are going to add others in future? If not, why are you using a list?

Use rejectattr() to simplify you template. Just reject the ‘off’ state and count that.

1 Like

Maybe in the future but for the foreseeable future I only have just one thermostat. And can you give me an example please or link the documentation? Would like to get this sensor fixed.

Thanks.

Try this (untested)

configuration.yaml

template:
  sensor:
    name: "Climate on"
    state: >
      {% set climate = [
        states.climate.3family_room
      ] %}
      {{ climate | rejectattr('state','eq','off') | list | count  }}
```[wrap="center"]
Text
[/wrap]

That didn’t work unfortunately. Just noticed even my other sensors are throwing errors in the logs but they work so that’s weird.

template:
  - sensor:
      name: "Climate on"
      state: >
        {% set climate = [
          states.climate.3family_room
        ] %}
        {{ climate | rejectatt('state','eq','off') | list | count  }}
Logger: homeassistant.config
Source: config.py:868
First occurred: 3:42:16 PM (1 occurrences)
Last logged: 3:42:16 PM

Invalid config for [template]: invalid template (TemplateSyntaxError: expected token ',', got 'family_room') for dictionary value @ data['sensor'][0]['state']. Got None. (See /config/configuration.yaml, line 234).

Typo in rejectattr. You really need to understand these templates rather than copy and paste them. Retype them manually and look up every word or symbol you don’t understand.

2 Likes

Thanks. Fixed above.

Thanks for the help, sadly it didn’t work.

        {% set climate = [
          'states.climate.3family_room'
        ] %}

Using your code it appears to have kinda worked! Thanks.

One last thing it’s reporting the thermostat as on right now even though its off so how can I make it use the “hvac_action” attribute which actually reports when its cooling or heating?


  - sensor:
      - name: "Climate on"
        state: >
          {% set climate = [
          'states.climate.3family_room'
          ] %}
          {{ climate | rejectattr('state','eq','off') | list | count  }}
{{ climate | rejectattr('state','eq','off') | rejectattr('attributes.hvac_action', 'eq', 'idle') | list | count }}
1 Like

This makes the sensor report as unavailable now.

Then use the template editor to work out why.