The standard wiring pattern in USA (but not necessarily elsewhere in the world) for 110/120 AC is
black - hot
white - neutral
bare or green - ground
The hot wire brings the current to the outlet or fixture, often through a switch.
The neutral completes the circuit back to the panel.
The ground is a safety mechanism that, hopefully, will shunt the hot current to the ground (literally) rather than through a person if there is a short.
The terminals of a single pole switch (the only switch in a circuit and turning on/off one device or one set of devices) get the black wires, one on each terminal. Many switches also have a grounding terminal. This is not one of the switch terminals, but is usually a green screw on the metal strap of the switch frame, often on the opposite side from the terminal screws.
In general the white wires in a box should always be connected to each other, and never to or through a switch.
Similarly, the ground (bare) wires should be connected to each other and to the ground screw if the switch has one.
In your case, one set of wires (black, white, bare) are the source of the power, and one set (also black, white, bare) go to the fixture.
More complicated systems are possible with more colors, more switching choices and other configurations, but they don't apply in this case.
To work on this, make sure that the power is off (a non contact tester is best), before handling any wires.
It sounds like the two black wire with the pigtail are the incoming hot and a branch hot to another location, such as the outlet. The other black attached to the switch is probably the switched hot that goes to the fixture being controlled.
You can verify this by turning the switch to off, making sure all the wires and terminals are clear and not touching anything else metal, and then turning on the breaker. Using the non-contact tester, carefully check the wires. The paired blacks should read hot, but the switched black should not.
If this is what you have, wiring the new switch is pretty straightforward. Turn the breaker off again. Confirm no wires are now hot. The new switch is basically connected the same way as the old, but with a neutral wire and ground wire added.
- The hot pigtail is connected to the line terminal.
- The switched black wire is connected to the load terminal.
- A pigtail (white) is added to the bundle of neutral wires and connected to the neutral terminal
- A pigtail (bare) is added to the bundle of ground wires and connected to the ground terminal.
The traveler terminal is not used (and it looks like it is covered anyway).
Use wire nuts, and if you like, tape over them for extra safety. You also could put a wrap of tape around the z-switch covering the terminals. Carefully insert the wires back in the box. Screw the switch into the box. Turn the power back on.
While most dumb switches do not need a neutral connection, many smart switches, such as the z-switch, do. Now all switches have a separate ground connection, although many in the past did not.
Best Answer
Your switches are single pole double throw with center off. You have a hot black pigtailed to the two switched with the red wire nut in the front of your switch box. The light switch has a yellow wire and a brown wire out of it, spliced together and connected to a black wire in one of the NM cables. The switch was probably meant for a light/nightlight combination unit and the old unit only had a light, as your new one does. The switch will operate the light in both positions with center off Where your black hot enters the switch box, you'll have a white neutral that should be connected to a white wire from one of the cables going to the ceiling. That should put a black switched hot and a neutral up at the ceiling, possibly the one marked "l." The other switch has a blue wire and a red wire that would be switched hots to turn on the vent or the heat depending on the position on the switch. The blue wire is connected to a black wire going up to the ceiling and the red wire appears to be connected to the other white wire going up to the ceiling, possibly the other cable. Which ever white wire at the ceiling is the neutral, you'll connect it to the three white wires out of your new unit with a wire nut. The other white wire that appears to be a switched hot will go to the red wire out of your unit. That white wire should have some black tape wrapped around it to show it's hot. One of the black wire out of the ceiling, possibly the one marked "l." will go to the blue wire out of your unit for the light. The other black will go to the black wire from your unit for the vent. You will need to verify which wire is which, we can't do that from here. You also need to verify the breaker will handle the load of the heater. The copper wires, ground wires, need to be connected together in the box and to the box. They also need to be connected at the ceiling and to the green wire out of your unit. The two cables going into your unit need the proper connector to secure them to the unit. The two cables are not code compliant and one of the NM cables would need to be replaced with a three wire plus ground to meet code.