Nextion Switch Component¶
nextion switch platform supports the many switched components in the Nextion as well as integer variables. It can be a component or variable in the Nextion display.
It is best to set the components vscope to global in the Nextion Editor. This way the component will be available if the page is shown or not.
See Nextion TFT LCD Display for setting up the display
# Example configuration entry display: - platform: nextion id: nextion1 # ... switch: - platform: nextion id: r0_switch name: "Radio 0 Switch" component_name: r0 # pageX.r0 for a global update_interval: 4s - platform: nextion id: darkmode name: "Is Darkmode Set" variable_name: darkmode
name (Required, string): The name of the sensor.
nextion_id (Optional, ID): The ID of the Nextion display.
component_name (Optional, string): The name of the Nextion component.
variable_name (Optional, string): The name of the Nextion variable. Any value over
0is considered to be on
background_color (Optional, Color): The background color
background_pressed_color (Optional, Color): The background color when pressed
foreground_color (Optional, Color): The foreground color
foreground_pressed_color (Optional, Color): The foreground color when pressed
visible (Optional, boolean): Visible or not
All other options from Switch.
Only one component_name or variable_name can be set
See How things Update for additional information
The Nextion does not retain data on Nextion page changes. Additionally, if a page is changed and the component_name does not exist on that page then
nothing will be updated. To get around this, the Nextion components can be changed to have a vscope of
global. If this is set then the component_name
should be prefixed with the page name (page0/page1 or whatever you have changed it to).
set_state(bool value, bool publish, bool send_to_nextion): Set the state to value. Publish the new state to HASS. Send_to_Nextion is to publish the state to the Nextion.
update(): Poll from the Nextion
set_background_color(Color color): Sets the background color to Color
set_background_pressed_color(Color color): Sets the background color to Color
set_foreground_color(Color color): Sets the background color to Color
set_foreground_pressed_color(Color color): Sets the background color to Color
set_visible(bool visible): Sets visible or not. If set to false, no updates will be sent to the component
How things Update¶
A Nextion component with an integer value (.val) or Nextion variable will be automatically polled if update_interval is set. To have the Nextion send the data you can use the Nextion Custom Switch Protocol for this. Add the Nextion Custom Switch Protocol to the component or function you want to trigger the send. Typically this is in Touch Press Event but some components, like a slider, should have it set in the Touch Release Event to capture all the changes. Since this is a custom protocol it can be sent from anywhere (timers/functions/components) in the Nextion.
There is no need to check the Send Component ID for the Touch Press Event or Touch Release Event since this will be sending the real value to esphome.
- Using the above yaml example:
“Radio 0 switch” will poll the Nextion for the
r0.valvalue and set the state accordingly.
“Is Darkmode Set” will NOT poll the Nextion. Either the Nextion will need to use the Nextion Custom Switch Protocol or use a lambda:
No updates will be sent to the Nextion if it is sleeping. Once it wakes, the components will be updated. If a component is invisible,
visible(false), then it won’t update until it is set to be visible.
Nextion Custom Switch Protocol¶
All lines are required
printh 90 prints "r0",0 printh 00 prints r0.val,0 printh FF FF FF
printh 90Tells the library this is a switch bool/integer data
prints "r0",0Sends the name that matches component_name or variable_name
printh 00Sends a NULL
prints r0.val,0The actual value to send. For a variable use the Nextion variable name
printh FF FF FFNextion command ack