Create entity based other entity values

Hi Friends!

I’ve find lot of similar topic but no one help me solve this issue.

I’ve an air quality sensor based on ESP. It can measure different values. My goal is set a category from 1-6 for each measured values. The ranges of measured values are not the same and this is why i do not want use automations for all possible outputs. My idea was a template sensor but it does not appear in my entity list, here is my code.

template:
  - trigger:
    - trigger: state
      entity_id: sensor.aiq1_h1_pm1
    sensor:
      - name: "aiq1 pm1 cat"
        state: >
          {% set a1pm1 = states('sensor.aiq1_h1_pm1') | float %}
          {% if a1pm1 >= 0 and a1pm1 < 6.7 %}
            {% set aiq1h1pm1 = 1 %}
          {% elif a1pm1 >= 6.7 and a1pm1 < 26.2 %}
            {% set aiq1h1pm1 = 2 %}
          {% elif a1pm1 >= 26.2 and a1pm1 < 41.2 %}
            {% set aiq1h1pm1 = 3 %}
          {% elif a1pm1 >= 41.2 and a1pm1 < 93.7 %}
            {% set aiq1h1pm1 = 4 %}
          {% elif a1pm1 >= 93.7 and a1pm1 < 168.7 %}
            {% set aiq1h1pm1 = 5 %}
          {% elif a1pm1 >= 168.7 %}
            {% set aiq1h1pm1 = 6 %}
          {% else %}
            {{ a1pm1 }}
          {% endif %}
      - name: "aiq1 pm25 cat"
        state: >
          {% set a1pm25 = states('sensor.aiq1_h1_pm2_5') | float %}
          {% if a1pm25 >= 0 and a1pm25 < 9 %}
            {% set aiq1h1pm25 = 1 %}
          {% elif a1pm25 >= 9 and a1pm25 < 35.4 %}
            {% set aiq1h1pm25 = 2 %}
          {% elif a1pm25 >= 35.4 and a1pm25 < 55.4 %}
            {% set aiq1h1pm25 = 3 %}
          {% elif a1pm25 >= 55.4 and a1pm25 < 125.4 %}
            {% set aiq1h1pm25 = 4 %}
          {% elif a1pm25 >= 125.4 and a1pm25 < 225.4 %}
            {% set aiq1h1pm25 = 5 %}
          {% elif a1pm25 >= 225.4 %}
            {% set aiq1h1pm25 = 6 %}
          {% else %}
            {{ a1pm25 }}
          {% endif %}

I will be happy for any idea.

I’ve programmed the ESP to measure in every 5 minutes for 30 sec and swich of the sensor, this is why i try to use the trigger-> state template.

When it will work i have to check all air quality sensor and choose the biggest value (1-6, the biggest is the worst) and this will be the quality of the air.

You don’t have an expression to retrieve the values for the variables you are assigning to i.e. a1pm25. Either get rid of the set statements or add an expression at the end.

Also, if you aren’t going to assign a default value for the float filter, you should really set an availability:

...
      - name: "aiq1 pm25 cat"
        state: >
          {% set a1pm25 = states('sensor.aiq1_h1_pm2_5') | float %}
          {% if 0 <= a1pm25 < 9 %}
            1
          {% elif 9 <= a1pm25 < 35.4 %}
            2
          {% elif 35.4 <= a1pm25 < 55.4 %}
            3
          {% elif 55.4 <= a1pm25 < 125.4 %}
            4
          {% elif 125.4 <= a1pm25 < 225.4 %}
            5
          {% elif a1pm25 >= 225.4 %}
            6
          {% else %}
            {{ a1pm25 }}
          {% endif %}
        availability: "{{ has_value('sensor.aiq1_h1_pm2_5') }}"

Thank you so much for your fast and accurate reply!
Withoute set it was working and you were right with the availability.