How to calculate fan setpoint based on CO2 in HA script?

Hi all,

Comming from Domoticz and used to Python and Dzvents I find myself struggling a lot with the HA script styles.

After trying for hours I would like to get some advise on how to script this very simple thing. I have a CO2 meter en want a, more or less, stepless calculation to send to my ventilation unit.

In domoticz it was just:

Setpoint = ((CO2 - MinPPM) / Range) * 100 
domoticz.devices('MV box zolder').dimTo(Setpoint)

In HA I try to do the same thing by using this script portion (with fixed numbers for testing, later I need the numbers to be variables who depent on others sensors):

           - service: light.turn_on
             metadata: {}
                 value_template: >-
                 {{ ((sensor.esp_woonkamer_co2_woonkamer - 610)/(610-1350)) * 100 | float }}
               entity_id: light.mv_box_zolder

All I get is an error message “Kan service script/co2_sturing_mv_box_zolder niet aanroepen. Error rendering data template: UndefinedError: ‘sensor’ is undefined”

So I tried this:
{{ ((states("sensor.esp_woonkamer_co2_woonkamer") | float - 610)/(610-1350)) * 100 | float }}

But that errors in:
"Kan service script/co2_sturing_mv_box_zolder niet aanroepen. expected float for dictionary value @ data['brightness_pct']"

These things where so easy in Domoticz, but it can’t be that hard in HA can it? What am I doing wrong here :slight_smile: ?

           - service: light.turn_on
             metadata: {}
               brightness_pct: >
                 {{ (( states('sensor.esp_woonkamer_co2_woonkamer')|float(0) - 610) / (610 - 1350)) * 100 }}
               entity_id: light.mv_box_zolder
1 Like

Thanks @tom_l ! I tried your code but got:

Kan service script/co2_sturing_mv_box_zolder niet aanroepen. value must be at least 0 for dictionary value @ data['brightness_pct']

I did copy the code multiple times to make sure it was correctly copied, but it doenst seems to be working.

Sounds like you need to adjust your equation.

What value does this return in the developer tools → template editor:

{{ (( states('sensor.esp_woonkamer_co2_woonkamer')|float(0) - 610) / (610 - 1350)) * 100 }}

I tested it with my CO2 sensor, the template is valid.

If you need to, you can set bounds to your equation output (e.g. 0 to 100%) like this:

               brightness_pct: >
                 {{ ([0, (( states('sensor.esp_woonkamer_co2_woonkamer')|float(0) - 610) / (610 - 1350)) * 100, 100]|sort)[1] }}

This sorts the list of [0,{{value}},100] by increasing value then pics the middle one [1] (the list element index starts counting at 0).

Yes, that was the reason. When running the template editor I saw a < 0 value. Turns out that the equation was wrong indeed (610 - 1350 → 1350 - 610).

Thanks for you assistance! Helped me out a lot. Bottemline was that I should not have used the value_template: key but instead use the brightness_pct: key for the equation to take place.

One more thing that I encountered is that it seems impossible to show the brightness_pct value on my dashboard. The device does not have that attribute when I look up the states in developertools. I does have de brightness attribute, but that is from 0-255 instead of 0-100 percent.

Is there a way to convert this when showing on the dashboard?

I tried:

type: entity
entity: light.mv_box_zolder
attribute: brightness
unit: '%'
value_template: {{ (states('light.mv_box_zolder.brightness')|float() / 2.55)}}

But seems like the attribute brightness can’t be found in the value_template part. (when removed the sensor works, but shows 0-255 output). When trying it with:

value_template: {{ (states('light.mv_box_zolder')|float() / 2.55)}} it show that the state ‘on’ was received and the calculation can’t be done, which makes sense as the number value is in the brightness attribute.

@tom_l , do you see where I need to go to get the correct 0-100% output? Learning a lot these days :smiley:

Edit: I saw the compensation integration, but that would be overengineering such an easy problem I guess.

Edit2: Ah, I fixed the missing attribute issue by finding this formatting:
{{ (state_attr('light.mv_box_zolder','brightness')|float() / 2.55)}}
But how to get this now correct formula to show this value on the dashboard?