How to automate light switch and cover close

Hello,

i want to have one button with two actions.
First action will switch light to on and second action will close cover.
Could you help me how to do it?
How the code in automation.yaml is supposed to look?

Regards,
Lukasz

Just add more entries under “action”:

action:
- service: switch.turn_on
  entty_id: switch.foo
- service: cover.close_cover
  entity_id: cover.bar

Thanks, but what should be in trigger condition or on automation can be only actions?

  • alias: “Somehing”
    Trigger: ???
  • action:
    • service: switch.turn_on
      entity_id: switch.foo
    • service: cover.close_cover
      entity_id: cover.bar

What is the button you wish to use?

I want to have one button on lovelace UI and after I wilk click on this button, then lamp should turn on and cover should close.

You can either have the button send an “event” and use the event to trigger the automation.
Or you can write the “automation” as a script (which don’t have a trigger) and have the button run the script.

Do you want the same button to also be able to do the opposite actions: turn off light and open cover?

Have a look at the Button card. You can set its tap_action to call a script. The documentation even includes an example that meets your requirements:

Example

type: button
name: Turn Off Lights
show_state: false
tap_action:
  action: call-service
  service: script.turn_on
  service_data:
    entity_id: script.turn_off_lights

It will be good :slightly_smiling_face:

Define an input_boolean:

input_boolean:
  my_test:
    name: My Test

Then use this automation:

- alias: My Test
  trigger:
    - platform: state
      entity_id: input_boolean.my_test
  action:
    - service_template: >
        switch.turn_{{trigger.to_state.state}}
      entity_id: switch.my_light
    - service_template: >
        cover.{{'close' if trigger.to_state.state == 'on' else 'open'}}_cover
      entity_id: cover.my_cover

Of course, you will need to change the name of the switch and cover to whatever you are using.

@Taras Thank you very much for your code.

Lamp is switching correctly, but nothing happen with cover.

I try to made something like this:

- alias: "sleeping child"
  trigger:
    - platform: state
      entity_id: input_boolean.sleeping_child
  action:
    - service_template: >
       switch.turn_{{trigger.to_state.state}}
      entity_id: switch.lamp_at_Sam
    - service_template: >
         {% if trigger_to_state.state == "on" %}
           cover.close_cover
         {% else %}
           cover.open_cover
         {% endif %}
      entity_id: cover.roleta_at_Sam

But still nothing happen with cover.
Could help me why?

This:

    - service_template: >
        cover.{{'close' if trigger.to_state.state == 'on' else 'open'}}_cover

and this:

    - service_template: >
         {% if trigger.to_state.state == "on" %}
           cover.close_cover
         {% else %}
           cover.open_cover
         {% endif %}

are equivalent. If neither of them work, then it suggests the problem is not the template but something else.

In Home Assistant, the name of an entity_id is always in lowercase. However, I noticed you specified entity_ids with uppercase S.

      entity_id: switch.lamp_at_Sam

      entity_id: cover.roleta_at_Sam

Look at Developer Tools > States and you will see that all names are lowercase.

I suggest you change the uppercase S to lowercase s and try again.

- alias: "sleeping child"
  trigger:
    - platform: state
      entity_id: input_boolean.sleeping_child
  action:
    - service_template: >
       switch.turn_{{trigger.to_state.state}}
      entity_id: switch.lamp_at_sam
    - service_template: >
        cover.{{'close' if trigger.to_state.state == 'on' else 'open'}}_cover
      entity_id: cover.roleta_at_sam

If the cover still fails to work, then I suggest you test its operation directly from Developer Tools > Services. If you can’t control the cover that way, there may be a fundamental communication problem between Home Assistant and the physical cover.

yeah, so I changed name to lower case.
And also checked on service call developer tool and it works okay.
I want to add that I have separate card for cover and it works okay too.

This is my code now:

- alias: "sleeping child"
  trigger:
    - platform: state
      entity_id: input_boolean.sleeping_child
  action:
    - service_template: >
       switch.turn_{{trigger.to_state.state}}
      entity_id: switch.lamp_at_sam
    - service_template: >
       cover.{{"close" if trigger_to_state.state == "on" else "open"}}_coverover
      entity_id: cover.cover_at_sam

And still only lamp works. Cover is not moving.

If this is actual cut and paste, you have written “_coverover” in the automation.

No no, sorry.
I Fixed it.
It is just _cover

So my code right now is:

- alias: "sleeping child"
  trigger:
    - platform: state
      entity_id: input_boolean.sleeping child
  action:
    - service_template: >
       switch.turn_{{trigger.to_state.state}}
      entity_id: switch.lamp_at_sam
    - service_template: >
       cover.{{"close" if trigger_to_state.state == "on" else "open"}}_cover
      entity_id: cover.cover_at_sam

and as I said earlier. Cover is not moving.
@Olen edited

If this is indeed a copy and paste of your actual automation, you are using the switch entity_id for the cover.open/close-service.

I see you are using two different ways to refer to the cover. Which of these two is the correct one? cover.roleta_at_sam
cover.cover_at_sam

If this is indeed a copy and paste (which I am starting to suspect it isn’t) you are using a “_” instead of a “.” in if trigger_to_state.state (it should be trigg*er.to_state…).

Please. If you need help. At least show us your ACTUAL code, not some random, made up stuff that might or might not be slightly similar to what you are using in your automation.

Ctrl-c, Ctrl-v is your friend.

That’s probably due to a typo I made (and have since corrected) in my original post and he used it verbatim.

We now know that the cover works (confirmed it’s functional when called via Developer Tools > Services) so the problem is probably due to a simple typo in the cover’s service_template.

Assuming the cover’s entity_id is cover.cover_at_sam then this should work:

- alias: "sleeping child"
  trigger:
    - platform: state
      entity_id: input_boolean.sleeping child
  action:
    - service_template: >
        switch.turn_{{trigger.to_state.state}}
      entity_id: switch.lamp_at_sam
    - service_template: >
        cover.{{"close" if trigger.to_state.state == "on" else "open"}}_cover
      entity_id: cover.cover_at_sam
1 Like

Thank you!
Yes, I have not realized that this is a method. Stupid me :slight_smile:
Works now as expected :slight_smile:

1 Like