(Yet another) ignore previous "unavailable" state

On second thought, I actually figured it out. In case this helps someone else…

My motion detector is part of an ESP32 node with multiple sensors attached, when one sensor goes offline, it’s actually the whole node that’s down, not just the one sensor (I’m sure this will be similar for many integrations, where the whole integration drops, not just the one sensor) So I added an extra condition to check not only how long the sensor I care about has been in the appropriate condition, but also that the node itself (which only reports online/offline status) hasn’t changed in an even longer time. e.g.:

{{  is_state('binary_sensor.office_door_motion', 'on') or (
  is_state('binary_sensor.office_door_motion', 'off') and (
  now() - states.binary_sensor.office_door_motion.last_changed).total_seconds() < 1.5 )  and (
  now() - states.binary_sensor.motion_sensors_node.last_changed.total_seconds() > 2)) }}

I have an automation with several “or” and it works normally, but I want to add an AND condition

new state is not 'unknown', 'unavailable','none'
trigger:
  - platform: event
    event_type: state_changed
    event_data: {}
condition:
  - condition: or
    conditions:
      - condition: template
        value_template: "{{ trigger.event.data.new_state.domain == 'light' }}"
      - condition: template
        value_template: >-
          {{ trigger.event.data.new_state.domain == 'binary_sensor' 

          and trigger.event.data.new_state.attributes.device_class | default('')
          == 'door'

          or trigger.event.data.new_state.attributes.device_class | default('')
          == 'lock'

          or trigger.event.data.new_state.attributes.device_class | default('')
          == 'window'

          or trigger.event.data.new_state.attributes.device_class | default('')
          == 'garage_door'  }}
      - condition: template
        value_template: "{{ trigger.event.data.new_state.domain == 'person' }}"
      - condition: template
        value_template: "{{ trigger.event.data.new_state.domain == 'input_boolean' }}"
      - condition: template
        value_template: >-
          {{ trigger.event.data.new_state.domain == 'sensor' and
          trigger.event.data.new_state.attributes.friendly_name == "Zonas
          Anuladas"  }}

But when I add:

  - condition: template
    value_template: "{{ trigger.to_state.state not in ['unknown', 'unavailable','none'] }}"

Like this:

trigger:
  - platform: event
    event_type: state_changed
    event_data: {}
condition:
  - condition: template
    value_template: "{{ trigger.to_state.state not in ['unknown', 'unavailable','none'] }}"
    enabled: true
  - condition: or
    conditions:
      - condition: template
        value_template: "{{ trigger.event.data.new_state.domain == 'light' }}"
      - condition: template
        value_template: >-
          {{ trigger.event.data.new_state.domain == 'binary_sensor' 

          and trigger.event.data.new_state.attributes.device_class | default('')
          == 'door'

          or trigger.event.data.new_state.attributes.device_class | default('')
          == 'lock'

          or trigger.event.data.new_state.attributes.device_class | default('')
          == 'window'

          or trigger.event.data.new_state.attributes.device_class | default('')
          == 'garage_door'  }}
      - condition: template
        value_template: "{{ trigger.event.data.new_state.domain == 'person' }}"
      - condition: template
        value_template: "{{ trigger.event.data.new_state.domain == 'input_boolean' }}"
      - condition: template
        value_template: >-
          {{ trigger.event.data.new_state.domain == 'sensor' and
          trigger.event.data.new_state.attributes.friendly_name == "Zonas
          Anuladas"  }}

it doesn’t work.
What am I missing?

Your trigger is not a state trigger.

See how you’re accessing new and old state in your other templates?

Now look at your new condition’s template that you added

I always get lost with template… and various other things. :roll_eyes:
And I used the condition from another automation.

I changed it to (I added “on” just for testing):


{{ trigger.event.data.new_state.state not in ['unknown', 'unavailable','none', 'on'] }}

And it’s working.

Thanks

1 Like

Hey petro could you please help my with my problem. I tried everything but I dont understand why my program doesnt work. If you want to help me please write under my topic. Thanks;)

Me again.

Although the conditions are working the way I want, I get this error from time to time:

2023-03-12 21:30:23.206 ERROR (MainThread) [homeassistant.helpers.template] Template variable error: 'None' has no attribute 'attributes' when rendering '{{ trigger.event.data.new_state.attributes.device_class == 'door'
or trigger.event.data.new_state.attributes.device_class == 'lock'
or trigger.event.data.new_state.attributes.device_class == 'window'
or trigger.event.data.new_state.attributes.device_class == 'garage_door'  }}'
trigger:
  - platform: event
    event_type: state_changed
    event_data: {}
condition:
  - condition: template
    value_template: "{{ trigger.event.data.new_state.state not in ['unknown', 'unavailable'] }}"
  - condition: or
    conditions:
      - condition: template
        value_template: "{{ trigger.event.data.new_state.domain == 'light' }}"
      - condition: template
        value_template: >-
          {{ trigger.event.data.new_state.attributes.device_class == 'door'

          or trigger.event.data.new_state.attributes.device_class == 'lock'

          or trigger.event.data.new_state.attributes.device_class == 'window'

          or trigger.event.data.new_state.attributes.device_class ==
          'garage_door' }}
      - condition: template
        value_template: "{{ trigger.event.data.new_state.domain == 'person' }}"
      - condition: template
        value_template: "{{ trigger.event.data.new_state.domain == 'input_boolean' }}"
      - condition: template
        value_template: >-
          {{ trigger.event.data.new_state.entity_id == 'sensor.pc'

          or trigger.event.data.new_state.entity_id ==
          'sensor.particoes_ativadas'

          or trigger.event.data.new_state.entity_id == 'sensor.zonas_anuladas'
          }}