Read switch state on wakeup

I have a Sonoff NSPanel that I’ve added an icon to for the state of the heating system. Its using this yaml:

  - platform: homeassistant
    id: heating_symbol
    entity_id: switch.sonoff_4ch_relay_1
    on_value:
      then:
        - wait_until:
            switch.is_on: nextion_init
        - lambda: |-
            int symbol=36; // 36 is heating off.
            if(id(heating_symbol).state == "on"){
              symbol=37;
            }
            id(disp1).send_command_printf("Home.heating_symbol.pic=%i", symbol);

It works great until the screen goes off, when you wake it up later it displays the off icon and will only change to the on icon if you turn the heating relay off and on again.

How do I read the state of the relay when the screen turns on?

force_update maybe this?

Do you have nextion_init defined in the code?

nextion_init is defined here:

# Basically a global variable to keep track on whether the Nextion display is ready or not. Delays initial info from HA to the display.
- platform: template
    name: $device_name Nextion inited
    id: nextion_init
    entity_category: config
    restore_state: false
    assumed_state: off
    optimistic: true

@lordzid Where would I set force_update I get an error if I tru to use it on the relays binary sensor or the displays output

I have a weather display on the screen that stays at its correct state between screensaver events. Its pretty much the same just based off weather.home instead

I have no clue why you have that issue then.
I don’t have it.

You could add a debug print inside the on_value to see what the nextion_init is and what heating_symbol is.

I doubt this will work because I always have issues with outputing states in ESP-Home, but some variant of this should perhaps give you some clues what is going on.

  - platform: homeassistant
    id: heating_symbol
    entity_id: switch.sonoff_4ch_relay_1
    on_value:
      then:
        - lambda: |-
            ESP_LOGD("nextion_init: ", "%s", id(nextion_init) );
        - wait_until:
            switch.is_on: nextion_init
        - lambda: |-
            ESP_LOGD("nextion_init: ", "%s", id(nextion_init) );
            ESP_LOGD("heating_symbol: ", "%s", id(heating_symbol).c_str() );

            int symbol=36; // 36 is heating off.
            if(id(heating_symbol).state == "on"){
              symbol=37;
            }
            id(disp1).send_command_printf("Home.heating_symbol.pic=%i", symbol);
1 Like

I got to the bottom of this.

I had to rebuild the .tft file so that the symbols where global and not local. This means that the state is set for the screen as a whole and isn’t lost when the screen moves to another page.