State change, compare to previous value

Hi all,

I think it’s simple but I can’t get a good example.

I have a rotary encoder wich I want to use to select radiostations. When I turn CW, it counts up, CCW it counts down.

How do I make an automation like:

If (value >= previous value){
input_select.select_next:
“entity_id”: “input_select.radio_station”
}

and

If (value <= previous value){
input_select.select_previous:
“entity_id”: “input_select.radio_station”
}

1 Like

can you share more info like your sensor name?
so far I would see it working like this (assuming your rotary sensor is called rotaty_encoder):

- alias: Change Radio Station from Rotaty Encoder
  initial_state: true
  trigger:
    - platform: state
      entity_id: sensor.rotaty_encoder
  action:
    - service_template: >
      entity_id: input_select.radio_station
      input_select.{%- if trigger.from_state.state < trigger.to_state.state -%}select_next{%- else -%}select_previous{%- endif -%}

My sensor name is: sensor.rotary_value.

If i change that and put in in my automations I get an error:

Configuration invalid CHECK CONFIG
Error loading /home/homeassistant/.homeassistant/configuration.yaml: while scanning a simple key
in “/home/homeassistant/.homeassistant/automations.yaml”, line 81, column 7
could not find expected ‘:’
in “/home/homeassistant/.homeassistant/automations.yaml”, line 82, column 1

sorry indentation. try this:

- alias: Change Radio Station from Rotaty Encoder
  initial_state: true
  trigger:
    - platform: state
      entity_id: sensor.rotary_value
  action:
    - service_template: >
        input_select.{%- if trigger.from_state.state < trigger.to_state.state -%}select_next{%- else -%}select_previous{%- endif -%}
        entity_id: input_select.radio_station
1 Like

Looked at the logfiles and errors, so I changed it to this, and it works!! Thank you so much!

- alias: Change Radio Station from Rotaty Encoder
  trigger:
  - entity_id: sensor.rotary_value
    platform: state
  condition:
    - condition: state
      entity_id: sensor.wemos_encoder_switch
      state: '1'
  action:
    service_template: >
        {%- if trigger.from_state.state < trigger.to_state.state -%}
        input_select.select_next
        {%- else -%}
        input_select.select_previous
        {%- endif -%}
    entity_id: input_select.radio_station
1 Like

I have a similar issue. The sensitivity of the ikea dimmer is too high in my opinion.
The dimmer reports its orientation from 0 to 250.

Instead of an absolute value too dimm the light I’d like to use the change of the value to dimm the lights. This way it would be easy to change the sensibility (devide the value).

With code similar to the one above this would be possible.
The issue here will be the minimum and maximum area.

So when the dimmer value is 0 and I want to dimm the light down, the from_state and the is_state will both be zero. So nothing happens. The same would happen with 255 (turning up).

Is there another way to achieve this?

Also I’d like to know if or how it is possible to use is_state and from_state in a binary_sensor. Or is is only possible to use this in an automation trigger.from_state.state
How cann I adress the from_state in a template sensor, where I don’t have a trigger.

1 Like