Conflicting mqtt automations for input_number (slider)?

I am trying to make an input_number slider entity work with an mqtt roller shade. I have 2 automations, the first sets the input_number value when triggered by an mqtt (Set Shade1 slider), and the second sends an mqtt when the slider is moved in the gui (Shade1 slider moved). The mqtt format for the shade is simple. When it is sent a value between 0 and 100 (percentage of closed), it will first send out a json with its current position (position) and the new setting (set). After the shade has completed moving, it sends out another json with the current status (position and setting) and in this transmission, they are always the same (the new set position).

i.e. Assuming current position is 10.

when shade is sent payload: 20
shade returns “set”: 20, “position”: 10
after it finishes moving, shade returns “set”:20, “position”:20

Both automations will work individually (when the other is removed), but when both are present, the shade will move to the new position (as set by the slider or an external source), but then it will move back up to zero in single step increments.

input_number:
  shade1:
    name: Shade1
    mode: slider
    initial: 0
    min: 0
    max: 100
    step: 10
    icon: mdi:blinds

automation:
  - alias: "Set Shade1 slider"
    trigger:
      platform: mqtt
      topic: /raw/esp8266/6831410/out
    condition:
      - condition: template
        value_template: "{{ trigger.payload_json.set == trigger.payload_json.position }}"
    action:
      - service: input_number.set_value
        data_template:
          entity_id: input_number.shade1
          value: "{{ trigger.payload_json.position | int }}"

  - alias: "Shade1 slider moved"
    trigger:
      platform: state
      entity_id: input_number.shade1
    action:
      - service: mqtt.publish
        data_template:
          topic: /raw/esp8266/6831410/in
          retain: true
          payload: "{{ states('input_number.shade1') | int }}"

Thanks

I think you forgot to ask a question… :wink:

Well, the problem/question was why does the shade always return to zero after it has moved to the correct position?

And, I just figured it out. For anyone else who winds up here…

I had flashed my roller shade’s esp8266 with the binary file from this project:

Conphilpott/motor-on-roller-blind-ws

Turns out, the esp8266 firmware decrements the “set” value by one. So if you send it a payload of 10, it returns “set”: 9, “position”: x. And when complete, it returns returns “set”: 9, “position”: 9 (not 10!).

Sooo… the shade was always moving to one value less than the set value and this caused the automation to incrementally step it down to zero. By adding one to the payload in the second automation…

  - alias: "Shade1 slider moved"
    trigger:
      platform: state
      entity_id: input_number.shade1
    action:
      - service: mqtt.publish
        data_template:
          topic: /raw/esp8266/6831410/in
          retain: true
          payload: "{{ states('input_number.shade1') | int + 1 }}"

the problem is solved. Not sure why the esp8266 code does that, but I’m sure there is a good reason.

Cheers

1 Like