Turn off lamp after x seconds beeing turned on with sonoff pir + sonoff gateway

Hi all !
I’m new with HA but already have some things configured and automated. Both the sonoff basic and bridge have tasmota installed.

I have a sonoff basic connected to a lamp and already configured in HA.
The sonoff bridge is sending mqtt codes to HA when the PIR2 detects movement.
The automation is set when the bridge sends the PIR code, the lamp turns on.

What I’m trying to do is, after the lamp has been turned on, it turns off after x minutes.
I don’t have any binary sensor configured. Here’s the configurations.

configuration.yaml (sonoff basic switch)

switch:
   - platform: mqtt 
   name: "sonoff_lamp"
   state_topic: "stat/sonoff_lamp/POWER"
   command_topic: "cmnd/sonoff_lamp/power"
   availability_topic: "tele/sonoff_lamp/LWT"
   payload_available: "Online"
   payload_not_available: "Offline"
   qos: 1
   payload_on: "ON"
   payload_off: "OFF"
   retain: true 

And automations.yaml

- alias: Light on
  hide_entity: true
  trigger:
     platform: mqtt
     topic: tele/sonoffBridge/RESULT
  condition:
      condition: template
      value_template: '{{trigger.payload_json.RfReceived.Data == "EEA0FE" }}'
  action:
      service: homeassistant.turn_on
      data:
              entity_id: switch.sonoff_lamp

This is working.

I’m trying to turn it off after a few minutes.
This is the code I’m using - but is not correct

- alias: Light off
trigger:
      platform: state
      entity_id: switch.sonoff_lamp
      from: 'ON'
      to: 'OFF'
      for:
              minutes: 1
condition:
      condition: state
      entity_id: switch.sonoff_lamp
      state: 'ON'
action:
      service: homeassistant.turn_off
      entity_id: switch.sonoff_lamp

I’ve seen configurations with binary sensors defined, but don’t know if its a similar approach or a better one.
Thank you for any help

Your second automation is from on to off. Shouldn’t that be reversed?

The formatting got kinda weird. Make sure the spacing is correct.

HA is case sensitive. Try this:

      from: 'on'
      to: 'off'
      for:
        minutes: 1

Also your indentation is a bit off.

Thank you ! Will try that and see what happens.

I know it’s a bit off, but it has been working… It’s because i’m using vim. Will try to configure it for yaml.

Thank you ! Will post any changes

The condition needs case adjustment too:

condition:
  condition: state
  entity_id: switch.sonoff_lamp
  state: 'on'

Thank you all for the replies… But no luck…still doesn’t turns off the light…

That’s no surprise. Look closely at your light off automation’s trigger and condition.

  • The trigger requires the light to be off for 1 minute.
  • The condition requires the same light to be on.

The automation is triggered when the light remain off for a minute so the condition, requiring the light to be on, will never be fulfilled.

I don’t know why you added that condition but if you remove it, the automation will work.

Thank you !

Reading your comment made me look better at the code and realize (I’m a newbie in HA) that I had the trigger conditions switched, so, here it is: working:

- alias: Light off
  trigger:
    platform: state
    entity_id: switch.sonoff_lamp
    from: 'off'
    to: 'on'
    for:
       minutes: 1
action:
   service: homeassistant.turn_off
   data:
     entity_id: switch.sonoff_lamp

Thank you !