Sending different IR codes depending on current state

I have a humidifier controlled with IR remote.

It has three spray levels (1, 2, and 3, 3 being the highest) that can be controlled with one button on the remote or on the humidifier.

The behavior of the humidifier is as follows:

  1. When plugged into wall outlet and then turned on by pressing the power button (either on the remote or on the humidifier), the humidifier automatically starts to spray at level 3.
  2. If I press “Level” button once , the spray level goes to level 1.
  3. If I press “Level” button again, the spray level goes to level 2.
  4. the spray level goes 3→2→1→3→2→1 as “Level” button is pressed.
  5. When the humidifier is not unplugged, the spray level is maintained at next turn-on. However, since I always unplug the humidifier (turn off the smart plug) after use, this is of no concern.

The power consumption, which is monitored by the smart plug every 3 seconds, changes depending on the spray level. The humidifier consumes about 5-8W at spray level 1, about 12-16W at spray level 2, and about 19-23W at spray level 3.

I already captured IR codes and I can successfully control the humidifier with Esphome. Now I want to use select: component to enable selecting spray levels.

Since only a single IR code is used for changing the spray level (no separate IR codes for levels 1, 2, and 3), I have to use the power consumption (already defined as a sensor component in yaml) to infer the current spray level and use said current spray level to send proper IR codes.

For example, if the current spray is determined to be level 2 and I select level 3, the IR code should be transmitted once (2→3). For another example, if the current spray is determined to be level 2 and I select level 1, the IR code should be transmitted twice (2→3→1). For yet another example, if the current spray is level 1 and I select level 3, the IR code should be transmitted twice (1→2→3).

The possible combinations are:

1→2 (transmit IR code once)
1→3 (transmit IR code twice, 1→2→3)
2→3 (transmit IR code once)
2→1 (transmit IR code twice, 2→3→1)
3→1 (transmit IR code once)
3→2 (transmit IR code twice, 3→1→2)

Can this be achieved by lambda or any other means?

Any help would be appreciated.