Anyone know how to use the old_state of an input_slider

Does anyone know how to get the old_state of an input_slider? I want to be use the input_slider to control the volume of an amplifier using lirc. Thus I need to know what the last value of the slider was to know whether it went up or down and to thus send the correct command.

TIA

Can you share the automation you have so far, if any? And also, I believe itā€™s from_state and not old_state.

Hi

Thanks for the responseā€¦I thought I had posted a reply to you. Doh!

Iā€™ve spent some time learning HA and I still think the from_state value is not made available ie it is not a state property.

Here is the automation:

automation 3:
alias: Sony Amp Volume Set
trigger:
platform: state
entity_id: input_slider.ampvol
action:
service: notify.notify
data:
message: ā€˜Volume has gone from {{ states.input_slider.ampvol.from_state}} to {{ states.input_slider.ampvol.state}}ā€™

The message I receive is:

Volume has gone from to 14.0

It doesnā€™t matter whether I use from_state or old_state.

I can make automations work when the trigger is from a value to another value so the triggering code ā€œknowsā€ the old_state but I need to know simply if the difference between old_state and current state is positive or negative. Thus I would know whether to turn volume down or up.

TIA

It may not fix the issue, but try this:

automation 3:
  alias: Sony Amp Volume Set
  trigger:
    platform: state
    entity_id: input_slider.ampvol
  action:
    service: notify.notify
    data:
      message: 'Volume has gone from {{ trigger.from_state.state }} to {{ trigger.to_state.state }}'

Hi. Just for anyone elseā€™s benefit Davidā€™s answer is almost right. This requires the data_template to be used thus the following does work OK:

automation 3:    
  alias: Sony Amp Volume Set
  trigger:
    platform: state
    entity_id: input_slider.ampvol
  action:
    service: notify.notify
    data_template:
      message: 'Volume has gone from {{ trigger.from_state.state }} to {{ trigger.to_state.state }} '
1 Like

Oh, oops! Seems I overlooked that bit, but then I couldnā€™t actually test my code. Glad itā€™s working now, though :smile:

and an Oops! from me. I forgot to thank you for your help. Ta.

Ok so one tiny step forward. Now Iā€™m trying to build an if statement into the data_template but it fails. This is the automation:

automation 3:    
  alias: Sony Amp Volume Set
  trigger:
    platform: state
    entity_id: input_slider.ampvol
  action:
     service: notify.notify
     data_template: 
        message: {% if trigger.to_state.state | int - trigger.from_state.state | int < 0 %}
                           'Volume has gone DOWN! '
                       {% else %}
                           'Volume has gone UP! or stayed the same'
                       {% endif %} 

The error log shows the following and homeassistant does not start:

16-08-31 20:21:52 homeassistant.util.yaml: while scanning for the next token
found character '%' that cannot start any token
in "/home/hass/.homeassistant/configuration.yaml", line 65, column 17
automation 3:    
  alias: Sony Amp Volume Set
  trigger:
    platform: state
    entity_id: input_slider.ampvol
  action:
    service: notify.notify
    data_template: 
      message: >-
        {% if (trigger.to_state.state | int - trigger.from_state.state | int) < 0 %}
          Volume has gone DOWN!
        {% else %}
          Volume has gone UP! or stayed the same
        {% endif %}

The main issue is that your message (the entire if-statement in your case) either needs to be quoted, or started with > followed by a newline. Using >- instead of just > strips unnecessary newlines and spaces from the text being shown.

I also wrapped your if between parentheses so the entire thing gets compared to 0, not just the from state. Or you could use the following if instead: if trigger.to_state.state | int < trigger.from_state.state | int

Finally, I just fixed the indentation here and there.

1 Like

Wow! You are a champion. Thanks. Worked perfectly and I will give me a rap on the knuckles for not carefully looking at syntax. Thanks againā€¦now to send the correct lirc commands