Checking if an entity went offline today or the last x hours or so

My gas sensor goes offline from time to time and becomes unavailable, and because it does a check i guess if it’s under a certain amount of LEL, it will trigger an automation saying ‘Gas leak resolved’. Which is slightly startling.

So i thought of putting in a condition like so:

conditions:
  - condition: state
    entity_id: binary_sensor.kitchen_gas_sensor_1
    state: unavailable

But it doesn’t happen necessarily when the automation triggers, it can be 12 minutes after, so i need to check if it went unavailable in the last hour or so, or maybe even the entire day.

Any way of doing this ?

Well for a start that condition will only pass if the state is unavailable. You want it to not be unavailable:

condition:
  - condition: not
    conditions:
      - condition: state
        entity_id: binary_sensor.kitchen_gas_sensor_1
        state: unavailable

You may also have to fix your trigger. Post the whole automation.

Hmm not sure why it didn’t post that, i did use the ‘not’ condition. Anyway, here’s my whole code:

- id: '1642837463085'
  alias: Gas leak Announce Resolved
  description: Announce gas leak is resolved if LEL is below X
  trigger:
  - type: value
    platform: device
    device_id: 1f32b26df70dd90cded60063504df5b6
    entity_id: sensor.kitchen_gas_sensor_1
    domain: sensor
    below: 1
  condition:
  - condition: not
    conditions:
    - condition: state
      entity_id: binary_sensor.kitchen_gas_sensor_1
      state: unavailable
  action:
  - service: notify.alexa_media_everywhere
    data:
      data:
        type: announce
        method: all
      message: Gas Leak Resolved
  mode: single

Thanks for looking.

Try this:

- id: '1642837463085'
  alias: Gas leak Announce Resolved
  description: Announce gas leak is resolved if LEL is below X
  trigger:
  - platform: numeric_state
    entity_id: sensor.kitchen_gas_sensor_1
    below: 1
  condition:
  - condition: template
    value_template: "{{ trigger.from_state.state != 'unavailable' }}"
  action:
  - service: notify.alexa_media_everywhere
    data:
      data:
        type: announce
        method: all
      message: Gas Leak Resolved
  mode: single
1 Like

Cheers Tom, that looks like it should do the job. Many many thanks…