[Custom Component] Stateful Scenes

Hey :wave:, I would like to introduce a custom integration I created:

Stateful Scenes

Do you want to use your Home Assistant scenes in HomeKit, but get annoyed when the scenes do not stay ‘on’?

Stateful Scenes solves this problem by creating a switch for each scene and inferring the state of the scene by analysing the entities in the scene. Plus, when you activate a scene in Home Assistant, the scene will also turn on in HomeKit—magic!

Installation

HACS – custom repository

Click this button:

Or, in HACS go to integrations and then click the three buttons at the top right. From there, add a custom repository. Repository name is hugobloem/stateful_scenes and the category is integration.

Custom repository screenshot

HACS – default repository (not available yet)

Install via HACS by searching for stateful scenes in the integrations section

Manual

Or, clone the repository and copy the custom_components folder to your home assistant config folder.

git clone GitHub - hugobloem/stateful_scenes: Stateful Scenes in Home Assistant (Home Kit scene compatible) cp -r stateful_scenes/custom_components config/

Configuration

This integration is now configured via the config flow. After you have installed and restarted Home Assistant, go to Devices and Services, Add Integration, and search for Stateful Scenes. Alternatively, just click this button:

Config flow screenshot

Scene path

If your configuration has a different location for scenes you can change the location by changing the Scene path variable. By default, Home Assistant places all scenes inside scenes.yaml which is where this integration retrieves the scenes.

Rounding tolerance

Some attributes such as light brightness will be rounded off. Therefore, to assess whether the scene is active a tolerance will be applied. The default tolerance of 1 will work for rounding errors of ±1. If this does not work for your setup consider increasing this value.

Transition time

Furthermore, you can specify the default transition time for applying scenes. This will gradually change the lights of a scene to the specified state. It does need to be supported by your lights.

Supported attributes

Note that while all entity states are supported only some entity attributes are supported at the moment. For the entities listed in the table the state is supported as well as the attributes in the table. Please open an issue, if you want support for other entity attributes.

Entity Domain Attributes
light brightness, rgb_color, effect
cover position
media_player volume_level, source
fan direction, oscillating, percentage

Scene configurations

For each scene you can specify the individual transition time by changing the transition time variable on the scene’s device page.

HomeKit configuration

Once you have configured this integration, you can add the scenes to HomeKit. I assume that you already set up and configured the HomeKit integration. Expose the newly added switches to HomeKit. Then, in HomeKit define scenes for each Stateful Scenes switch.

Going forward

I have a few things I would like to add to this project, such as restoring entities to a previous state when turning off a scene. If you have any suggestions, please leave a comment, or open an issue on Github. Also, if you run into any problems do not hesitate to reach out on here, or through Github.

hugobloem/stateful_scenes

4 Likes

Thank you Hugo. This worked perfectly for my current needs. I am currently using 2 scenes for my office: Work and Off. I display a card with the following states in my office dashboard:

image

Now that I have the ability to do stateful scenes, I will apply this concept to other rooms.

1 Like

This is literally the best custom component ever. Scenes should be this smart.

Question though - when deleting a scene the stateful scene devices don’t seem to get cleaned up? Bug? PEBKAC? Help me obi-wan bloem

How did you achieve a manual state mode in this example

Can this support scenes created in Philips Hue? Obviously they don’t appear in scenes.yaml. They work in pretty much the same way though and I believe are significantly faster than trying to create the scene in HA. Would LOVE to have that option.

The difficulty is that it is not possible to read the Hue scene configuration. Or I would have to build a manual mode where it learns the state of the lights. Is that something you’d want?

I’d probably need to understand your integration better first (I don’t use it now as I don’t use HomeKit) - but given a large number of hue users, I think this could be worth it. I’m still struggling with scenes from Hue to HA.

I don’t have a Hue bridge but as I understand it Hue exposes a scene entity in HA which you can click to activate right?

I guess it could cycle through the Hue scenes to learn the state of lights whenever the scene is active

Yes that’s right. It exposes Hue scenes which can be used the same as scenes created in HA but you can’t edit them in HA (obviously) and I don’t think HA knows what the lights and settings are within that scene (AFAIK). Not sure if there are other differences. I guess HA just triggers it via the Hue Bridge.

…so I guess that doesn’t give you what you would need?

I’m gonna play around and see what I can come up with. Technically you could manually select what lights are in a scene, activate the scene, and then learn what the settings for a scene are. However, this may be more tricky than it seems. I will have a look though.

1 Like

Ok thanks. Let me know if you want me to test anything.

Thanks for your Efforts Hugo!

For me the reason I found your component, was my search how to pass on Hue scenes (with gradients on lights that support it, like the Signe Floor lamp, and for multiple lights).

I see the Hue scenes I set in the Hue app in the list of HASS entities and I can activate them. So I’ve added them to a card (Entities card with all the scenes). But there is no state visible (so I found your custom component) and I’m not sure what is the best way to pass these on to Homekit, so I can use Siri to activate themes.

But, if you’re looking into setting individual Hue lights, to (re)create a scene, unfortunately, HASS is not yet capable of setting gradients. Which for me was the reason to set the scenes in Hue in the first place (in stead of Homekit or HASS).

Thanks again! Hope you figure this out.

I have created a beta version with support for non-HA scenes. It is available in 1.3.0b0, please try it out if you can and report whether it is working or not.

1 Like

I have installed this integration and love it, but I have trouble to use it.
I installed it via HACS and habe one Scene from HomeAssistant to Homekit. Now I try to make the same thing with my other scenes, bit only get this unknown error:
grafik

Any idea? scenes.yaml is in the default folder

Is there more information in the logs, under Settings > System > Logs?

Can’t find any information in the logs, but now I got it working. I loaded it again and take a new beginning. Now I have some Switches, that are buttons. Scene 5s on, than automatically off :rofl:
I’m not use the beta. Should I have to give it a try?

Only a quick test with Hue scenes on 1.3.0beta1 but it seems to work - amazing! Will now go and integrate a few more scenes. It takes a bit of effort to setup but worth it!

Thanks for your work on this Hugo!

and a question:

So the integration works :slight_smile: In the Hue app, they have a different design on how they track scenes: If you select a scene, you can still change the overall brightness of the scene with a master brightness control and the scene still stays selected. As soon as you change an individual lamp, the scene ‘turns off’ or rather shows the scene as deactivated. I think that you can also change the level of an individual lamp but not turn it off or change the colour and the scene still shows as active.

Is that possible to replicate in this integration? How does that work for HA scenes?

And a final question - is it possible to ignore the HA scenes when configuring - or at least have an option to turn them off in the config?

Thanks!