Can I use toggle and clone/copy instead of on/off

I have a wall switch that sends “on”, “off”, and even a toggle, and I want to send the relevant signal to another device. However it seems in automations I can only create seperate automations for on and off.

That is,
When switch activates ON, turn device 2 On
When switch activates OFF, turn device 2 OFF

I note that I can use
When switch toggles, toggle device 2, however, that relies on them being in sync to begin with

I’d like to be able to have an automation that basically says:

When switch toggles, clone the switch state to device 2

Am I able to do this? I think I may just not have the terminology down pat

How is the wall switch integrated with HA? And how the toggle message is seen in HA, is it seen as a mqtt message payload or?

yes it is via mqtt.

The wall switch and receiving power board are both tasmota

trigger:
  platform: state
  entity_id: switch.one
action:
  service: "switch.turn_{{ trigger.to_state.state }}"
  entity_id: switch.two
4 Likes

trigger.to_state not trigger_to_state :slight_smile:

1 Like

Fixed. Thank you.

You guys are awesome, for completeness, my application:

alias: Lights Kitchen On/Off
description: ''
trigger:
  - platform: state
    entity_id: switch.loungewallplate_lkitchen
action:
  - service: 'switch.turn_{{ trigger.to_state.state }}'
    entity_id: switch.kitchenlight_roofplugkitchen
mode: single

as an aside, this is how I have done it where I need two actions to take place, is this correct/the best way?

alias: Lights Big6 on/off
description: ''
trigger:
  - platform: state
    entity_id: switch.loungewallplate_l6lights
condition: []
action:
  - service: 'switch.turn_{{ trigger.to_state.state }}'
    entity_id: switch.arlecpb4_loungepbceil_socket1
  - service: 'switch.turn_{{ trigger.to_state.state }}'
    entity_id: switch.arlecpb4_loungepbceil_socket4
mode: single

Thanks again, this has cleaned up my automations something chronic!

edit: I need to learn more about this “service” call, in the GUI that’s not at all intuitive and needs to be edited in the yaml directly. Any hints/pointers there?

Just put both entities as a list instead of two separate service calls.

 service: 'switch.turn_{{ trigger.to_state.state }}'
 entity_id: 
   - switch.arlecpb4_loungepbceil_socket1
   - switch.arlecpb4_loungepbceil_socket4
1 Like

The automation editor only supports a fairly basic set of actions.

It could be done in the editor using the choose action instead of the service template but would end up being eight times as verbose. e.g.

action:
  - choose:
      - conditions:
          - condition: state
            entity_id: switch.one
            state: 'on'
        sequence:
          - service: switch.turn_on
            target:
              entity_id: switch.two
      - conditions:
          - condition: state
            entity_id: switch.one
            state: 'off'
        sequence:
          - service: switch.turn_off
            target: 
              entity_id: switch.two

That’s not necessarily a bad thing.
Makes it easy to read and understand for a newbie

I thought I tried that, but (I didn’t actually test it, I noted the code/gui/code editor seemed to drop the second line)…

testing again…

Yeah, doing it through the GUI and selecting “Edit as YAML” and adding:

entity_id: switch.arlecpb4_loungepbceil_socket3
entity_id: switch.arlecpb4_loungepbceil_socket4

only saves the first line. The yaml editor even has a red bar down the line numbers to indicate it doesn’t like the yaml.

I did figure multiple entity_ids would be ok, but I think I would need to edit the specific yaml directly??

yes and no. Whilst I can read what @tom_l has put there, I find that MORE difficult to read than @Burningstone.

I do like the idea of the GUI editor, but sometimes I think raw code is so much easier…

if only the yaml had some form of auto-completion!!!

Use VSCode to edit yaml with the Home Assistant helper extension installed.

1 Like

ooohhhhh, now we’re talking!

it’s 4am where I am, I dare not open that can of worms right now.

Thanks for the insight, I’ll look into that in the next day or two. Thanks very much (for ruining my life!) ROFL

Seriously though, thanks, I had no idea.

the reason it doesn’t like that is because you are adding “entity_id:” to both lines.

use the format recommended by burningstone above where it uses “entity_id:” once and both entities are under that denoted by a dash.

2 Likes

Yes, excellent that was it.

It wasn’t easy to fix and I think the reason I struggled with this is the Automations GUI editor is pretty unforgiving of editing in a way that it’s not expecting. It kept modifying what I was typing and where I was typing it.

eg, in the Automation Action I had this:

service: 'switch.turn_{{ trigger.to_state.state }}'
entity_id: switch.arlecpb4_loungepbceil_socket1

if you insert your cursor before the “s” in ‘switch’ of line two, to insert a new line and a dash, the “enter” key and first two spaces work, however as soon as you type the - it removes those spaces and newline and inserts quotes around it, resulting in:

service: 'switch.turn_{{ trigger.to_state.state }}'
entity_id: '-switch.arlecpb4_loungepbceil_socket1'

which is obviously wrong.

Having a dash on the line and backspacing over an existing entry always added the word null. Back spacing over null resuled in 'n'… arrrrgh! :slight_smile:

It was a pain to edit, but copying the existing YAML to an external editor and pasting it in, worked.
I also found that leaving something on the first line (in this case 'n') and inserting the required entities below this, I could then go back and remove the ancillary text. convoluted, but it worked.

This happened in Chrome on linux and Safari in OSX (I thought it may have been browser specific, but I don’t think so)

Anyway. as @Burningstone put above, worked perfectly.

Thanks to all! Cheers

edit: just thought I would include the full example, even though it is listed above

service: 'switch.turn_{{ trigger.to_state.state }}'
entity_id: 
  - switch.arlecpb4_loungepbceil_socket1
  - switch.arlecpb4_loungepbceil_socket3
  - switch.arlecpb4_loungepbceil_socket4

edit: should I report this editing thing as a bug? Being that it’s something outside of how the GUI editor is expecting, I feel that it’s not really relevant. I could make a video demo-ing the problem if need be

Yeah, there is a reason why a lot of the advanced users don’t use the UI editors.