Like quite a few other people, I’ve been working on integrating my Sonoff NSPanels into Home Assistant. But the approach I’m using is a bit different from what I’ve seen others doing - it uses a Python script to provide a service so that you can send instructions to Home Assistant directly in the Nextion Editor.
This allows you to program a Nextion touch screen device (NSPanels in particular) to interact with Home Assistant (HA) without having to do any coding in ESPHome YAML or Home Assistant automations.
The diagram below summarises how it works, and I’ve listed the instruction set that nextion_handler makes available in the Nextion Editor below. There are only 3 places in your HMI files that you need to add customized code (highlighted in light blue in the diagram) - the rest can use standardized template code.
I have shared more details on GitHub together with some example files that can be used as templates to get going.

(Updated: version 0.7beta - 2022-07-24)
SET COMMANDS (fetch HA data to update the Nextion)
(Nx = Nextion variable name; E is Home Assistant entity_id).
-
sett Nx len E(assignlenchars of state ofE, as string/text, toNx). -
setn Nx scale E (d)(assignNxthe integer value ofscale* state ofE) -
setb Nx E(assignNx0 or 1 based whether the state ofEis in FALSE_STATES)). -
setb Nx E cp x(assignNx0 or 1 based on comparing (cp) the state ofEtox). -
setlt Nx_state Nx_tp Nx_brt Nx_ct Nx_rgb565 E(assignNxvariables the state, ‘type’ (supported modes), brightness, color temperature and color of lightE). -
setmp Nx_vars E(assign state and attributes of media_playerEto the list ofNx_vars) -
setcl Nx_vars E(assign state and attributes of climateEto the list ofNx_vars) -
setntf Nx_count (Nx_title) (Nx_msg) (n) (chars_title) (chars_msg)(assign 3Nxvariables the Count, Title and Message of thenth Persistent Notification). -
setdt Nx(assignNxcurrent data-time as “dd/mm HHhMM”). -
setsys Nx_vars(assign system data to the list ofNx_vars).
ACTION COMMAND LIST (called by Nextion events to perform actions in HA)
-
tgl E(toggleE). -
ton E(turn onE). -
tof E(turn offE). -
inps E string(set value of input_selectEtostring). -
inpb E b(turn input_binaryEonif b!=0 otherwise turnoff). -
inpn E x(set value of input_numberEtox). -
scn E(turn on sceneE). -
scpt E(call scriptE). -
say E string(Play TTS of messagestringto media playerE). -
ntf string(Display a persistent notification with messagestringto HA). -
ntfx n(Dismiss thenth Persistent Notification in HA). -
sub Nx(click Nx,1the Nextion (hidden) hotspotNxto execute a ‘subroutine’). -
cl svc E args(call thesvcservice for climateEwith the listedargs). -
cv svc E args(call thesvcservice for coverEwith the listedargs). -
lt svc E args(call thesvcservice for lightEwith the listedargs). -
mp svc E args(call thesvcservice for media_playerEwith the listedargs).
(For full details of what each Set and Act command does, see the doc strings for functions with the same names in the Python script, including the adjust() helper function that helps make delta/absolute/percentage changes to attributes.)


v0.6 now uploaded to repository, including
Widget UI option. )

) once you have found the section you need.