I have several CO2 Sensors and have config a view with nice graphes.
Now I want to combine these sensors for the main view to show only an alert if one is over a limit. So I like to show a status ‘CO2 is OK’, or if one goes over a limit ‘CO2 alert at Bedroom’. Any idea how to start with this?
as the value_template. Replace sensor_name with the entity ID of the sensor, and replace threshold with whatever value is unsafe. Also set device_class to safety in the template sensor. These sensors will display Unsafe if the level is above the threshold, and Safe if it’s below the threshold.
Then use an entity filter card in your frontend to only display those template sensors with a state of on (Unsafe).
You could use a template sensor instead of a binary template sensor if you wanted the output to say “CO2 alert at [location]” like you wanted, but it’s simpler to set up a template binary sensor (and it might still fit your needs).
To clarify, I meant you could set up a template sensor for each CO2 monitor to individually display “CO2 alert at [location].” You’d still end up with several sensors instead of one with a for loop in a single template sensor.
Although, how many CO2 monitors do you have? Because you could do exactly what you want to accomplish with a single template sensor without a for loop, but it would just be more complicated. If you had three sensors, you could do something like this as the value_template in a single template sensor (this example assumes sensor1 is Bedroom, sensor2 is Kitchen, and sensor3 is Guest Room):
{% set threshold = 100 %}
{% if float(states('sensor.sensor1')) > threshold and float(states('sensor.sensor2')) > threshold and float(states('sensor.sensor3')) > threshold %}
CO2 Bedroom, Kitchen, Guest Room alert
{% elif float(states('sensor.sensor1')) > threshold and float(states('sensor.sensor2')) > threshold %}
CO2 Bedroom, Kitchen alert
{% elif float(states('sensor.sensor1')) > threshold and float(states('sensor.sensor3')) > threshold %}
CO2 Bedroom, Guest Room alert
{% elif float(states('sensor.sensor2')) > threshold and float(states('sensor.sensor3')) > threshold %}
CO2 Kitchen, Guest Room alert
{% elif float(states('sensor.sensor1')) > threshold %}
CO2 Bedroom alert
{% elif float(states('sensor.sensor2')) > threshold %}
CO2 Kitchen alert
{% elif float(states('sensor.sensor3')) > threshold %}
CO2 Guest Room alert
{% else %}
CO2 OK
{% endif %}
Change the sensor names and outputs as necessary, and set the threshold to whatever you want. You can see this is ridiculously complicated but it would indeed accomplish what you’re looking for. It becomes even more complicated if you have more than three sensors, but the concept is the same.
This is why I wish I could understand for loops…I’ve been trying.
If you use the built-in Developer Tools \ Automation Editor, the example that comes up on the default screen has a good, working example of a for loop iterating through a collection.
{% set threshold = 1000 %}
{% set output = namespace(text="CO² Warnung") %}
{% for entity in states.group.grp_co2.attributes.entity_id if float(states(entity)) > threshold %}
{{- entity}}
{% if float(states(entity)) > threshold %}
{{- states(entity)}}
{% set output.text = (output.text ~ entity.friendly_name) %}
{% endif %}
{% else %}
{% set output.text = "CO² OK" %}
{% endfor %}
{{output.text}}
The only thing not working is ‘{% set output.text = (output.text ~ entity.friendly_name) %}’ here the entity object do not know the friendly_name or name. I want to get a list of the sensors are over the treshhold.