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)
1 Like

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.

3 Likes

Dear all,
I think I landed on the right topic… however I’m struggling to make it work and really find the right way of syncing the slider helper in HA with the brightness value in ESPHome Nextion.
Can anyone share the HMI nextion file as well as the yaml… I think I’m missing something that I’m unable to understand from a piece of code.

Thanks a lot, in advance!

You can take a look at my NSPanel code here: https://github.com/ilyavolodin/NSPanel it has two sliders for controlling lights brightness and temperature that sync both ways.

1 Like

Hello!

I tried to use the slider, but don’t understand why it doesn’t works. Can anybody help?

I use the HMI-File from @ivolodin . In the log on ESP-Home i see the value changed when i slide on the screen. But i can’t implement this value in HA (first i will use a number helper for trying)

Here is my YAML:

binary_sensor:

  • platform: nextion
    name: $device_name Volume Slider
    page_id: 4
    component_id: 12
    internal: true
    on_release:
    then:
    - lambda: !lambda |-
    id(volume_value).update();

sensor:

  • platform: nextion
    id: volume_value
    component_name: Music.Volume
    on_value:
    then:
    - homeassistant.service:
    service: input_number.set_value
    data:
    entity_id: input_number.volume

Thanks!

Pretty sure that’s because you are not passing in valid value into the service. Try this instead

sensor:
  - platform: nextion
    id: volume_value
    component_name: Music.Volume
    on_value:
      then:
      - homeassistant.service:
        service: input_number.set_value
        data:
          value: !lambda 'return (int)x;'
        entity_id: input_number.volume

Thank you very much! This works perfect!

Thanks for your good post, you are a very modest man, when I looked at the github page as suggested, you also have a color wheel in your code, I have gone and pondered how to make it, I will try to understand your code, and then adapt it to my needs, again thank you for sharing with the rest of us.

How would I make this work with a fan?

I have a fan component on another ESP module that I’d like to control with a slider on a Sonoff NSP.
The fan is in Homeassistant and has 3 speeds so the slider would have 4 positions - 0, 33, 66, 100

Thanks.

Hi, question… what event should I type in the event bar in the nextion editor? I’m strugging to get a value to HA…

Hello, I have this dimer created in esphome:
output:

  • platform: ledc
    pin: GPIO19
    id: gpio_19
    frequency: “1000Hz”

light:

  • platform: monochromatic
    output: gpio_19
    name: “Dimmer Test”
    id: dimmer_teste

Using your logic, how synchronization would look like.
Thanks