Solved: Automation: Filter scenes and trigger the last one used

Hello,

I have some scenes:

scene.HueRoomA_SceneNameA
scene.HueRoomA_SceneNameB
scene.HueRoomA_SceneNameC
scene.HueRoomA_SceneNameD
etc.

I would like to know in an automation which of the scenes from scene.RoomA_* was triggered last in order to trigger it again.

Does anyone have an idea how I can do this?
Perhaps via a template helper which I can use to trigger the last scene?

PS: I don’t want to list the scenes in the script, as these come from Philips Hue and some may be added or removed. What I can make sure of, however, is that the scenes always start with the same prefix.

You can use this template in your automation to get a list of the entity_ids of all scenes whose object_id begins with HueRoomA_SceneName.

{{ states.scene
 | selectattr('object_id', 'match', 'HueRoomA_SceneName')
 | map(attribute='entity_id') | list }}

Modify the HueRoomA_SceneName string to meet your specific needs. Let me know if you need additional assistance to customize the template. For example the next step would be to determine which scene’s state contains the latest datetime value.

{{ expand(states.scene 
  | selectattr('object_id', 'match', 'HueRoomA_SceneName')
  | map(attribute='entity_id'))
  | rejectattr('state', 'in', ['unknown', 'unavailable'])
  | sort(attribute='state')
  | map(attribute='entity_id')
  | list | last }}
3 Likes

Hi @123 Thank you for your quick help.

Ok I’ve only been using Home Assistant for 2 months :smiley:
I’m still not quite sure where to insert your script into the automation. I tried it once in a helper, but nothing happens except that the script is not reported as faulty.

Can you tell me where I have to insert the script? And what do I see then?

I think I have to add your script to the automation under Wait-Template as an action, right?

What exactly did you mean when you said you wanted to “trigger it again”?

Did you want to turn on the most recent scene using scene.turn_on?

That would mean you want to wait until the most recently activated scene is activated again?

It’s no longer clear to me what you want to do with the most recently used scene.

Hi @123,

Example:

Yes, I would like to reactivate the last scene used in a room. So Turn_on.

For example, a wait template that does the following.

  • Filter scenes that start with “HueRoomA_SceneName”.
  • Filter the last used scene.
  • Activate the last used scene.

Background information:
I control my Philips Hue lamps with relays.
So they are dead when not in use.
Ceiling light, bedside tables, TV lamp, etc.

  • Now, for example, the ceiling light is switched on.
  • A scene is changed by a person.
  • Now the light on the bedside table is switched on.

Now I would like to trigger the last scene that was used again via automation so that the lights also receive the same scene.
As they were without power and did not know which scene was used.

What you want to do is accomplished with scene.turn_on. I don’t see a need for a wait_template, which is part of an automation’s actions, not its triggers. Unless you planned to trigger on one activity and then wait for another activity to occur. Is that what you want to do?

What were you planning to use for the automation’s trigger(s)?

No, I just want to find out what the last scene for the room was and activate it again.

I don’t know where to insert your script.

Hello @123 ,
Could you perhaps give me a Yaml configuration for an automation where the example is included.

  • Search for scenes with prefix HueRoomA_SceneName
  • Search for the last used scene
  • Set Last Scene scene.turn_on

It is a mystery to me where I have to install this.

OK, I have now understood where I apparently have to install your script.
Under Developer tools → Templates right?

The result is already correct. :smiley:

That doesn’t seem to be the way to do it either.

Ok, I’m not getting an error, but it doesn’t seem to be right yet.

OK, I now also have a template helper that shows me the right scene.
Soon I might be able to use something in an automation ;D

Hi @123,

OK, that’s also wrong.

I don’t know how to get the result from you into the automation so that I can use it with scene.turn_on.

At least I have already learned a few new things. But now I have to wait for your help.

Rock \W/

Ich habs doch noch raus bekommen :smiley: Mir fehlten gänsefüßchen „"

service: scene.turn_on
metadata: {}
target:
  entity_id: "{{ expand(states.scene 
  | selectattr('object_id', 'match', 'dg_dachboden_kinderzimmer_huez_')
  | map(attribute='entity_id'))
  | rejectattr('state', 'in', ['unknown', 'unavailable'])
  | sort(attribute='state')
  | map(attribute='entity_id')
  | list | last }}"

@123 Taras: Thank you for your help :slight_smile:

You’re welcome!

I’m glad to hear that you now understand that in order to activate a scene you must use the scene.turn_on service call.

Please consider marking my post above with the Solution tag. It will automatically place a checkmark next to the topic’s title which indicates that this topic is solved. This helps other users find answers to similar questions.

For more information about the Solution tag, refer to guideline 21 in the FAQ.

@123

I have been spending 3 days trying to figure this problem out and you came in with the perfect answer. Thank you so much!

You’re welcome!

I’m glad you found my post. It might have been a bit easier to find had @LHBL2003 marked it with the Solution tag.

There’s a blueprint for this now