Creating virtual lights that keep their state after HA restart?

Hi everyone! I’ve been fiddling with Virtual Components and Template Lights these weeks to create virtual devices to store data for further automations. One example is: there’s a virtual light called “Next Wash Cycle”, when that virtual light is on, there is an automation (through Node-RED) that defines the time when my washing machine will turn on the next day, depending on its brightness level.

This device isn’t directly connected to another device. Turning it on or off won’t trigger any devices instantly, Node-RED will use these attributes (on/off state, brightness level) to define a time variable later. I think I needed to clarify this because I see that most of the users who are creating template lights or virtual components have them linked to other services directly in the configuration.yaml, which is not my case.

I just found out that both the template lights and the virtual component lights don’t keep their state after rebooting HA. And currently I’m constantly trying things with HA and I restart it a couple of times a day. Also, I would like my system to survive a power outage.

So, can anyone recommend me a way to create virtual lights that keep their state after a restart? (I need some with only brightness and others with brightness and color). I already use many input_boolean helpers and they keep their states, it would be great to have helpers that work as dimmer lights.

Thanks for all the help!
Rodrigo

Is there a reason you cannot use input number helpers instead of a virtual light entity?

If you’re set on having all the different factors in a single entity, you might want to look at the Hass-Variables or Home-Assistant-Variables custom integrations

It can be done using template light and variable for keeping state. For turn_on and turn_off actions use var.set service.

1 Like

The main intention behind this was to be able to ask Google to set a value, and the current method I know for this is using a virtual light (and asking Google to set the light at X percent).

I will keep this in mind. It just surprises me that these things I thought were basic, aren’t easier to achieve. Anyway it’s good to learn different mthods :slight_smile:

R.

Ok great, I just created a variable to store brightness level, the thing is, how do I make the light get this value after a restart?? I can see how changing the light value sets the variable, and the variable value is kept after a restart. But the light is still off when restarted.

I did this:

light:
  - platform: template
    lights:
      kitchen_test_light:
        friendly_name: "Kitchen Test Light"
        turn_on:
          service: input_boolean.turn_on
          entity_id: input_boolean.kitchen_test_light
        turn_off:
          service: input_boolean.turn_off
          entity_id: input_boolean.kitchen_test_light
        set_level:
          - service: var.set
            data:
                entity_id: var.kitchen_test_light_level
                value: "{{ brightness }}"
        
var:
  kitchen_test_light_level:
    friendly_name: 'VAR Kitchen Test Light Level'
    initial_value: 0
    icon: mdi:bug

Thanks,
R.

I’m looking more into variables, and I would like to know if it’s possible to store two different attributes (state and level) in the same variable and how to set that from the light template.

I tried:

        set_level:
          - service: var.set
            data:
                entity_id: var.kitchen_test.level
                value: "{{ brightness }}"

But this shows an error in the config check saying the light template cannot use these attributes. How do I set the variable’s attributes from the light template?

Thanks,
Rodrigo

light:
  - platform: template
    lights:
      kitchen_test_light:
        friendly_name: "Kitchen Test Light"
        value_template: "{{ states('input_boolean.kitchen_test_light') }}"
        level_template: "{{ states('var.kitchen_test_light_level') }}"
        turn_on:
          service: input_boolean.turn_on
          entity_id: input_boolean.kitchen_test_light
        turn_off:
          service: input_boolean.turn_off
          entity_id: input_boolean.kitchen_test_light
        set_level:
          - service: var.set
            data:
                entity_id: var.kitchen_test_light_level
                value: "{{ brightness }}"

Template Light

1 Like

Thanks @IgorZg for your help! I was about to start creating virtual RGB lights in Tuya Smart IOT Platform :S but thanks to you I was finally able to understand the logic behind these templates.

I still find it too complicated… I hope they add virtual dimmers and RGB lights to the Helpers list soon.

I wonder if there isn’t a way to use a single variable with multiple attributes (one variable per virtual light instead of multiple input number/boolean).
I was able to create variables with attributes (‘level’ and ‘status’) and change the attributes from the Call Service panel, but I don’t know the syntax to do this from the configuration.yaml in the turn_on or off part.

Thanks again,
Rodrigo

1 Like

“I wonder if there isn’t a way to use a single variable with multiple attributes (one variable per virtual light instead of multiple input number/boolean).”

It is possible using another very usefull script: Home Assistant Entities Script

Best regards!

P.S.
If you need such thing often, maybe it can be done as blueprint and then easily used.

1 Like

Hi there, I’m having trouble installing this script. I have tried to search for it in HACS, but can’t find it. I’ve tried to add the github repo to my HACS instance, but I keep getting an error. Any help with this?

Thanks

Edit: Nevermind, I figured it out. You have to add python_script: into configuration.yaml, then create the <config>/python_scripts/ folder, then restart. (Full instructions here: https://www.home-assistant.io/integrations/python_script/)

After that, I went into HACS and it was immediately available for me to load