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 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):
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 ?
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?
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
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?