How to detect if a value is NULL

I’ve been struggling to properly detect if a text_sensor or sensor has the value of NULL.

I have a text_sensor with an if statement to either display page1 or page3 depending on the condition:

text_sensor:

  - platform: homeassistant
    id: sonos_media_channel
    entity_id: media_player.sonos_arc
    attribute: media_channel

  - platform: homeassistant
    entity_id: media_player.android_tv1
    id: android_tv
    on_value: 
      then:
        - if:
            condition:
              lambda: 'return id(android_tv).state != "off" || id(sonos_media_channel).state != NULL );'
            then:
              - logger.log: "TV is On"
              - display.page.show: page1
            else:
              - logger.log: "TV is Off"
              - display.page.show: page3

This check does not work. I also tried setting up a “normal” non text sensor, but that also results in not working code.

My workarround for now is using a Home Assistant template to determine that a value “is defined”. But it would be much nicer if it could be contained within the Esphome configuration of the device.

Can someone explain how I can reliably check if a text_sensor or a normal sensor has a value of NULL?

Never tried in esphome, but try:
lambda: ‘return id(android_tv).state != “off” || id(sonos_media_channel).state;’

Forgot to reply. When using binary sensor id(your_binary_sensor_name).state will result in either true or false. That’s the way I went…Well I refactored the whole lot and now items in the display are shown conditionally (but using the logic you suggested), in stead of having 2 complete pages with all the items I want to show, with 2 parenthesis added:

lambda: "return ((id(android_tv).state != 'off' ) || (id(sonos_media_channel).state));"

Within the parenthesis it will be ( (true) or (false) ) resulting in false or vice versa.

Good that it worked!