Automation with custom sensor state not working

Hello, could somebody let me know why is not working the following automation?
I just want a lamp to be switch on when a combination of door/windows are closed, but it does not work and I can’t figure out what is the problem.

This is the automation:

alias: Lampara Auto On con Ventanas
description: ''
trigger:
  - platform: state
    entity_id: sensor.cierres_casa_contact
    to: 'off'
    from: 'on'
condition: []
action:
  - type: turn_on
    device_id: add6e2a654b14e9fce3f7e6319224b0c
    entity_id: switch.lampara_salon_2
    domain: switch
mode: single

while the custom sensor, which is correctly working, is the following one:

cierres_casa_contact:
    friendly_name: "Cierres de Casa"
    value_template: >-
      {% if is_state('binary_sensor.puerta_terraza_contact', 'off') and is_state('binary_sensor.ventana_despacho_contact', 'off')
      and is_state('binary_sensor.ventana_dormitorio_contact', 'off') and is_state('binary_sensor.ventana_habitacion_contact', 'off') and is_state('binary_sensor.ventana_vestidor_contact', 'off') and is_state('binary_sensor.puerta_cocina_contact', 'off') %}
        Off
      {% elif is_state('binary_sensor.puerta_terraza_contact', 'off') and is_state('binary_sensor.ventana_despacho_contact', 'off')
      and is_state('binary_sensor.ventana_dormitorio_contact', 'off') and is_state('binary_sensor.ventana_habitacion_contact', 'off') and is_state('binary_sensor.ventana_vestidor_contact', 'off') and is_state('binary_sensor.puerta_lavadero_contact', 'off') %}
        Off
      {% elif is_state('binary_sensor.puerta_terraza_contact', 'off') and is_state('binary_sensor.ventana_despacho_contact', 'off')
      and is_state('binary_sensor.ventana_dormitorio_contact', 'off') and is_state('binary_sensor.ventana_habitacion_contact', 'off') and is_state('binary_sensor.ventana_vestidor_contact', 'off') and is_state('binary_sensor.ventana_lavadero_contact', 'off') %}
        Off
      {% else %}
        On
      {% endif %}

Many thanks in advance

What does the trace say?

You could try:


action:
  - service: switch.turn_on
    target:
      entity_id: switch.lampara_salon_2

I already tried to do the action by calling the service but it does not work too.
In my opinion it’s not a problem with the action but with the trigger, since if I change the custom sensor by whatever device, the automation works.
I’m new to HA and I’m starting dealing with custom sensors, so now I have the doubt if an state change in a custom sensor could be used as a trigger…

Then I would change Off to off in your sensor.

This is embarrassing…
The error was exactly that one…
I modified the sensor and now the automation is working.
Thank you very much for your help!

Glad to hear! And it’s not embarrassing, it’s life :slightly_smiling_face: