Create custom sensor for light

I have light which can be controlled via ir blaster and has 3 color modes. When i want change color mode then i just press button until right color mode is arrived.

I have connected shelly relay to light. I know that every light color mode has specified power wattage range.

So is it possible make sensor where i define power wattage range for each color mode and when I need for example “white color mode” then it cycles through 3 preset (which has defined power wattage condition) and choose right one?

You should be able to do this with a template sensor. I use the following to determine if a tv is on, off or on standby, but you can adjust this slightly for your use case:

template:
  sensor:
    - name: "TV Power State"
      unique_id: tv_power_state
      state: >
        {% if states('sensor.tuya_plug_lounge_tv_active_power')|int(0)<3%}
        off
        {% elif states('sensor.tuya_plug_lounge_tv_active_power')|int(0)<100%}
        standby
        {%else%}
        on
        {%endif%}

If you want to track more than just three colours, you could add additional elif statements

Yeah, but this is only for viewing current state.
I need it cycles through color modes by pressing “switch color” and when it finds right color which is defined by power wattage then it stops cycling and choose that color.

Ah ok,

So if the light is controlled via the ir blaster, can the ir blaster be controlled by home assistant ?

I think using active power would be too slow. Devices like this only usually update every 30 seconds or more, so it would take a long time to cycle through colour modes, waiting on the active power reading to tell you which colour its at.

You could do something like creating an input helper that represents the current colour (maybe as a number), and in your automation that controls the IR blaster, every time you fire the automation you increase the number by 1, until you get 3, then loop back to 1. You could then put a loop in your automation until you reach the desired colour (input helper value).

Right now i use loop autmation like you described. But lately few weeks ago tuya integration lags. Sometimes it wont activate first “change color mode” and then all sequence is wrong again. :slight_smile:

Maybe, at the start of the automation, you check the current power level of the light to check that it matches what you are expecting and that its in sync with the input helper.

So if the power level of the bulb is say 50 watts, which you think should be (say) red, but your input helper thinks its on green, then you could reset the input helper to red and start your loops ?

This way it should always re-sync the helper with the state of the light.