Automation template - how to detect slider last and new state and compare it in action template

Here is link if someone have similar ir light.

I try to make new automation + python I already have working version with one problem.

Here is link.

On remote I have only one button of on and off. When I decrease fan speed and when I add slider to 1 automation call FAN + DOWN in python script because I configured it like that:

part from automation:

{% elif slider == 1 %}
  6

part from python:

elif cmd == 6:
service_data = {‘host’:‘192.168.2.1’, ‘packet’:’{}’.format(rf[‘FAN’])}
hass.services.call(‘broadlink’, ‘send’, service_data, False)
time.sleep(0.55)
loop = 1
rfc = rf[‘DOWN’]

Question: What I must add to line {% elif slider == 1 %} to only trigger that line when I increse slider?

When diff is positive number. --> {% set diff = (from - to) | round(0) %}

I figured out. Simple :slight_smile:

{% elif slider == 1 and diff == '-' %}

:thinking:

If diff is the result of a subtraction then diff == '-' will always return false.

Screenshot%20from%202019-05-05%2011-37-46

I don’t know theoretically but I did like I say and is working :sunglasses:

I’m glad to hear it is working for you. Although, honestly, I don’t know what you expect it to do because the following statement will always evaluate to false.

{% elif slider == 1 and diff == '-' %}

For the elif statement to evaluate to true, both slider==1 and diff='-' must be true. The problem is diff=='-' is always false. So this elif can never be true and whatever code is underneath it will never execute.

Screenshot%20from%202019-05-05%2014-12-44

1 Like

You should listen to @123. He’s right. This will never ever be anything other than false.

This should only trigger on positive differences.

{% elif slider == 1 and diff > 0 %}

I don’t know why but solution you suggested not working.

My solution works :slight_smile:

{% elif slider == 1 and diff == '-' %}

Well you haven’t posted your full template so we are assuming you’re doing the math correctly. If you post the full template, we can help steer you towards a correct template and a robust automation.

Here is link

Link is dead, is that a private repo? Can you just copy/paste the template?

####################################################
# FAN CONTROL
####################################################

- alias: 'ir_kitchen_hood_fan_set'
  trigger:
    platform: state
    entity_id: input_number.ir_kitchen_hood_fan_speed
  action:
    service: python_script.ir_kitchen_hood_fan_control
    data_template:
      command: >-
        {% set to = trigger.to_state.state | int %}
        {% set from = trigger.from_state.state | int %}
        {% set diff = (from - to) | round(0) %}
        {% set slider = states('input_number.ir_kitchen_hood_fan_speed') | round(0) %}
        {% set step = (diff / 1) | round(0) %}
        {% if slider == 0 %}
          0
        {% elif slider == 1 and diff == '-' %}
          6
        {% else %}
          {{step}}
        {% endif %}

Link is now working.

What are the values for from and to that make diff equal to -

{% set diff = (from - to) | round(0) %}

Answer: There are no values that can make diff equal to -

This is true:

4 - 6 = -2

this is not possible:

4 - 6 = -

You keep saying ‘it works’ so you’re suggesting that this statement will evaluate to true

        {% elif slider == 1 and diff == '-' %}
          6

The problem is it can never evaluate to true because diff == '-' is always false. It doesn’t matter if slider==1 is true, the other half is always false. Therefore, the elif will never report 6.

Whatever way you think this thing ‘works’ is not working the way you think it does.

Anyway, I’ve done my best to try to convince you that this can never be true:

4 -6 = -

I wish you luck with all of your future automations.

I appreciate all your help.

I don’t know why my automation is working, make no sense.

But it works.

for giggles, remove the elif statement and see if it still works. I’m guessing it will.