Do several things when turning on/off

Hi there,

I have the following problem:
I have a switch for my computer and my TV to turn them on or off.

However, when I turn off my PC, I want it to shut down via RPC first and then turn off the switch.

The same with my TV. When I turn it on, I want the switch to be turned on first and then the TV to be turned on with the Samsung integration. When I turn it off, the TV should first be turned off with the Samsung integration and then the switch should be turned off.

For the shutdown via RPC or the Samsung integration, there are services that I can call.
However, how do I do this with the rest?

you can use the Samsung integration to get the state (on or off) of your device. Use an automation that turns the switch off after the tv has been of for a couple of minutes. Turn the tv on, after the switch has been turned on for a few seconds.

The pc might be observable using Device Tracker, but that could get hard as modern PCs are “always on”, keeping an active network link even if powered off.

Okay, I already had an idea in that direction. However, how can I set the PC switch, for example, so that the switch is switched on when it is switched on and the RPC command is sent when it is switched off, so that I can then switch the switch off with an automation?

Sound to me like you want to define certain scripts that do turn things on and off in the exact order as you mentioned here:

and if one thing requires another thing, take a look at the wait_template:

An alternative approach might be automations, where you can define things like:
Up on turning this switch on, turn also that switch on. However you have to be a bit careful with these kind of things as you can end up with switching loops here ( two swtiches toggle each other for ever). Best way to prevent this IMO is a Master/Slave relation between two switches. Meaning, one switch will just mimic the state of the other, but that is not possible in all situations.

For monitoring the state of a PC, have a look at the wake on lan component:

If I remember correctly it also monitors the state of your device.