DEV Community

JF Gagnon
JF Gagnon

Posted on

NSPanel ESPHome: Unlimited actions

My cheap and great NSPanel had an issue. Flashing with ESPHome allowed me to make my own UI and program everything I want. So I would make a button on Nextion Editor, and tell ESPHome what to do when pressed. That required flashing both ESPHome and Nextion.

However I ran out of space. It would compile and tell me I have plenty of flash/ram but the device would be in a bootloop, requiring me to take out the device from my wall. I raised an issue but I couldn’t find a way to fix it myself. So I came up with this workaround…

GPSig: General Purpose Signal

The idea is simple: All buttons will change the variable “gpsig”. On RAW change, this would call the HA script GPSig with the action ID and all arguments/variables stored also on ESPHome. Best thing is we dont even need to edit ESPHome to add new action: flash Nextion and add new action id in Home Assistant.

Nextion: Writing to ESPHome variable

Truthfully, dont ask question. Simply copy and paste these two
Text:

printh 92
prints "text_sensor_name",0
printh 00
prints "new value!!",0
printh 00
printh FF FF FF
Enter fullscreen mode Exit fullscreen mode

Number:

printh 91
prints "number_sensor_name",0
printh 00
prints 123,0
printh FF FF FF
Enter fullscreen mode Exit fullscreen mode

A full example of Nextion code for PIN Code input would be:

printh 92
prints "argstr1",0
printh 00
prints code.txt,0
printh 00
printh FF FF FF
printh 91
prints "gpsig",0
printh 00
prints 1,0
printh FF FF FF
code.txt=""
Enter fullscreen mode Exit fullscreen mode

ESPHome: Send up Action ID and variables

Note: If you use on_value instead of on_raw_value, pressing on the button twice will only do the action once, because changing the variable from 1 to 1 is not a “change”. on_raw_value will however trigger.

Note 2: removing c_str() will send a string with a null character in the end, somehow. So doing == in Home Assistant will fail even though the UI will show you two identical strings.

text_sensor:
  - platform: nextion
    nextion_id: disp1
    name: argstr1
    id: argstr1
    component_name: argstr1
    update_interval: 4s
sensor:
  - platform: nextion
    internal: true
    id: argint1
    variable_name: argint1
  - platform: nextion
    internal: true
    id: argint2
    variable_name: argint2
  - platform: nextion
    internal: true
    id: gpsig
    variable_name: gpsig
    on_raw_value:
    - homeassistant.service:
       service: script.gpsig
       data:
          actionid: !lambda return (int)(x);
          argstr1: !lambda return id(argstr1).state.c_str();
          argint1: !lambda return (int)(id(argint1).state);
          argint2: !lambda return (int)(id(argint2).state);
Enter fullscreen mode Exit fullscreen mode

Home Assistant: Triggering action

alias: gpsig
sequence:
  - choose:
      - conditions:
          - condition: template
            value_template: "{{ actionid == \"1\" }}"
        sequence:
          - if:
              - condition: template
                value_template: "{{ argstr1 == \"1234\" }}"
            then:
              - service: script.alarm_lock_disarm
                data: {}
            else:
              - service: notify.mobile_app_nexus4
                data:
                  message: Someone tried to unlock the alarm with {{ argstr1 }}.
      - conditions:
          - condition: template
            value_template: "{{ actionid == \"2\" }}"
        sequence:
          - service: script.alarm_lock
            data: {}
    default: []
mode: single
icon: mdi:robot-industrial
fields:
  actionid:
    name: actionid
    description: actionid
    selector:
      number:
        min: 0
        max: 100000
        step: 1
  argstr1:
    name: argstr1
    description: argstr1
    selector:
      text: null
  argint1:
    name: argint1
    description: argint1
    selector:
      number:
        min: 0
        max: 100000
        step: 1
  argint2:
    name: argint2
    description: argint2
    selector:
      number:
        min: 0
        max: 100000
        step: 1
Enter fullscreen mode Exit fullscreen mode

Pros and cons

- Unlimited actions!
- Complex at first.
- New buttons requires TFT upload and gpsig script modifications. No ESPHome flashing required!
- One way only: Nextion->HA
Enter fullscreen mode Exit fullscreen mode

Top comments (0)