Nextion Binary Sensor Component
The nextion binary sensor platform supports the many switched components in the Nextion as well as integer variables (>0 == true). 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 for setting up the display
# Example configuration entrydisplay: - platform: nextion id: nextion1 # ...
binary_sensor: - platform: nextion page_id: 0 component_id: 8 component_name: page0.mode # Only needed to support changing colors id: mode - platform: nextion id: r0_binary_sensor name: "Radio 0 Binary Sensor" component_name: page0.r0 # r0 is a global component in the Nextion Editor on page 0 update_interval: 4s - platform: nextion id: darkmode name: "Is Darkmode Set" variable_name: darkmodeConfiguration variables
Section titled “Configuration variables”- 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 - page_id (Optional, string): The ID of the page the component is on. Use
0for the default page. - component_id (Optional, string): The ID (the number, not name!) of the component to track.
- update_interval (Optional, Time): The duration to update the sensor. If using a Nextion Custom Binary Sensor Protocol this should not be used
- background_color (Optional, Color): The background color
- foreground_color (Optional, Color): The foreground color
- visible (Optional, boolean): Visible or not
- All other options from Binary Sensor.
Touch Sensor: The Nextion will send a page_id and component_id when the Send Component ID check box is selected for the component. To enable this native event page_id and component_id are required. No Nextion Custom Binary Sensor Protocol is required. If page_id and component_id are set then the component will only react to touch events from the Nextion. Setting component_name will allow setting options like foreground color.
NOTE
background_color(s), foreground_color(s) and visible do not retain their state on page change. Binary Sensor Settings.
A Nextion Sensor with a custom protocol sending the current page can be used to execute the API call Update Components By Prefix to update all the components for that page
Example:
- platform: nextion id: current_page nextion_id: nextion1 name: current_page variable_name: current_page on_value: lambda: |- id(nextion1).update_components_by_page_prefix("page"+x+".");See How things Update for additional information
Globals
Section titled “Globals”The Nextion does not retain data on Nextion page changes. Additionally if a page is changed and the nextion_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 nextion_component_name
should be prefixed with the page name (page0/page1).
Example: nextion_component_name: page0.r0
binary_sensor.nextion.publish Action
Section titled “binary_sensor.nextion.publish Action”You can also publish a state to a Nextion binary sensor from elsewhere in your YAML file
with the binary_sensor.nextion.publish action.
# Example configuration entrybinary_sensor: - platform: nextion id: nextion_bsensor ...# in some triggeron_...: - binary_sensor.nextion.publish: id: nextion_bsensor state: true # These are optional. Defaults to true. publish_state: true send_to_nextion: true # Templated - binary_sensor.nextion.publish: id: nextion_bsensor state: !lambda 'return true;' # These are optional. Defaults to true. publish_state: true send_to_nextion: trueConfiguration variables
Section titled “Configuration variables”-
id (Required, ID): The ID of the Nextion switch.
-
state (Required, string, templatable): The boolean state to publish.
-
publish_state (Optional, bool, templatable): Publish new state to Home Assistant. Default is true.
-
send_to_nextion (Optional, bool, templatable): Publish new state to Nextion display which will update component. Default is true.
NOTE
This action can also be written in lambdas. See Lambda Calls
Lambda Calls
Section titled “Lambda Calls”From lambdas, you can call several methods to access some more advanced functions (see the full API Reference: nextion_binarysensor.h for more info).
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 Colorset_background_pressed_color(Color color): Sets the background color to Colorset_foreground_color(Color color): Sets the background color to Colorset_foreground_pressed_color(Color color): Sets the background color to Colorset_visible(bool visible): Sets visible or not. If set to false, no updates will be sent to the component
How things Update
Section titled “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 Binary Sensor Protocol for this. Add the Nextion Custom Binary Sensor 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.
NOTE
There is no need to check the Send Component ID for the Touch Press Event or Touch Release Event for an integer value component since this will be sending the real value to esphome, but make sure you have both checked for a touch sensor.
Using the above yaml example:
-
“mode” is a touch sensor and will trigger when a user presess the component with ID
8in page0 -
“Radio 0 Binary Sensor” 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 Binary Sensor Protocol or use a lambda:
NOTE
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 Binary Sensor Protocol
Section titled “Nextion Custom Binary Sensor Protocol”All lines are required
printh 93prints "r0",0printh 00prints r0.val,0printh FF FF FFExplanation
Section titled “Explanation”printh 93Tells the library this is a binary sensor bool/integer dataprints "r0",0Sends the name that matches nextion_component_name or nextion_variable_nameprinth 00Sends a NULLprints r0.val,0The actual value to send. For a variable use the Nextion variable namer0with out.valprinth FF FF FFNextion command ack