Syncing slider between HA and Nextion display

I have a Sonoff NSPanel and I’m having absolutely no luck getting a slider to sync both ways.
I have managed to get it to sync from Home Assistant to Nextion, but I can’t get it to go the other way

number:
- platform: template
    name: $device_name Slider1
    id: slider1
    #entity_category: config
    min_value: 5
    max_value: 255
    step: 1
    initial_value: 255
    set_action:
      then:
        - lambda: 'id(disp1).set_component_value("Slider1",x);'  

I had a suggestions to use something similar to this

sensor:
  - platform: nextion
    name: Slider1Test
    component_name: page0.Slider1
    id: Slider1Test
    #update_interval: 1s
    on_value:
      then:
      - number.set:
          id: slider1
          value: !lambda return x;

This did nothing other than cause the display so go a bit crazy if I enabled update_interval: 1s

Any help really appreciated, I’ve been stuck on this for days with no luck

I also had problems getting the slider sync right in two directions when using a Nextion display. The solution which worked for me:

  1. Add an extra (hidden) helper button on the HMI panel and also define a binary sensor for this button in your YAML file.
  2. In the Touch Release Event of the slider in the HMI panel generate a ‘fake’ press event on the helper button with a printh statement like printh 65 00 03 01 FF FF FF where the 00 is the pagenum, 03 the id off the helper button. Both these numbers must be the hex representation of the decimal ID values you see in the Nextion editor for the page id and button id. (You can see the actual hex values if you press this helper button in the debug mode of the editor).
  3. In your YAML file add an on_press event for the helper button where you do a manual update of your slider sensor to get the current nextion slider value, the YAML code:
binary_sensor:
  - platform: nextion
    page_id: XX <--- your Nextion page num
    component_id: YY <--- your Nextion helper button ID
    id: slider_helper
    on_press:
      then:
      - lambda: !lambda |-
          id(slider_helper).publish_state(false); // Reset state helper
          id(my_slider).update();   // Get slider value from Nextion panel

Hopefully this works for you too

Jos

Thanks for the respose

I’ve added the following to my yaml file:

binary_sensor:
  - platform: nextion
    page_id: 0
    component_id: 18
    id: slider_helper
    name: $device_name Slider Helper
    on_press:
      then:
      - lambda: !lambda |-
          id(slider_helper).publish_state(false);
          id(slider1).update();

And added the helper button and code to my slider
Screenshot 2022-01-14 163655

I can see that an event is being sent to ESPHome now for the helper button when I move the slider, but the slider_helper button turns to an ON state and never changes back, and the slider in Home Assistant doesn’t update

Any thoughts?

Yes the button on the panel looks like it stays “ON”, however that doesn’t bother me, in my ‘production’ version this helper button is made invisible :slight_smile:.

There is no direct link between HA and the panel, so now you have the correct value off the panel slider available in your ESP, you have to update the HA value via some lines of code in your ESPHome slider component. In my case it is a slider for the brightness so here is the code I use to update the slider in HA (and of course also the lamp brightness itself :slight_smile:).

sensor:
  - platform: nextion       
    id: p3_studylamp_slider
    component_name: study.slider
    on_value:
      then:
        - homeassistant.service:
            service: light.turn_on
            data:
              entity_id: light.study_lamp_rgbww_light
              brightness: !lambda return int(x); // <--- the slider value as integer (in HA brightness is defined as an integer between 0 and 255)

I know this issue has been resolved, but I was just following instructions from @jsuanet and realized that you don’t need that extra hidden button at all. Slider in Nextion can report press and release events to ESPHome, if you check appropriate checkboxes in Nextion Editor. So you can just update sensor value on release of the slider itself.

sensor:
  - platform: nextion
    id: brightness_value
    component_name: brightnessVal
    on_value:
      then:
        - logger.log:
            format: "Updated brightness value to %i"
            args: ['(int)id(brightness_value).state']
binary_sensor:
  - platform: nextion
    name: $device_name Brightness Slider
    page_id: 1
    component_id: 6
    on_release:
      then:
        - lambda: !lambda |-
            id(brightness_value).update();

The code above works great and reports correct value every time you let go of a slider in the UI. Hope it helps somebody.