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
(assignlen
chars of state ofE
, as string/text, toNx
). -
setn Nx scale E (d)
(assignNx
the integer value ofscale
* state ofE
) -
setb Nx E
(assignNx
0 or 1 based whether the state ofE
is in FALSE_STATES)). -
setb Nx E cp x
(assignNx
0 or 1 based on comparing (cp
) the state ofE
tox
). -
setlt Nx_state Nx_tp Nx_brt Nx_ct Nx_rgb565 E
(assignNx
variables the state, ‘type’ (supported modes), brightness, color temperature and color of lightE
). -
setmp Nx_vars E
(assign state and attributes of media_playerE
to the list ofNx_vars
) -
setcl Nx_vars E
(assign state and attributes of climateE
to the list ofNx_vars
) -
setntf Nx_count (Nx_title) (Nx_msg) (n) (chars_title) (chars_msg)
(assign 3Nx
variables the Count, Title and Message of then
th Persistent Notification). -
setdt Nx
(assignNx
current 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_selectE
tostring
). -
inpb E b
(turn input_binaryE
on
if b!=0 otherwise turnoff
). -
inpn E x
(set value of input_numberE
tox
). -
scn E
(turn on sceneE
). -
scpt E
(call scriptE
). -
say E string
(Play TTS of messagestring
to media playerE
). -
ntf string
(Display a persistent notification with messagestring
to HA). -
ntfx n
(Dismiss then
th Persistent Notification in HA). -
sub Nx
(click Nx,1
the Nextion (hidden) hotspotNx
to execute a ‘subroutine’). -
cl svc E args
(call thesvc
service for climateE
with the listedargs
). -
cv svc E args
(call thesvc
service for coverE
with the listedargs
). -
lt svc E args
(call thesvc
service for lightE
with the listedargs
). -
mp svc E args
(call thesvc
service for media_playerE
with 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.)