Help with template for light - Welcome light

Writing a rule on the away/home state change. Evrything is working great and now i want to add a welcome light if it is dark.
Already have a sensor for night, evening day and a rule (from different set-up) like this

#  - service: script.turn_on
#       entity_id: >
#        {% if is_state("sensor.day_night", "Night") or is_state("sensor.day_night", "Evening") %}
#          script.onkyo_night
#        {% else %}
#          script.onkyo_day
#        {% endif %}

So how can i rewrite it for a single light? Something like

#  - service: script.turn_on
#       entity_id: light.123
#       data: >

???

If the entity is a light, then:

- service: light.turn_on
  entity_id: light.yourlight

Nice answer but i guess i wasn’t mor specific
What goes for the template?
I want it on only during specific time

data: >...????
        {% if is_state("sensor.day_night", "Night") or is_state("sensor.day_night", "Evening") %}
          light.turn_on???
        {% else %}
          nothing_done???
        {% endif %}

Use conditions to limit the time it is allowed to run.

It’s worth taking a read through the introduction guide that explains how the automation works, and how triggers, conditions and actions all link. Then you end up with something like:

trigger:
  platform: state
  entity_id: device_tracker.mobile
  state: home
condition:
  condition: sun
  after: sunset
action:
  service: switch.turn_on
  entity_id: switch.welcome

It’s easy enough to extend and for something that simple you don’t need a template.

Here is my Late night helper script.

https://github.com/CCOSTAN/Home-AssistantConfig/blob/master/automation/late_night_helper.yaml

Thank you - i have already stolen a lot from your configs.

I did. But getting familiar with HA means learning templating.
So instead of
Rule A
condition state from away to home do stuff A
Rule B
condition state from away to home do stuff B
Rule C
condition state from away to home do stuff C

I just want to have one Rule A and inside fork it
So in this case
Rule A
condition state from away to home
do stuff A
do stuff B
If Night - Do stuff C and if not do stuff D

I know it can be done just to undestand what to put for the templating. As a work around i can use scenes and than run a template like below. But want to operate directly with lights. So entity is already known it’s the action turn_on or turn_off that needs to be templated.

  - service: scene.turn_on
       entity_id: >
        {% if is_state("sensor.day_night", "Night") or is_state("sensor.day_night", "Evening") %}
          scene.welcome_night
        {% else %}
  NONE
       {% endif %}

By the way what can i put instead of NONE above? If i leave it blank - i get errors in HA log. So i have to put up an empty scene for this (something like scene.none)

If I’m understanding correctly, I think you’ll want to use service_template

action:
  service_template: >
    {% if is_state("sensor.day_night", "Night") or is_state("sensor.day_night", "Evening") %}
      light.turn_on
    {% else %}
      light.turn_off
    {% endif %}
  data:
    entity_id: light.name

You did! Thank you! Now everything cam together as a puzzle.
And just to emphasize what if here i wanted to do nothing. How can i state that nothing should be done between else and endif?

{% else %}
  light.turn_off
{% endif %}

If you want to do nothing in the else case, you should just put the template as a condition for the automation

condition:
  condition: template
  value_template: "{{ is_state('sensor.day_night', 'Night') or is_state('sensor.day_night', 'Evening') }}"

If you REALLY want to do it like this in a single automation, you can use a condition within your script to stop it from doing the last step unless it’s night or evening.

In your automation:

action:
  - service: script.do_stuff

In your script:

do_stuff:
  sequence:
    - service: light.turn_on
      entity_id: light.always
    - condition: template
      value_template: "{{ is_state('sensor.day_night', 'Night') or is_state('sensor.day_night', 'Evening') }}"
    - service: light.turn_on
      entity_id: light.night_or_evening

This way, light.always always turns on when your script runs and light.night_or_evening only turns on if it’s night or evening.

Or you can move some of that stuff from the script to the automation if you want.

automation:

action:
  - service: light.turn_on
    entity_id: light.always
  - service: script.morning
  - service: script.night_or_evening

script:

morning:
  sequence:
    - condition: template
      value_template: "{{ is_state('sensor.day_night', 'Morning') }}"
    - service: light.turn_on
      entity_id: light.morning

night_or_evening:
  sequence:
    - condition: template
      value_template: "{{ is_state('sensor.day_night', 'Night') or is_state('sensor.day_night', 'Evening') }}"
    - service: light.turn_on
      entity_id: light.night_or_evening

With that, light.always will always turn on, light.morning will only turn on in the morning, and light.night_or_evening will only turn on at night or in the evening.

Note that I’ve not tested any of this, but it should give you some ideas of how to accomplish what you want.