Deleting a dynamically created scene within the same automation

I just got introduced to the scene functions and have been moving over to them. I have 5 Govee HS800 WiFi bulbs and wish to use them as indicators of various issues throughout the house. For example, I have a scene to activate if the bathroom sink leak detector turns on to turn these to yellow and dim to 100%.

I saw the scene create function and the scene delete function and it says it deletes dynamically created scenes. However; when I enter the yaml code to do it, it does not like the delete part of it. If I could get this working, there are a lot of cool things I could be doing with this.

Here is the code. ← are areas of interest.

alias: “Bathroom: Sink Leak Detected”
description: “”
trigger:

  • platform: state
    entity_id:
    • binary_sensor.bathroom_sink_leak_detection_sensor_state_any
      to: “on”
      condition:
      action:
  • service: switch.turn_off
    data: {}
    target:
    entity_id:
    - switch.main_floor_cold_water_shutoff
    - switch.main_floor_hot_water_shutoff
  • service: notify.mobile_app_sm_s908u
    data:
    message: >-
    {{ trigger.entity_id }}, Date: {{ now().strftime(‘%Y-%m-%d’) }}, Time:
    {{ now().strftime(‘%H:%M’) }}
    title: Bathroom Sink Leak Detected
  • service: scene.create ← Here is where I create the temporary scene.
    metadata: {}
    data:
    snapshot_entities:
    - light.alarm_indicator_lights <–This is a light group I made of those HS300 lights.
    scene_id: bathroom_sink_leak_detected_temp <–Here is the name of the temporary scene
  • service: scene.turn_on
    metadata: {}
    target:
    entity_id: scene.alarm_bathroom_sink_leak <–This is the indicator scene, not the temp scene, to turn those lights to yellow.
  • wait_for_trigger:
    • platform: state
      entity_id:
      • binary_sensor.bathroom_sink_leak_detection_sensor_state_any
        to: “off”
        for:
        hours: 0
        minutes: 5
        seconds: 0
  • service: scene.turn_on
    metadata: {}
    target:
    entity_id: bathroom_sink_leak_detected_temp <–This is to turn back on that temporary scene.
  • service: scene.delete
    metadata: {}
    data:
    entity_id: bathroom_sink_leak_detected_temp <–This is to delete that temporary scene and this is where it gets mad at me.
    mode: single

For your scene.delete, it should look like this:

- service: scene.delete
    metadata: {}
    data: {}
    target:
      entity_id: scene.bathroom_sink_leak_detected_temp 

Wow! That was fast. But, unfortunately, it still is giving me that error when I try to save it.

alias: “Bathroom: Sink Leak Detected”
description: “”
trigger:

  • platform: state
    entity_id:
    • binary_sensor.bathroom_sink_leak_detection_sensor_state_any
      to: “on”
      condition:
      action:
  • service: switch.turn_off
    data: {}
    target:
    entity_id:
    - switch.main_floor_cold_water_shutoff
    - switch.main_floor_hot_water_shutoff
  • service: notify.mobile_app_sm_s908u
    data:
    message: >-
    {{ trigger.entity_id }}, Date: {{ now().strftime(‘%Y-%m-%d’) }}, Time:
    {{ now().strftime(‘%H:%M’) }}
    title: Bathroom Sink Leak Detected
  • service: scene.create
    metadata: {}
    data:
    snapshot_entities:
    - light.alarm_indicator_lights
    scene_id: bathroom_sink_leak_detected_temp
  • service: scene.turn_on
    metadata: {}
    target:
    entity_id: scene.alarm_bathroom_sink_leak
  • wait_for_trigger:
    • platform: state
      entity_id:
      • binary_sensor.bathroom_sink_leak_detection_sensor_state_any
        to: “off”
        for:
        hours: 0
        minutes: 5
        seconds: 0
  • service: scene.turn_on
    metadata: {}
    target:
    entity_id: bathroom_sink_leak_detected_temp
  • service: scene.delete
    metadata: {}
    data: {}
    target:
    entity_id: scene.bathroom_sink_leak_detected_temp
    mode: single

What error is it throwing? I use that same snippet of code without issues, so I’m not sure what it’s barking about. :slight_smile:

EDIT: Wait a second… is the mode: single at the top level or in the service: scene.delete call?

Also, can you edit the message and include your code in backticks? It’ll format properly then.

Thank you. The forum prompts me to change it from my paste. I will choose that option (been burned with forums screwing up formatting).

This is the error:
Message Malformed: not a valid value for dictionary value @ data[‘action’][5][‘target’][‘entity_id’]

I think it stems from the fact that “bathroom_sink_leak_detected_temp” doesn’t yet exist because it was not created. Do I temporarily create it as a null scene, finish the automation, then delete it?

Here is the code with me answering yes to the reformatting question when I post it:

alias: "Bathroom: Sink Leak Detected"
description: ""
trigger:
  - platform: state
    entity_id:
      - binary_sensor.bathroom_sink_leak_detection_sensor_state_any
    to: "on"
condition: []
action:
  - service: switch.turn_off
    data: {}
    target:
      entity_id:
        - switch.main_floor_cold_water_shutoff
        - switch.main_floor_hot_water_shutoff
  - service: notify.mobile_app_sm_s908u
    data:
      message: >-
        {{ trigger.entity_id }}, Date: {{ now().strftime('%Y-%m-%d') }}, Time:
        {{ now().strftime('%H:%M') }}
      title: Bathroom Sink Leak Detected
  - service: scene.create
    metadata: {}
    data:
      snapshot_entities:
        - light.alarm_indicator_lights
      scene_id: bathroom_sink_leak_detected_temp
  - service: scene.turn_on
    metadata: {}
    target:
      entity_id: scene.alarm_bathroom_sink_leak
  - wait_for_trigger:
      - platform: state
        entity_id:
          - binary_sensor.bathroom_sink_leak_detection_sensor_state_any
        to: "off"
        for:
          hours: 0
          minutes: 5
          seconds: 0
  - service: scene.turn_on
    metadata: {}
    target:
      entity_id: bathroom_sink_leak_detected_temp
  - service: scene.delete
    metadata: {}
    data: {}
    target:
      entity_id: scene.bathroom_sink_leak_detected_temp
mode: single

1 Like

His code didn’t have correct indentation. From your original post, just change
bathroom_sink_leak_detected_temp

to
scene.bathroom_sink_leak_detected_temp

whenever you reference the entity_id.

2 Likes

Argh! I was just typing that… lol Thanks @petro

1 Like

I swear I do not drink when I work on this stuff…THANK YOU. Yes, I freely admit I am a noob at coding. Thank you both for your help.

1 Like