Templating simple status change

Is there an easier way to do this (in my sensors.yaml)

- platform: template
  sensors :
    server_temp_0_warning
    friendly name: temp 0 warning 
    value_template: >-
      {% if ( states("sensor.server_temp_0") | float > 30 %)
        warning
      {% else %}
        ok
      {% endif %}

- platform: template
  sensors :
    server_temp_1_warning
    friendly name: temp 1 warning 
    value_template: >-
      {% if ( states("sensor.server_temp_1") | float > 30 %)
        warning
      {% else %}
        ok
      {% endif %}

...

repeat 28 more time

I have the sensors in a group is that help

PS this is used to set the backgroud colour in my picture element card (conditional on server_temp_0_warning – if OK then green, if warning then red which all works, just need to find a better way of doing this

If I understand correctly, you have 30 temperature sensors. You want a warning message when a sensor’s state value exceeds 30.

The easiest route is a single Template Sensor that shows a count of the number of sensors that exceed the threshold. Optionally, it can have an attribute that lists the names of all the sensor’s that are running hot.

Does that sound acceptable to you?

So unfortunately it’s not quite as simple as that. Each temp sensors has its own threshold that is different.
What I have at the moment works but I have only done or for two sensors because of the large amount of code it will take.
The sensors are also in a group if that helps.

I also tried creating an automation to detect when the temp sensors value goes above a threshold then change the status of the warning but you can’t change the status of a sensor from within an automation so that didn’t work.

I am parsing the original values from CSV to JSON in python so I might make another JSON with warning values in it at that point of I can’t find a simpler solution.
It seems crazy!

The fact each one has a unique threshold is what forces your hand to create a separate Template Sensor for each one. In this case, having them in a group adds little flexibility.

What I can offer is a streamlined way to define the sensors. It makes it easier to create additional sensors, via copy-paste, then just tweak the index and threshold values.

- platform: template
  sensors:
    server_temp_0_warning:
      friendly_name: 'temp 0 warning'
      value_template: "{{ 'warning' if ( states('sensor.server_temp_0') | float > 30 else 'ok' }}"

    server_temp_1_warning:
      friendly_name: 'temp 1 warning'
      value_template: "{{ 'warning' if ( states('sensor.server_temp_1') | float > 35 else 'ok' }}"

    server_temp_2_warning:
      friendly_name: 'temp 2 warning'
      value_template: "{{ 'warning' if ( states('sensor.server_temp_2') | float > 28 else 'ok' }}"

    server_temp_3_warning:
      friendly_name: 'temp 3 warning'
      value_template: "{{ 'warning' if ( states('sensor.server_temp_3') | float > 39 else 'ok' }}"

However, if this was just ‘demo code’ and the sensor names have little in common and aren’t sequential, then you’re still facing the tedium of renaming each one.