Turn on/opff lights

i have this automation:

- id: '12311155342113132423412'
  alias: lampen aan voor 6 uur
  trigger:
    - platform: time
      at: 
        - '12:00:00'
        - '18:00:00'
  action:
    - variables:
        t: "({{ now().hour }},{{ now().minute  }})"
    - service: "switch.turn_{{ 'on' if t >= (12,00) or t < (18,00) else 'off' }}"
      target:
        entity_id: switch.tasmota_light1

i cannot get it to work. Is this a correct automation?

I think variable should be like trigger and action:

- id: '12311155342113132423412'
  alias: lampen aan voor 6 uur
  trigger:
    - platform: time
      at: 
        - '12:00:00'
        - '18:00:00'
  variables:
    t: "({{ now().hour }},{{ now().minute  }})"
  action:
    - service: "switch.turn_{{ 'on' if t >= (12,00) or t < (18,00) else 'off' }}"
      target:
        entity_id: switch.tasmota_light1

Your variable t is a string so cannot compared in the way that you are comparing it. Additionally it is not needed there, it can be used directly in the service call.

{% set t = now.hour()|int %}{{ "on" if (t >= 12 or t < 18) else "off" }}

Now the question is - did you mean or, or did you mean and?
OR means the light will be turned on is the hour is after 11am OR before 6pm otherwise it will be off.
AND means the light will be turned on if the hour is after 11am AND the hour is before 6pm.

In this case, your automation will trigger at 12pm and turn the light on, and when it triggers again at 6pm it will turn the light off.

No because 00 is not a valid integer. Just use 0 in the two tuples (12,0) and (18,0).

However, why do you believe there’s a need for the template?

The automation’s Time Trigger can only occur at 12:00 and 18:00 (when the light will be turned on). How will the template ever report off if the Time Trigger never triggers at any other times than 12 and 18?

i get this:

  action:
    - service: "switch.turn_{% set t = now.hour()|int %}{{ "on" if (t >= 12 or t < 18) else "off" }}"

----
bad indentation of a sequence entry at line 393, column 61:
     ... {% set t = now.hour()|int %}{{ "on" if (t >= 12 or t < 18) else  ... 
                                         

There’s no apparent need for the template because the Time Trigger cannot trigger at any other times than 12:00 and 18:00.

- id: '12311155342113132423412'
  alias: lampen aan voor 6 uur
  trigger:
    - platform: time
      at: 
        - '12:00:00'
        - '18:00:00'
  action:
    - service: switch.turn_on
      target:
        entity_id: switch.tasmota_light1

If you want the switch to be turned off at specific times, you will need to specify them in the Time Trigger and then the template will be required.

well, i just created 2 automations for now, turn on at 12:00, turn off at 18:00
but i wanted to combine these in 1 automation

This might be what you are looking for.

- id: '12311155342113132423412'
  alias: lampen aan voor 6 uur
  trigger:
    - platform: time
      at: 
        - '12:00:00'
        - '18:00:00'
  action:
    - service: "switch.turn_{{ 'on' if now().hour == 12 else 'off' }}"
      target:
        entity_id: switch.tasmota_light1

If you want to make it more robust with the ability to set the switch to the correct state after a restart:

- id: '12311155342113132423412'
  alias: lampen aan voor 6 uur
  trigger:
    - platform: homeassistant
      event: start
    - platform: time
      at: 
        - '12:00:00'
        - '18:00:00'
  action:
    - service: "switch.turn_{{ 'on' if 12 <= now().hour < 18 else 'off' }}"
      target:
        entity_id: switch.tasmota_light1