Differences between switch platform state and switch on/off changes

Hi experts,

I would like to know the fundamental differences between writing an trigger automation for a sonoff switch platform state OR a trigger for a switch turned on/off. Both the below works and I am trying to see the difference.

Technically, I would like to only catch the switch turning on/off by a user or a service. But seems that the automation triggers also when the unreliable WIFI knocks out the switch for a few minutes - and I don’t need that to be triggered.

Any advice please ?

Automation 1

alias: Sonoff Switch State Change
description: ""
trigger:
  - platform: state
    entity_id:
      - switch.sonoff_100066a913
    for:
      hours: 0
      minutes: 3
      seconds: 0
condition: []
action:
  - service: ifttt.trigger
    data:
      event: Sonoff_T_Switch_State_Change
      value1: "{{ trigger.from_state.attributes.friendly_name }}"
mode: single

Automation 2

alias: "Sonoff Switch OnOff Change"
description: ""
trigger:
  - platform: device
    type: changed_states
    device_id: 74113e4baff67904e45de6b8e35a9a5c
    entity_id: switch.sonoff_100066a913
    domain: switch
    for:
      hours: 0
      minutes: 3
      seconds: 0
condition: []
action:
  - service: ifttt.trigger
    data:
      event: Sonoff_T_Switch_State_Change
      value1: "{{ trigger.to_state.name }}"
mode: single
1 Like

Use option one as much as you can. Here’s why.

1 Like

Thank you @tom_l . Option 1 was my go-to precisely of the reasons you stated. All entity_ids in 1 place for simpler templating automation.

That said, how do I eliminate those pesky WIFI connects/reconnects triggers ?

Probably like this:

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

Let me try and reply back on the result. Thanks @tom_l

Tom’s answer is compact, but just want to mention you can also use the not_from and not_to options on triggers added earlier this year.

1 Like

Thank you. Interesting. I did not know this but this is not as intuitive as the ones Tom suggested. My old man memory will not remember why I added that when I go back to it 2 years down the road - LOL