I’ve got three scenes for possible lighting conditions: all lights on, just the lamp, all lights off. What I want is an automation where I can press a button to cycle through from one scene to another. (Or if we want to get more complex, two buttons where I can cycle in either direction). I have the first version of this working where I manually set the conditions based on the device status. But I’m wondering if there’s a cleaner way to do it.
I think I could have one automation increment/decrement a counter, and another automation mapping the counter to a scene. But ideally I’d like the counter to be modified if someone changes the device state outside that automation. (Such as flipping the light switch off and on, which will reset the smart bulbs to on.) Is there a perfect way to do this?
#Toggling through scenes?
1 messages · Page 1 of 1 (latest)
It seems like I can use Scene on automations, but I can’t access whether they’re active or not. And it doesn’t seem like the scene gets marked as active if the devices go to that state without the scene being activated anyway.
So a scene is not "active" per se, It is stored set of states which get set when its "activated" but it itself is not active.
You could template an input select (dropdown) helper with your 3 options.
Then have an automation that triggers on this changing, which sets the right scene for what the input select has been changed to.
Then for your cycling you can have the button automation call input_select.select_next with the cycle flag targeting your helper. which will cycle the input select and therefore the above will active the scene you have cycled to.
This means you can have a dropdown to select easily on a dashboard too.
That’s slightly nicer than the counter, thanks. But is there no way to easily do the last bit (update the state so the input_select advances if the devices are adjusted to match another scene)?
I use a helper counter and works perfect, but I take note of that way, thanks
I am not sure what you mean?
Yeah its basically the same thing in concept, I like using input_select's though as it has some minor usage benefits 🙂
Lets say the scenes are
- lights on + lamp on
- lights off + lamp on
- lights off + lamp off
In that order.
Let’s say you’re on input_selext 2 (lights off + lamp off) and then instead of using the HA automation, someone manually presses the button on the smart outlet for the lamp to turn it off. Now the input_select is still on 2, but the actual state of the entities is equivalent to state 3. If you trigger the automation again, it will move the input_select to 3 (which won’t do anything) and then you have to trigger it again to get back to input_select 1 and go to lights on+lamp on.
Essentially what I’d want is some generic way to recognize that the devices have set in a way that matches a particular Scene, programmatically
I have a similar automation. In my case, I don't bother if the state of the devices are not aligned with the scene. It can be done, but I think it could be a nightmare to maintain whenever you update/change the automation. And you'll do, believe me 😅
That’s why I want a programmatic way to do it. Essentially some kind of trigger like “the devices were changed and now match the scene” that I could use to change the input_select to match. (Especially because if you do this wrong there’s a risk of an infinite loop)
Well, you can make a helper that activates if some of the devices' states changes after the scene activation
Right, I see what you are driving at now. I cant think of a way to do it straight away but there is probably a way somehow... let me try something.
You can do this with IF and THEN statements. If both are on, turn one of, if one is on turn both off, if both are off turn them both on.
Yes but that requires duplicating the scene in the automation. This is the thing I don’t want to do because it’s so cumbersome and error prone
Makes sense. Fair enough
So... I think I have a theory on how you could do it...
it is not exactly great...
so... in a template you can get a list of entities that are in a scene and therefore find their current state. but there is no way to get the scene state from templating...
so you could call a script that calls a shell_command that cats the scenes.yaml file then you use templating to pass that yaml to find a specified scene you want and get the target entities and states. then check the current states of those entities and return true/false.
so you then call this script with the scene name as a parameter. you trigger this when any of the entities in the scene change. so: light on -> does scene match? -> if true set input_select.
If there was a way of pulling the target states of a scenes entities in templating directly then it wouldn't be "THAT" hard to do...
would be something like this
{% set scene_match = namespace(match=true) %}
{% for entity in state_attr("scene.test_match", "entity_id") %}
{% set current_state = states(entity) %}
{% set target_state = "WE NEED THIS INFORMATION" %}
{% if current_state != target_state %}
{% set scene_match.match = false %}
{% endif %}
{% endfor %}
{{ scene_match.match }}
except with a way to fill in the obvious blank...
Pulling the yaml and parsing it is the only way I can think of to get the info. but that is a bit messy.
But... If you want to go down that road, I am sure its "doable"...
In programming, almost anything is possible 🙂
Is this maybe helpful?
https://community.home-assistant.io/t/how-to-cycle-through-scenes/633607/34?u=thefes
Yes, that’s because the state and available_options both use expand(area_entities(area)) as the starting point. It then filters it down to the scenes only. So indeed, the template will be re-evaluated when any entity in the area changes state, however it will only actually change if that entity was a scene entity. If you do it like this it w...
Yes and no. As I understand it, it seems like a better way to map scenes to the automation. That’s awesome! But it doesn’t make it understand changes to entities outside of a scene activation. Did I miss something?
No, you're right, that isn't in there. I kinda remember also writing up something for that
Smart idea! The template below checks if any of the entities used in the scene was changed more than half a second after the last time the scene was activated, without the need for a for loop. {% set scene_name = 'scene.new_scene' %} {% set scene_activated = as_datetime(states(scene_name)) %} {% set time_check = scene_activated + timedelta(sec...
This tracks if devices have changed since the scene being activated to see if its still "active".
However I believe (and I could be wrong here) the question was more along the lines of, if I manually set everything to the same as what is in a scene how can I get it to detect "this setup is the same as scene xyz" and set my the selector to that.
simple example with 1 device:
scene a: light is off
scene b: light is on
scene selector helper set to scene a
someone then turns on the light
**magic happens**
scene selector knows that although a scene action was not called that scene b is now active.
That magic is where we would need to trigger on a device changing and it checking the current states against target states of a scene to know what is happening.
Ah okay. That will be more difficult
What is the XY problem? Why do you need to now that? Is something critical at some point? 🙂
Other than this situation, There is perhaps a case to be made for having the ability to access the target states? e.g. For confirmation checks and such.
In think here its tying edges cases together with a nice neat bow on top but I think there might be other use cases for this potentially.
Maybe an action with an action respons? scene.get_config which you can compare with the current state?
That would work too, I was thinking of just having an attribute with a list then people can template whatever they want to do. An action is probably better though.
The XY problem here is the automation will do the wrong thing. Imagine if you had a two way cycle. You wanted to go from “all off” to “gentle light”. But the automation thinks you’re already in “gentle light” (because you manually turned off a light when watching tv for example) and goes to “all on”, which is too bright
Home Assistant did that back in the days. It doesn’t work.
For example, transitions on lights, rounding differences in brightness or color temperatures, slight changes in color interpretation (and many more of these). These cause them to be almost never active.
This was my fear. I could see that happening very easily.
I think the answer is either “accept that you’ll get weirdness” or try to do something clever with checking to see if entities have changed and just making the next transition to set the scene that you think it’s already in?
I think scenes does not work that way. AFAIK a scene is a snapshot of a state. So when you switch to that scene, all related device must change to the desired state. That said, last time I tinker with scenes, it doesn't work 🤣
Anyway, I think it's a nice way of learning and to use the brain
Leaving aside the concern I mentioned above, I'm trying to figure out if such a thing is possible. It must be because Home Assistant actually does something under the hood to figure out how to apply a scene
I'm sure it can be done, as I said earlier. The level of complexity is the thing 🙂
It'd be nice if templates could pull the paired list of entities/states set in the scene so you could pretty easily query if you're currently in that scene
I made a simple automation to cycle scenes. I'm was not at all interested in it taking into account manual adjustment, just activate the scene that was called the longest ago for in the area.
- variables:
s_id: >-
{% set area = 'Hall' %}
{% set scenes = area_entities(area)
| select('match', '^scene\.')
| list %}
{% set sorted = states
| selectattr('entity_id', 'in', scenes)
| sort(attribute='state', reverse=false)
| map(attribute='entity_id')
| list %}
{{ sorted[0] | default(None) }}
- action: scene.turn_on
metadata: {}
target:
entity_id: "{{ s_id }}"
data: {}