I’m not 100% clear on what you want.
You want a smart switch to toggle the states of A and B such that, if A is on and you push the button, A turns off and B turns on.
This would also include A and B both on. In this case, only A would turn off and B would remain on.
Otherwise, you want it to turn on A and turn off B. So if both lights are off, it will only turn on A. If only B is on, it will turn off B and turn on A.
- alias: Toggle Lights
trigger:
platform: state
entity_id: switch.my_switch
action:
# Need to do 2 actions. One for A and one for B.
# First, turn on or off A depending on the state of A.
# This might turn A off if it was already off. Oh well, nothing bad will happen.
# We can't have an empty service, so we have to call SOMETHING no matter what.
# So turn the light off if it's off it is.
- service_template: "light.turn_{{ 'off' if is_state('light.lightA', 'on') else 'on' }}"
entity_id: light.lightA
# Now control light B. It strictly follows light A (which has already been handled).
# Thus, if light A is off, this must turn on.
# Otherwise, if light A is on, this must turn off.
- service_template: "light.turn_{{ 'on' if is_state('light.lightA', 'off') else 'on' }}"
entity_id: light.lightB
That automation will do exactly as you asked.
But, to control 2 lights with one button…that could mean something else.
Specially, scroll down to the examples. You can have one switch control 2 lights, copycat, mirror, etc.