Counting temp sensors over 45 degrees

I am trying to get a count of temp sensors that are over 45 degrees. this template sensor keeps reporting unavailable.
suggestions on the best way to do this?

- platform: template
  sensors:
      fridge_sensor_count_over_45:
        friendly_name: "Count Fridge Sensors over 45"
        value_template: >-
          {{ [
               states('sensor.fridge_temperature') | float > 45,
               states('sensor.garage_fridge_temperature') | float > 45,
               states('sensor.garage_freezer_temperature') | float > 15,
               states('sensor.down_fridge_temperature') | float > 45,
             ] | select('==', true) | length }}

this seems to be working and returns false (when the temp is below 45) in my template editor

{{ (states('sensor.fridge_temperature') | float > 45 )

You really should be using the template integration for new template sensors.

template:
  - sensor:
      - name: "Count Fridge Sensors over 45"
        state: >
          {{ states.sensor
            | selectattr('object_id', 'search', 'fridge_temperature$')
            | map(attribute='state')
            | map('float', 0)
            | select('>', 45)
            | list
            | count }}

Matches all sensors that end in fridge_temperature then counts the number who’s state value is greater than 45.

thanks for the help. In order to use the new template integration format do I have to convert every template in my sensors.yaml file to the new format? is there a a good guide on making these changes?
also can I use two selectattr lines (to grab the freezer) as follows?

{{ states.sensor
            | selectattr('object_id', 'search', 'fridge_temperature$')
            | selectattr('object_id', 'search', 'freezer_temperature')
            | map(attribute='state')
            | map('float', 0)
            | select('>', 45)
            | list
            | count }}

No. There is no indication that the legacy format is going away and it can exist alongside the new format. The legacy format does not get new features though. e.g. state_class is only supported by the new format. This is why you should use it for new sensors.

Not the way you have it. That will require both fridge_temperature and freezer_temperature to be in the entity_id. You want or logic:

{{ states.sensor
    | selectattr('object_id', 'search', '(fridge_temperature|freezer_temperature)')
    | map(attribute='state')
    | map('float', 0)
    | select('>', 45)
    | list
    | count }}

thanks for the help. I created a template.yaml file with your suggested code and it works great.

1 Like