Concatenate entity id in Blueprint for service call

Hi,
I’m writing a blueprint to shut off the heat when a window or dor is open. And to restore the heat when the window is closed.
I want to create a scene before turning the heat off with has the name of the entity. That way i can have multiple window open event at the same time. I tried multiple thing in the scene.turn_on to concatenate it but to no avail.

Any help would be greatly appreciated.

action:
  - choose:
      - conditions:
          - condition: state
            entity_id: !input window_sensor
            state: "on"
        sequence:
          - service: scene.create
            data:
              scene_id: !input target_climate
              snapshot_entities: !input target_climate

          - service: climate.turn_off
            data: {}
            target:
              entity_id: !input target_climate
      - conditions:
          - condition: state
            entity_id: !input window_sensor
            state: "off"
        sequence:
          - service: scene.turn_on
            data: {}
            target:
              entity_id: "scene.{{!input target_climate}}"

!inputs cannot be put into templates. You need to convert them to variables first. Look at other Blueprints in the exchange, nearly all of them do it.

You may also want to use a unique name for the scene and not the same one every time. I find the best way to get a unique id on the fly is to use:

this.context.id

It should be unique every time the automation runs.

This way if the automation is triggered while another copy is running, it won’t kick an error.

Here is a place I use that in my config:

Thank you,
I have updated my blueprint and it does not give me an error when saving. However, when executing i have an error related to upper case in the entity id.

And I do not see how you deal with this in our example.


try something like this:

action:
  - variables:
      target_climate: "{{ input.target_climate }}"
      window_sensor: "{{ states(input.window_sensor) }}"
  - choose:
      - conditions:
          - condition: template
            value_template: "{{ window_sensor == 'on' }}"
        sequence:
          - service: scene.create
            data:
              scene_id: "{{ target_climate }}"
              snapshot_entities: "{{ target_climate }}"
          - service: climate.turn_off
            target:
              entity_id: "{{ target_climate }}"
      - conditions:
          - condition: template
            value_template: "{{ window_sensor == 'off' }}"
        sequence:
          - service: scene.turn_on
            data:
              entity_id: "scene.{{ target_climate }}"
            target: {}

Apparently “input” is undefined

yes thats normal, as i copied it from my code.
it was more a way to show you how to use the variables.

Thanks i figured out

blueprint:
  name: WindowGuard Climate Control
  description: This blueprint keeps an eye on your window sensors, taking a snapshot of the environment when they open. When a window is detected as open, it intelligently suspends your heating or cooling systems to conserve energy. Once the window is closed, it restores the previous climate settings.
  domain: automation
  input:
    window_sensor:
      name: Window Sensor
      description: Select the sensor that monitors your windows to control the heating. **Tip:** If you have multiple window sensors in a room, consider creating a sensor group.
      selector:
        entity:
          filter:
            - domain: binary_sensor
            - device_class: window
    target_climate:
      name: Heating Entity
      description: Choose the climate entity you want to control (e.g., your heater, AC, or thermostat).
      selector:
        entity:
          filter:
            - domain: climate

trigger:
  - platform: state
    entity_id: !input window_sensor

action:
  - variables:
      target_climate_var: !input target_climate
  - choose:
      - conditions:
          - condition: state
            entity_id: !input window_sensor
            state: "on"
        sequence:
          - service: scene.create
            data:
              scene_id: "{{ target_climate_var | replace('.', '_') }}_snapshot"
              snapshot_entities: !input target_climate

          - service: climate.turn_off
            data: {}
            target:
              entity_id: !input target_climate

      - conditions:
          - condition: state
            entity_id: !input window_sensor
            state: "off"
        sequence:
          - service: scene.turn_on
            data: {}
            target:
              entity_id: "scene.{{ target_climate_var | replace('.', '_') }}_snapshot"

aaah nice, glad you got it !

regards

1 Like

It would be more efficient and less prone to error if you did this in the variable section giving it a variable name and using the variable name in the automation instead of generating it multiple times thru the automation.
I also still suggest using a unique name like I suggested above

so it doesn’t crash if 2 copies of the blueprint basecode try to run at the same time.
But I’m glad you got it working.

Hi thanks for the feedback, wen i used the this.context.id, it generated captial letters and the blueprint failed because of that, looking at your automation i dit not figured out how you did not encountered this issue or how to solve it.

90801b5591e156f812e8550629efc422af08b934_2_690x172

add a lower filter then.

In variables…

my_scene_id = "{{ this.context.id | lower }}_snapshot"

Sorry for the edits there, had to go to developer - templates to get the right syntax.

1 Like

I’m running into weird issues where wen the boolean go from off to on and on to off the

Returns different slugs


blueprint:
  name: WindowGuard Climate Control TEST
  description: This blueprint keeps an eye on your window sensors, taking a snapshot of the environment when they open. When a window is detected as open, it intelligently suspends your heating or cooling systems to conserve energy. Once the window is closed, it restores the previous climate settings.
  source_url: https://gist.github.com/Redblockmasteur/d3602266850016123b55d278f95ba8d4
  domain: automation
  input:
    window_sensor:
      name: Window Sensor
      description: Select the sensor that monitors your windows to control the heating. **Tip:** If you have multiple window sensors in a room, consider creating a sensor group.
      selector:
        entity:
          filter:
            - domain: binary_sensor
            - device_class: window
    target_climate:
      name: Heating Entity
      description: Choose the climate entity you want to control (e.g., your heater, AC, or thermostat).
      selector:
        entity:
          filter:
            - domain: climate

trigger:
  - platform: state
    entity_id: !input window_sensor

action:
  - variables:
      target_climate_var: !input target_climate
      my_scene_id: "{{  this.context.id | lower }}_snapshot"
  - choose:
      - conditions:
          - condition: state
            entity_id: !input window_sensor
            state: "on"
        sequence:
          - service: scene.create
            data:
              scene_id: "{{ scene_id }}"
              snapshot_entities: !input target_climate

          - service: climate.turn_off
            data: {}
            target:
              entity_id: !input target_climate

      - conditions:
          - condition: state
            entity_id: !input window_sensor
            state: "off"
        sequence:
          - service: scene.turn_on
            data: {}
            target:
              entity_id: "scene.{{ scene_id }}"

OK, yes it will.
Guess that is not what you need then.
There is time between on and off… Hmmm
Well, this will get out of sync kind of easy if you manually control the setting at all or a lot of different scenarios. You will have to watch it closely. I also don’t think creating a scene like you are trying to do lives across an HA restart. I would suggest creating a scene in yaml and setting it using that name instead of creating it when you call the blueprint. then it will be there across restarts.

My idea is not going to be what you want, yours is better for this.
Mine is more for grab the scene, do a thing, restore the scene.

Sorry about that.

But you did find a bug in my code I didn’t know was there, so thanks for helping me out.

1 Like

Thank you for investing your time in addressing my issues. After careful consideration, I’ve decided to stick with the current process. I don’t foresee Home Assistant experiencing a restart during a window open event, especially given their infrequent and short occurrences during the winter. However, I do acknowledge the potential issue you’ve highlighted.

If my input has contributed to uncovering a bug in your code, I’m genuinely pleased to have played a part in that discovery.

You have a new subscriber :wink:

1 Like

I know er were are solved here, but I was just pointed to a new addition to the HA documentation on the subject.
If it doesn’t help you, perhaps it will help someone referring to this thread in the future.
Link to the specific (I think new) section discussing creating scenes on the fly. It mentions the scene_id needs to be lower case… Ha Ha…

1 Like