How to stop duplicated code?

I have an automation and script for my windows and climate, if window open call script and shut climate off. I fwindow closes set climate to a temperature.

Here is a sample for open window in one room. I would like to write the code more so it can handle all my rooms with one code. Maybe with variables, groups or whatever. I have no idea was solutions home assistant can present here. Maybe someone here as similar ideas and a solution for me.

Can it be done with script variables? I read in the forum that entity id can not work with variables?

Automation
alias: Badezimmer Heizung aus wenn Fenster auf
hide_entity: true
trigger:
- platform: state
entity_id: binary_sensor.bathroom_window_sensor_9_0
state: ‘on’
action:
service: homeassistant.turn_on
entity_id: script.bathroom_window_open

Script
bathroom_window_open:
alias: “Badezimmerfenster auf - Heizung aus”
sequence:
- service: notify.Server
data:
message: ‘Badezimmerfenster offen, schalte Heizung aus’
- service: climate.set_temperature
data:
entity_id: climate.bathroom_heating_heating_1_2_1
temperature: 4

1 Like

If I understand what you are looking for …

You want a automation to turn off your climate control if any window in your home/apartment is opened and send a notification?

You can have multiple triggers and multiple actions in an automation. The below example should provide the same result as your above code, however, it has multiple triggers. Any one of the triggers will start the automation.

Automation:

  alias: Badezimmer Heizung aus wenn Fenster auf
  hide_entity: true
  trigger:
    - platform: state
      entity_id: binary_sensor.room1
      state: 'on'
    - platform: state
      entity_id: binary_sensor.room2
      state: 'on'
    - platform: state
      entity_id: binary_sensor.room3
      state: 'on'
  action:
    - service: notify.server
      data: 
	    message: 'Badezimmerfenster offen, schalte Heizung aus'
	- service: climate.set_temperature
	    entity_id: climate.bathroom_heating_heating_1_2_1
		temperature: 4

If you wanted the climate control to turn back on when all the windows are closed you would just need to change the trigger values to off and add a condition that all the windows are “off”

Automation:

  alias: Badezimmer Heizung aus wenn Fenster auf
  hide_entity: true
  trigger:
    - platform: state
      entity_id: binary_sensor.room1
      state: 'off'
    - platform: state
      entity_id: binary_sensor.room2
      state: 'off'
    - platform: state
      entity_id: binary_sensor.room3
      state: 'off'
condition:
  condition: and
  conditions:
    - condition: state
      entity_id: binary_sensor.room1
      state: off'
    - condition: state
      entity_id: binary_sensor.room2
      state: 'off'
    - condition: state
      entity_id: binary_sensor.room3
     state: 'off'
  action:
    - service: notify.server
      data: 
	    message: 'Alle Fenster geschlossen, schalten Heizung auf'
	- service: climate.set_temperature
	    entity_id: climate.bathroom_heating_heating_1_2_1
		temperature: #

No, I think he means something like (note, for illustration only, does not work :-):

alias: Heizung aus wenn Fenster auf, wo auch immer
  hide_entity: true
  trigger:
    - platform: state
      entity_id: binary_sensor.room1
      state: 'on'
    - platform: state
      entity_id: binary_sensor.room2
      state: 'on'
     ....
  action:
    - service: notify.server
      data: 
        message: '{{ event.room.name }} offen, schalte Heizung aus'
    - service: climate.set_temperature
        entity_id: '{{ event.room.climate_control }}'
	    temperature: 4

In a nutshell just write this one rule for all of the 120 rooms of his mansion and not copy-paste it a gazillion times.

Ahh, I missed that each room would need a specific message and climate control.

yes thats what i am looking for, is is possible to use variables like this? What i see is only inside data_template.

1 Like