Help with automation trigger itself when I reloaded Xiaomi gateway integration

Hi all,

I’m having trouble with this automation, I have many automation configured and somehow when I manually trigger the reload of ‘Xiaomi Miio’ or ‘Xiaomi Gateway 3’ Integration, basically reload the gateway integration, some of the automation will auto trigger itself, below is one of the examples.

I am running out of idea what could be the root cause, please enlighten me If you have the solution, thanks!

This is the code I configured.

alias: Kitchen_Temperature Sensor - Below 32°C - Exhaust Fan Off
description: ""
trigger:
  - type: temperature
    platform: device
    device_id: c183c245192668cc0898c5be02ccea77
    entity_id: sensor.0x00158d0001199959_temperature
    domain: sensor
    below: 32
condition:
  - condition: numeric_state
    entity_id: sensor.0x00158d0001199959_temperature
    below: 32
action:
  - type: turn_off
    device_id: f92eb9540ab9c3bebc82efa66a535d22
    entity_id: switch.0x00158d000153b15b_channel_1
    domain: switch
mode: single

This is the ‘Change Variables’ I copied and pasted from ‘Traces’

this:
  entity_id: automation.kitchen_temperature_sensor_below_32degc_exhaust_fan_off
  state: 'on'
  attributes:
    last_triggered: '2022-09-19T16:03:58.560984+00:00'
    mode: single
    current: 0
    id: '1634490711067'
    friendly_name: Kitchen_Temperature Sensor - Below 32°C - Exhaust Fan Off
  last_changed: '2022-09-19T16:01:15.015996+00:00'
  last_updated: '2022-09-19T16:03:58.568623+00:00'
  context:
    id: 01GDB7VWZ0K5P1WTXP6PRAY17T
    parent_id: 01GDB7VWYDJTDY234SXWXJBBWE
    user_id: null
trigger:
  id: '0'
  idx: '0'
  alias: null
  platform: device
  entity_id: sensor.0x00158d0001199959_temperature
  below: 32
  above: null
  from_state:
    entity_id: sensor.0x00158d0001199959_temperature
    state: unavailable
    attributes:
      restored: true
      state_class: measurement
      device_class: temperature
      friendly_name: Kitchen Thermometer Sensor Temperature
      supported_features: 0
      unit_of_measurement: °C
    last_changed: '2022-09-19T16:05:32.982250+00:00'
    last_updated: '2022-09-19T16:05:32.982250+00:00'
    context:
      id: 01GDB7YS5P1Y3FJGHYESRSK9AX
      parent_id: null
      user_id: null
  to_state:
    entity_id: sensor.0x00158d0001199959_temperature
    state: '30.78'
    attributes:
      state_class: measurement
      native_value: 30.78
      unit_of_measurement: °C
      device_class: temperature
      friendly_name: Kitchen Thermometer Sensor Temperature
    last_changed: '2022-09-19T16:05:33.556502+00:00'
    last_updated: '2022-09-19T16:05:33.556502+00:00'
    context:
      id: 01GDB7YSQM0G31N6ATNYCD2AER
      parent_id: null
      user_id: null
  for: null
  description: numeric state of sensor.0x00158d0001199959_temperature

When you reload the integration, the value of its sensors is reset to unavailable first and then to the actual value. The temperature sensor’s change from unavailable to a numeric value below 32 is what triggers the Numeric Device Trigger.

Add a Template Condition that blocks if the sensor’s previous value was unavailable.

alias: Kitchen_Temperature Sensor - Below 32°C - Exhaust Fan Off
description: ""
trigger:
  - type: temperature
    platform: device
    device_id: c183c245192668cc0898c5be02ccea77
    entity_id: sensor.0x00158d0001199959_temperature
    domain: sensor
    below: 32
condition:
  - condition: numeric_state
    entity_id: sensor.0x00158d0001199959_temperature
    below: 32
  - condition: template
    value_template: "{{ trigger.from_state.state != 'unavailable' }} "
action:
  - type: turn_off
    device_id: f92eb9540ab9c3bebc82efa66a535d22
    entity_id: switch.0x00158d000153b15b_channel_1
    domain: switch
mode: single

FWIW, the Numeric State Condition is redundant because it simply confirms what the Numeric Device Trigger is already doing (i.e. it triggers only when the temperature decreases below the threshold value of 32). It can be removed.

This is what I am looking for 123 Tara, let me apply to my Automations and I shall update the outcome here, thanks!

alias: Kitchen_Temperature Sensor - Below 32°C - Exhaust Fan Off
description: ""
trigger:
  - type: temperature
    platform: device
    device_id: c183c245192668cc0898c5be02ccea77
    entity_id: sensor.0x00158d0001199959_temperature
    domain: sensor
    below: 32
condition:
  - condition: template
    value_template: "{{ trigger.from_state.state != 'unavailable' }} "
action:
  - type: turn_off
    device_id: f92eb9540ab9c3bebc82efa66a535d22
    entity_id: switch.0x00158d000153b15b_channel_1
    domain: switch
mode: single

What if I want to add an Unknown state as well?

can I use 1 line as

{{ trigger.from_state.state != 'unavailable' or 'unknown}} 

or I shall add one more template instead?

condition:
  - condition: template
    value_template: >
              {{ trigger.from_state.state != 'unavailable' }}
  - condition: template
    value_template: >
              {{ trigger.from_state.state != 'unknown' }}

If it fails to work, replace the Numeric Device Trigger with a Numeric State Trigger.

alias: Kitchen_Temperature Sensor - Below 32°C - Exhaust Fan Off
description: ""
trigger:
  - platform: numeric_state
    entity_id: sensor.0x00158d0001199959_temperature
    below: 32
condition:
  - condition: template
    value_template: "{{ trigger.from_state.state != 'unavailable' }} "
action:
  - type: turn_off
    device_id: f92eb9540ab9c3bebc82efa66a535d22
    entity_id: switch.0x00158d000153b15b_channel_1
    domain: switch
mode: single
condition:
  - condition: template
    value_template: "{{ trigger.from_state.state not in ['unavailable', 'unknown'] }}"

I did this for another automation, it sync the status if AirConditioner is triggered manually by the physical remote control.

alias: Study Room_OC Sensor - Open - Turn On AC
description: ""
trigger:
  - type: opened
    platform: device
    device_id: bbf68c05e40aa566d484c6c785f90ff4
    entity_id: binary_sensor.0x54ef441000250727_contact_2
    domain: binary_sensor
condition:
  - condition: template
    value_template: "{{ trigger.from_state.state != 'unavailable' }} "
  - condition: template
    value_template: "{{ trigger.from_state.state != 'unknown' }} "
action:
  - service: switch.turn_on
    data: {}
    target:
      entity_id: switch.study_room_sensibo_ac_switch
mode: single

Did what I suggested prevent the automation from triggering after the integration was reloaded?

I added the coding below to all my Automation and monitor for a day and didn’t encounter any issue, thanks, issues resolved.

  condition:
  - condition: template
    value_template: '{{ trigger.from_state.state not in [''unavailable'', ''unknown'']
      }}'
  action:

seems that i shouldn;t at it to automation that triggers by timer, have to remove it or else wont work. :-/

If the automation is triggered by a Time Trigger, the trigger variable doesn’t have from_state and to_state properties, it only has platform and time properties.

got it, thanks.