Automation help with sun

I would like to create an automation that during the day when it is the sun opens the door with the sensor and after sunset it also turns on a light

alias: Apertura Porte di Casa Impronta Indice DX Luca e Maria 2
description: ''
trigger:
  - platform: state
    entity_id: sensor.fingerprint_id
    from: '11'
  - platform: state
    entity_id: sensor.fingerprint_id
    from: '21'
condition: []
action:
  - device_id: 2c136e2a7xxxxxxxxxxxxxxxxxxx
    domain: lock
    entity_id: lock.portone_di_casa_2
    type: unlock
  {% if states ('sun.sun',below_horizon) %}
  - service: shell_command.luce_salotto     
mode: single

Add a Template Condition after unlocking the lock. If the sun is below_horizon it will also execute the shell_command.

action:
  - device_id: 2c136e2a7xxxxxxxxxxxxxxxxxxx
    domain: lock
    entity_id: lock.portone_di_casa_2
    type: unlock
  - condition: template
    value_template: "{{ is_state('sun.sun', 'below_horizon') }}"
  - service: shell_command.luce_salotto     
mode: single
1 Like

You can also use a standard state condition:

action:
  - device_id: 2c136e2a7xxxxxxxxxxxxxxxxxxx
    domain: lock
    entity_id: lock.portone_di_casa_2
    type: unlock
  - condition: state
    entity_id: sun.sun
    state: below_horizon
  - service: shell_command.luce_salotto     
mode: single

Or Sunset/Sunrise Condition. :slightly_smiling_face:

Regardless of whichever type of condition you choose to use, the point is that to achieve your goal you must insert a condition before the final service call (shell_command). What you currently have is a template that is not part of anything and makes the automation invalid.

action:
  - device_id: 2c136e2a7xxxxxxxxxxxxxxxxxxx
    domain: lock
    entity_id: lock.portone_di_casa_2
    type: unlock
  - condition:
      condition: or
      conditions:
        - condition: sun
          after: sunset
        - condition: sun
          before: sunrise
  - service: shell_command.luce_salotto     
mode: single

thanks I’ll try tonight and let you know