Automations: Template condition syntax

Hello,

I’ve been struggling to get the template syntax correct.
Basically I want to enable the humidifer switch if the humidity drops above a setpoint and enable when below the setpoint

I got this working without templates, but I need to move to template because I want to enable when below setpoint-offset en disable when above setpoint+offset.

This is my code: (without the offset), i get nog errors, but nothing is triggered:

alias: "Downstair, Humidity: Control V2"
description: ""
trigger:
  - platform: state
    entity_id:
      - sensor.bedroom_luchtvochtigheid
      - input_number.downstairs_humidity_request
      - input_boolean.downstairs_humidity_control
condition: []
action:
  - if:
      - condition: or
        conditions:
          - condition: state
            entity_id: input_boolean.downstairs_humidity_control
            state: "off"
          - condition: template
            value_template: >-
              {{(sensor.downstairs_luchtvochtigheid|int)>(input_number.downstairs_humidity_request|int)}}
    then:
      - service: switch.turn_off
        metadata: {}
        data: {}
        target:
          entity_id: switch.humidifier_schakelaar_2
    else:
      - if:
          - condition: template
            value_template: >-
              {{(sensor.downstairs_luchtvochtigheid|int)<(input_number.downstairs_humidity_request|int)}}
        then:
          - service: switch.turn_on
            metadata: {}
            data: {}
            target:
              entity_id: switch.humidifier_schakelaar_2
mode: single

what is wrong with:
{{(sensor.downstairs_luchtvochtigheid|int)>(input_number.downstairs_humidity_request|int)}}

and if corrected can I implemente an offset like this?.:
{{(sensor.downstairs_luchtvochtigheid|int)>(input_number.downstairs_humidity_request|int)+1}}

kind regards,
Nick

Try your template in Developer Tools Template to see if there are any errors.

You will see ‘sensor’ is undefined. Same with input_number.

You need to get the state property of the state object.

{{ (states.sensor.downstairs_luchtvochtigheid.state | int) > (states.input_number.downstairs_humidity_request.state | int) }}

Here’s my attempt, trying to work to Frenck’s ideals of “trigger off everything, let the conditions sort out the logic, minimise what’s in the action”.

  • Trigger off any change to any of the related entities
  • Condition sets for turn_on or turn_off get checked to see if we need to proceed:
    • turn_on set needs the switch to be off and the control to be on and low humidity
    • turn_off set needs the switch to be on and either the control to be off or high humidity
  • Action simply toggles the switch: if the automation gets that far, the switch is in the wrong position.

I’m using template condition shorthand syntax a lot here, as well as and/or shorthand.

alias: "Downstair, Humidity: Control"
trigger:
  - platform: state
    entity_id:
      - sensor.downstairs_luchtvochtigheid
      - input_number.downstairs_humidity_request
      - input_boolean.downstairs_humidity_control
condition:
  - or:
    - and:
      - "{{ is_state('switch.humidifier_schakelaar_2', 'off') }}"
      - "{{ is_state('input_boolean.downstairs_humidity_control', 'on') }}"
      - "{{ states('sensor.downstairs_luchtvochtigheid')|float(0) < states('input_number.downstairs_humidity_request')|float(0) - OFFSET }}"
    - and:
      - "{{ is_state('switch.humidifier_schakelaar_2', 'on') }}"
      - or:
        - "{{ is_state('input_boolean.downstairs_humidity_control', 'off') }}"
        - "{{ states('sensor.downstairs_luchtvochtigheid')|float(0) > states('input_number.downstairs_humidity_request')|float(0) + OFFSET }}"
action:
  - service: switch.toggle
    entity_id: switch.humidifier_schakelaar_2