Simple Light on/off - first automation

I am fascinated that I have to ask this, but… How to create automation to simply switch lights off/on?

I created a Condition on a state of the Switch. I can easily Call Service to light.turn_on (or off), depending on state transition. However, I don’t want to create two automation for every single switch/light. I expected that I can call a service to set light to the state of the switch, or some variable, or whatever…

What is the proper way to do that (in the Lovelace interface, not script)? Why are no blueprints for such simple tasks in the initial configuration?

You don’t need a automation for turning on off a light.
Just add the light itself to your lovelace

1 Like

Have a read about trigger ids.
I would create an automation that controls both the turning on and turning off in one.

As a plan:
Triggers

  • switch on (id on)
  • switch off (id off)

Actions
-chose
If id is on then call service to turn on switch
If id is off then call service to turn off

Sorry I’m using my phone and away from the computer so don’t have time to type up a yaml example

1 Like

Ok, here is something on that theme - this works, I am not sure if it’s optimal:

- id: '1630222098133'
  alias: Test svetlo on/off
  description: ''
  trigger:
  - platform: state
    entity_id: switch.kuhinja_radna_povrsina_m7
  condition: []
  action:
  - service_template: |
    {% if trigger.to_state.state == "on" %}
      light.turn_on
    {% else %}
      light.turn_off
    {% endif %}
    entity_id: light.led_ogledalo_apartman
  mode: single

You don’t need a automation for turning on off a light.
Just add the light itself to your lovelace

Everything is in Lovelace. My devices are KNX Integration.

I haven’t understood how to connect a Switch (or Binary Sensor or whatever) to a Light (or Whatever) without automation. What I click to edit a Light entity, I don’t see a way to add a Switch (or the other way round).

If the switch is somehow connected (paired) to the light, like you can do for example with LSC or Ikea remotes, you don’t need a connection switch ↔ light in HA. You either switch the light directly in HA or you steer the light by using the switch in HA.
If the switch is paired to more than one light you can group the lights in HA for having the same effect.
But maybe I got you wrong…

I think the point is that it’s quite cumbersome to associate/synchronize simple states (on/off) of different integrations in HA. I’d love this to be easier than creating an automation too.
For me, connecting otherwise siloed iot universes is the main usecases of HA.

2 Likes

OK. I get it now.
I can do that in KNX (ETS5) – my point was to understand automation.

Okay I see.
Just start with for example switching a light on, when another light was switched on in HA and get more complex, maybe adding delays, etc.

The post you marked with the Solution tag suggests using a choose and trigger.id but your requirements are so simple that none of those concepts are needed.

- id: '1630222098133'
  alias: Test svetlo on/off
  trigger:
  - platform: state
    entity_id: switch.kuhinja_radna_povrsina_m7
  action:
  - service: "light.turn_{{ trigger.to_state.state }}"
    target:
      entity_id: light.led_ogledalo_apartman
1 Like

Thank you, @123 – this is what I asked for.

Now I understand the use of templates better. What I don’t understand (can’t find clearly documented) is service_template (*_template) statements.

1 Like

…and I created a blueprint

blueprint:
  name: Switch-activated Light
  description: Turn on a light on/off following a switch.
  domain: automation
  input:
    switch_entity:
      name: Switch
      selector:
        entity:
    light_target:
      name: Light
      selector:
        target:
          entity:
            domain: light

mode: restart
max_exceeded: silent

trigger:
  platform: state
  entity_id: !input switch_entity

action:
  - alias: "Turn on/off the light"
    service: "light.turn_{{ trigger.to_state.state }}"
    target: !input light_target

You can’t find it documented because it was deprecated in favor of service many versions ago.

1 Like

This PR removes the need for service_template & data_template in service and event actions. Instead, service & data now accept templates.

1 Like

Why did you feel it was necessary to include this?

max_exceeded: silent

The specified mode is restart which has no max value to exceed.

Leftover. Thanks for noticing!