Automation turns on / off the fan at a certain humidity difference between the two sensors. When calling the service, an error appears in the log. But automation works. Incorrect syntax? I ask for help.
Error executing script. Error for call_service at pos 1: Template rendered invalid service:
action:
service: >
{% if states('sensor.ble_humidity_a4c1385a4a7a') | int >= states('sensor.ble_humidity_a4c1388a4303') | int + 6 %}
switch.turn_on
{% elif states('sensor.ble_humidity_a4c1385a4a7a') | int <= states('sensor.ble_humidity_a4c1388a4303') | int + 4 %}
switch.turn_off
{% endif %}
entity_id: switch.ventvanna_2
Please post the error and your automation config, correctly formatted as explained in point 11 here.
Thanks for sorting the formatting.
Ok, what happens if neither of those statements are true?
You get no service.
I understand, thank you, I will try to correct it
I did, it seems, there are no errors
action:
- condition: template
value_template: >
{{ states('sensor.ble_humidity_a4c1385a4a7a') | int >= states('sensor.ble_humidity_a4c1388a4303') | int + 6 }}
- service: switch.turn_on
entity_id: switch.ventvanna_2
- condition: template
value_template: >
{{ states('sensor.ble_humidity_a4c1385a4a7a') | int <= states('sensor.ble_humidity_a4c1388a4303') | int + 4 }}
- service: switch.turn_off
entity_id: switch.ventvanna_2
The problem with doing it that way is that if the first condition is false, nothing will happen. Execution of the actions stops there. Use the choose action. Having none of the choices happen is valid and will not generate an error.
action:
- choose:
- conditions: "{{ states('sensor.ble_humidity_a4c1385a4a7a') | int >= states('sensor.ble_humidity_a4c1388a4303') | int + 6 }}"
sequence:
- service: switch.turn_on
entity_id: switch.ventvanna_2
- conditions: "{{ states('sensor.ble_humidity_a4c1385a4a7a') | int <= states('sensor.ble_humidity_a4c1388a4303') | int + 4 }}"
sequence:
- service: switch.turn_off
entity_id: switch.ventvanna_2
More here: https://www.home-assistant.io/docs/scripts#choose-a-group-of-actions
Thanks for the code and link. It works. The link is exactly what I myself could not find