Maths in conditions for automation

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:

  value_template: '{{ states.sensor.bathroom_humidity_2.state >= states.input_number.high_speed_humidity.state}}' 

I’ve tried to add the simple subtraction like this:

  value_template: '{{ states.sensor.bathroom_humidity_2.state >= states.input_number.high_speed_humidity.state - 5}}' 

The result is that the condition simply appears to never operate. Similarly these also don’t seem to work:

  value_template: '{{ states.sensor.bathroom_humidity_2.state >= states.input_number.high_speed_humidity.state|int - 5}}'
  value_template: '{{ states.sensor.bathroom_humidity_2.state >= states.input_number.high_speed_humidity.state|float}}' 

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.

What do you receive when you put any of those templates into the template editor/tester?

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}}'

Thanks @petro, that makes sense ! It’s curious that the greater than and less than comparisons work even on the strings.

I can’t log in from here at work except from my phone (that’s another thread). I’ll try this when i get home.

Tim

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 !

well anyways, I guess you’ll just have to update them:

This should emulate a 3+ way switch. I’d have to test it but can’t at the moment.

trigger:
- platform: state
  entity_id: switch.a
- platform: state
  entity_id: switch.b
action:
- service_template: >
    {% set switches = ['switch.a', 'switch.b'] %}
    {% set target = trigger.to_state.state %}
    {% set total_target = states | selectattr('entity_id', 'in', switches) | selectattr('state','eq', target) | list | length %}
    {% if total_target == switches | length or total_target == 0 %}
      light.turn_off
    {% else %}
      light.turn_on
    {% endif %}
  data_template:
    entity_id: light.a, light.b

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 :wink: . 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