Problem with light automation

This automation does not work, it runs regardless of the state. The switch acts on a latching relay and returns the status via a detector on the output. The switch works and the icon changes regularly between blue and yellow based on whether the lamp is on or off.
Where am I wrong?
Thank you

# Luce portico
  - platform: mqtt
    unique_id: portico_switch
    name: "portico"
    state_topic: "ESP_8_Termocamino/Lampada_portico_stato/State"
    command_topic: "ESP_8_Termocamino/cmd"
    payload_on: "LongPulse_mS,12,1,250"
    payload_off: "LongPulse_mS,12,1,250"
    state_on: "1"
    state_off: "0"
    optimistic: false
    qos: 0
    retain: false

# -------------------------
# ----- Automations ------------
# -------------------------
# Accende luce portico al tramonto
  - alias: 'Rule 1 Light on portico 20 dopo il tramonto'
      - platform: sun
        event: sunset
        offset: '00:15:00'
      - condition: state
        entity_id: 'switch.portico'
        state: "off"
      service: switch.turn_on
      entity_id: switch.portico
# Spegne luce portico alle 2
  - alias: 'Rule 2 Light off portico alle 2'
      - platform: time
        at: '02:00:00'
      - condition: state
        entity_id: 'switch.portico'
        state: "on"
      service: switch.turn_off
      entity_id: switch.portico

To allow for effective moderation and be inclusive of the majority this forum uses the English language.

Please edit your post and translate your question to English (online translators will do if you do not speak English).

Per consentire un’efficace moderazione ed essere inclusivo della maggioranza, questo forum utilizza la lingua inglese.

Per favore modifica il tuo post e traduci la tua domanda in inglese (i traduttori online lo faranno se non parli inglese).