Last value instead of unknown

pls help. what am I missing?
unknown
there is a sensor “sensor.light_sensor_new_bh1750_illuminance” sometimes when the network is lost (Ping) it returns the value unknown

I want it to show the latest value instead of unknown

  - trigger:
      - platform: state
        entity_id: sensor.light_sensor_new_bh1750_illuminance
        not_to:
          - unknown
          - unavailable
    sensor:
    - name: Outside_illuminance
      device_class: illuminance
      unit_of_measurement: 'lx'
      state: >
       {% set x = states('sensor.light_sensor_new_bh1750_illuminance') %}
       {{ x if has_value(x) else unknown | default(0, true) }}

i didn’t test this, but this structure should be what you use. your original spacing has a bunch of bugs in it. i thikn i caught them all.

- trigger:
    - platform: state
      entity_id: sensor.light_sensor_new_bh1750_illuminance
  sensor:
    - name: Outside_illuminance
      device_class: illuminance
      unit_of_measurement: 'lx'
      state: >
        {{ states('sensor.light_sensor_new_bh1750_illuminance')
           if states('sensor.light_sensor_new_bh1750_illuminance') not in ['unknown', 'unavailable'] 
           else 
           states('sensor.Outside_illuminance') }}
      
2 Likes

Thanks for your answer, I checked but it didn’t help :frowning:

Had a typo that @LiQuid_cOOled caught

had an extra quote. I fixed it above

1 Like

you are absolutely correct. Thank you very much, I checked and it works + I added “unique_id”

  - trigger:
    - platform: state
      entity_id: sensor.light_sensor_new_bh1750_illuminance
    sensor:
    - name: Outside_illuminance
      device_class: illuminance
      unit_of_measurement: 'lx'
      unique_id: 8794562456456478545458
      state: >
        {{ states('sensor.light_sensor_new_bh1750_illuminance')
           if states('sensor.light_sensor_new_bh1750_illuminance') not in ['unknown', 'unavailable'] 
           else 
           states('sensor.Outside_illuminance') }}

Now the problem is that this home assistant sensor is not visible as a device so that I can add it for automation

  - trigger:
    - platform: state
      entity_id: sensor.light_sensor_new_bh1750_illuminance
    sensor:
    - name: Outside_illuminance
      device_class: illuminance
      unit_of_measurement: 'lx'
      unique_id: 8794562456456478545458
      state: >
        {{ states('sensor.light_sensor_new_bh1750_illuminance')
           if states('sensor.light_sensor_new_bh1750_illuminance') not in ['unknown', 'unavailable'] 
           else 
           states('sensor.Outside_illuminance') }}

Can you specify which sensor?

you shouldn’t use capital letters states('sensor.Outside_illuminance') }}

hi, illuminance sensor bh1750

Not sure how it’s connected to your system, ZHA, Z2M, but is showing in the device integration?

sensor integrated ESPHome

esphome:
  name: light-sensor-new
  friendly_name: light_sensor_new

esp8266:
  board: esp01_1m

# Enable logging
logger:

# Enable Home Assistant API
api:
  encryption:
    key: "gBOoL4+U9Afa/p5ocy9jt/koGfnB4VSB3t6aPiuNTzM="

ota:
  - platform: esphome
    password: "549342293f5aa2350e60ab21dc8eeb04"

wifi:
  ssid: XXXXX
  password: XXXXXXX

  # Optional manual IP
 # manual_ip:
  #  static_ip: 192.168.88.69
  #  gateway: 192.168.88.1
  #  subnet: 255.255.255.0

  # Enable fallback hotspot (captive portal) in case wifi connection fails
  ap:
    ssid: "Light-Sensor Fallback Hotspot"
    password: "95VlzSrwyfSi"
i2c:
  sda: GPIO4
  scl: GPIO5
  scan: true
  id: bus_a

sensor:
  - platform: bh1750
    name: "BH1750 Illuminance"
    address: 0x23
    update_interval: 60s

The sensor itself “sensor.light_sensor_new_bh1750_illuminance” illuminance" is visible as a device, but the second "sensor.Outside_illuminance"one is not visible, which, based on the first one, removes the value unknown’, ‘unavailable’

Is this a sensor you created via a template or helper?

using template

Is the template sensor showing anything in Developer Tools?

Your template is a bit of a loop. If the bh1750_illuminance sensor drops it want to refer to a sensor created from the sensor that is missing.

yes


And what you are saying is the entity doesn’t appear here?

:slight_smile: shouldn’t it have appeared as a device? I didn’t even suspect that it should be looked for in entity

It’s not classified as a device, but will come up as an entity. Select Entity then State for the When.

1 Like

yes now I understand. Thanks a lot. need to learn something