I’m struggling to understand the syntax for simple maths in value templates for conditions.
I have a DHT22 sensor in the bathroom that turns the extractor on if the humidity is above a limit held in an input_number on a lovelace card. This works well, and has turned on the fan after showers etc. The condition logic to compare the value returned by the humidity sensor and the input_number value is fine.
The fan has some speed setting jumpers, and I’d like to be able to run slow and then medium speed. I currently have this operatiing using two input_number set points, but would like to just use one, and trigger slow speed at say a humidity that is 10% under the medium speed setpoint.
I’m struggling to understand the syntax to make this work. The basic comparison works fine like this:
Any pointers about what I’m doing wrong and how this is supposed to work would be gratefully received. I’m a mechanical engineer, without a significant background in languages that work anything like Jinja, and its proving to be real struggle to get to grips with it.
any state is a string. If you want to compare number numerically, they need to be cast as a specific type. Both need to be cast. In all your attempts, you only cast one side.
value_template: '{{ states.sensor.bathroom_humidity_2.state | int >= states.input_number.high_speed_humidity.state | int - 5}}'
Yes, characters have a value associated to them. The comparison starts left to right and compares each character. You can get into shit situations becasue 9 is greater than 10 if you start left to right.
In my limited forays into C and PLC code I’ve always steered clear of strings, with enough grief from casting between byte, long, signed/unsigned etc. Now I’m even more glad !
There’s a whole lot that I don’t know about yet. I’ve only just started setting up what i want HA to do with value templates, and now I see service templates and data templates in your example, along with a syntax involving % that reads much more logically to me - I’ll need to dig around some more !
You can’t make a 3 way switch without that logic. Just add the automation and test it out. If it doesn’t work report back and I can help you tailor it. Otherwise this will be an uphill slog. You don’t need to understand it to use it . But the templating syntax is Jinja, so look that documenation on google or look here. As for the trigger.to_state.state, that documentation is located here. And normal templating documentation is here.
I chickened out on the more complex stuff I’m afraid. casting both the sensor and input_number to numbers worked a treat. I have two tests, one to turn the fan on, and a second to turn the speed up if the humidity rises too far. Seems to be working a treat - many thanks @petro