Esphome with Nextion display and slider to dim

Hello, trying the following…

On a nextion display I have created a slider (h0). I put on the touch release event:
prints h0.val,0

This gives me the output like below on Esphome UART:
For 50% (hex):

65 00 04 00 FF FF FF 
32 00 00 00 

For 0% (hex):

65 00 04 00 FF FF FF 
00 00 00 00 

For 100% (hex):

65 00 04 00 FF FF FF 
64 00 00 00 

I read:
https://esphome.io/custom/uart.html

But I don’t get how to parse/do/import these value so in home assistant an entity can be “filled” with the uart value… Any help greatly appreciated.

What I want to achieve: Be able to use a slider to dim a light (control) in home assistant.

Thanks!

I’m waiting for this feature too, but at the moment it is not supported (to my knowledge).
This requires ‘sensor’ support, which is not available (yet, just ‘binary sensor’).
See here: Nextion Slider is unusable #412
As a work around I used 10 binary sensors (10 steps of the slider f.i.), if 10 steps are OK.

Can you share the nextion and esphome code for that?

So, can you please share your config for this? I would like to see it “working”. Thank you!

OK, there are two parts where you have to code, the Nextion side (for this you use the Nextion Editor) and the ESPHome side (where you configure using yaml).
Let’s start with the Nextion side:
In the example below I use one slider component which is called hShort.
Configure the slider with minval=0 and maxval=9.
When you select this component in the Nextion Editor you should see the ‘Event’ tab (at the bottom).
In my case I use the ‘Touch Release Event’ to send the value of the slider to ESPHome:

if(hShort.val==0)
{
  printh 65 00 17 01 FF FF FF
  delay=500
  printh 65 00 17 00 FF FF FF
}
if(hShort.val==1)
{
  printh 65 00 18 01 FF FF FF
  delay=500
  printh 65 00 18 00 FF FF FF
}
if(hShort.val==2)
{
  printh 65 00 19 01 FF FF FF
  delay=500
  printh 65 00 19 00 FF FF FF
}
if(hShort.val==3)
{
  printh 65 00 1A 01 FF FF FF
  delay=500
  printh 65 00 1A 00 FF FF FF
}
if(hShort.val==4)
{
  printh 65 00 1B 01 FF FF FF
  delay=500
  printh 65 00 1B 00 FF FF FF
}
if(hShort.val==5)
{
  printh 65 00 1C 01 FF FF FF
  delay=500
  printh 65 00 1C 00 FF FF FF
}
if(hShort.val==6)
{
  printh 65 00 1D 01 FF FF FF
  delay=500
  printh 65 00 1D 00 FF FF FF
}
if(hShort.val==7)
{
  printh 65 00 1E 01 FF FF FF
  delay=500
  printh 65 00 1E 00 FF FF FF
}
if(hShort.val==8)
{
  printh 65 00 1F 01 FF FF FF
  delay=500
  printh 65 00 1F 00 FF FF FF
}
if(hShort.val==9)
{
  printh 65 00 20 01 FF FF FF
  delay=500
  printh 65 00 20 00 FF FF FF
}

The first byte (all in HEX) (65) stands for ‘Touch Event’, the second (00) is the page ID, the next is the component ID (here from 17 to 20) and then comes the event type (01-press, 00-release). The end of the message is always FF FF FF.
So, actually I use here the Release Event to send a Press event to ESPHome to fire the according Binary Sensor (see yaml below).

On the ESPHome side the corresponding part looks like this:

binary_sensor:

### simulated Slider ###
  - platform: nextion
    page_id: 0
    component_id: 23 # Slider hShort -> simulated press m0
    id: hShort0
    on_press:
      then:
        - logger.log: "Slider hShort val 0"
        - cover.control:
            id: beschattung_kurz
            position: 0%
  - platform: nextion
    page_id: 0
    component_id: 24 # Slider hShort -> simulated press m1
    id: hShort1
    on_press:
      then:
        - logger.log: "Slider hShort val 1"
        - cover.control:
            id: beschattung_kurz
            position: 10%
  - platform: nextion
    page_id: 0
    component_id: 25 # Slider hShort -> simulated press m2
    id: hShort2
    on_press:
      then:
        - logger.log: "Slider hShort val 2"
        - cover.control:
            id: beschattung_kurz
            position: 20%
  - platform: nextion
    page_id: 0
    component_id: 26 # Slider hShort -> simulated press m3
    id: hShort3
    on_press:
      then:
        - logger.log: "Slider hShort val 3"
        - cover.control:
            id: beschattung_kurz
            position: 30%
  - platform: nextion
    page_id: 0
    component_id: 27 # Slider hShort -> simulated press m4
    id: hShort4
    on_press:
      then:
        - logger.log: "Slider hShort val 4"
        - cover.control:
            id: beschattung_kurz
            position: 40%
  - platform: nextion
    page_id: 0
    component_id: 28 # Slider hShort -> simulated press m5
    id: hShort5
    on_press:
      then:
        - logger.log: "Slider hShort val 5"
        - cover.control:
            id: beschattung_kurz
            position: 50%
  - platform: nextion
    page_id: 0
    component_id: 29 # Slider hShort -> simulated press m6
    id: hShort6
    on_press:
      then:
        - logger.log: "Slider hShort val 6"
        - cover.control:
            id: beschattung_kurz
            position: 60%
  - platform: nextion
    page_id: 0
    component_id: 30 # Slider hShort -> simulated press m7
    id: hShort7
    on_press:
      then:
        - logger.log: "Slider hShort val 7"
        - cover.control:
            id: beschattung_kurz
            position: 70%
  - platform: nextion
    page_id: 0
    component_id: 31 # Slider hShort -> simulated press m8
    id: hShort8
    on_press:
      then:
        - logger.log: "Slider hShort val 8"
        - cover.control:
            id: beschattung_kurz
            position: 80%
  - platform: nextion
    page_id: 0
    component_id: 32 # Slider hShort -> simulated press m9
    id: hShort9
    on_press:
      then:
        - logger.log: "Slider hShort val 9"
        - cover.control:
            id: beschattung_kurz
            position: 90%

As you can see, I use this to control a Roller Shutter. 10 steps for the slider are good enough for me for this purpose. Also the ‘Release Event’ is appropriate here.
If you want to control light (dim) you may want to play with the ‘Touch Move’ event on the Nextion side.
Let me know if this works, I didn’t try.
Also make sure that the component IDs which you use in the ‘printh’ message on the Nextion side matching those ones on the ESPHome side (Nextion: Hex, ESPHome: Decimal , f.i. 17 --> 23) and that you do not use them elswhere.

Hope this helps a bit.

1 Like

Good morning, I’m testing the next beta. And I have to say I’m delighted. Now with a single sensor, we can use the sliders. Update the TFT via wifi, wonderful. I’m super happy. This update takes the Nextion screen to another dimension. Best regards.