Variables in yaml - to make code easy to ajust

Is there any way to get this yaml to work with variables at top?

ESP_MAC:   "A848FAC08D39"
zone0:   "Køkken"
zone1:   "Køkken1"
zone2:   "Køkken2"
zone3:   "Køkken3"
zone4:   "Køkken4"
zone5:   "Køkken5"
zone6:   "Køkken6"
zone7:   "Køkken7"
zone8:   "Køkken8"
zone9:   "Køkken9"
zone10:   "Køkken10"
zone11:   "Køkken11"
zone12:   "Køkken12"
zone13:   "Køkken13"
zone14:   "Køkken14"
zone15:   "Køkken15"

climate wavinAhc9000:
  - platform: mqtt
    name: "{{zone0}}"
    current_temperature_topic: "heat/floor{{ESP_MAC}}/0/current"
    temperature_command_topic: "heat/floor{{ESP_MAC}}/0/target_set"
    temperature_state_topic: "heat/floor{{ESP_MAC}}/0/target"
    mode_command_topic: "heat/floor{{ESP_MAC}}/0/mode_set"
    mode_state_topic: "heat/floor{{ESP_MAC}}/0/mode"
    modes:
      - "heat"
      - "off"
    availability_topic: "heat/floor{{ESP_MAC}}/online"
    payload_available: "True"
    payload_not_available: "False"
    qos: 0

  - platform: mqtt
    name: "{{zone1}}"
    current_temperature_topic: "heat/floor{{ESP_MAC}}/1/current"
    temperature_command_topic: "heat/floor{{ESP_MAC}}/1/target_set"
    temperature_state_topic: "heat/floor{{ESP_MAC}}/1/target"
    mode_command_topic: "heat/floor{{ESP_MAC}}/1/mode_set"
    mode_state_topic: "heat/floor{{ESP_MAC}}/1/mode"
    modes:
      - "heat"
      - "off"
    availability_topic: "heat/floor{{ESP_MAC}}/online"
    payload_available: "True"
    payload_not_available: "False"
    qos: 0

  - platform: mqtt
    name: "{{zone2}}"
    current_temperature_topic: "heat/floor{{ESP_MAC}}/2/current"
    temperature_command_topic: "heat/floor{{ESP_MAC}}/2/target_set"
    temperature_state_topic: "heat/floor{{ESP_MAC}}/2/target"
    mode_command_topic: "heat/floor{{ESP_MAC}}/2/mode_set"
    mode_state_topic: "heat/floor{{ESP_MAC}}/2/mode"
    modes:
      - "heat"
      - "off"
    availability_topic: "heat/floor{{ESP_MAC}}/online"
    payload_available: "True"
    payload_not_available: "False"
    qos: 0

  - platform: mqtt
    name: "{{zone3}}"
    current_temperature_topic: "heat/floor{{ESP_MAC}}/3/current"
    temperature_command_topic: "heat/floor{{ESP_MAC}}/3/target_set"
    temperature_state_topic: "heat/floor{{ESP_MAC}}/3/target"
    mode_command_topic: "heat/floor{{ESP_MAC}}/3/mode_set"
    mode_state_topic: "heat/floor{{ESP_MAC}}/3/mode"
    modes:
      - "heat"
      - "off"
    availability_topic: "heat/floor{{ESP_MAC}}/online"
    payload_available: "True"
    payload_not_available: "False"
    qos: 0

  - platform: mqtt
    name: "{{zone4}}"
    current_temperature_topic: "heat/floor{{ESP_MAC}}/4/current"
    temperature_command_topic: "heat/floor{{ESP_MAC}}/4/target_set"
    temperature_state_topic: "heat/floor{{ESP_MAC}}/4/target"
    mode_command_topic: "heat/floor{{ESP_MAC}}/4/mode_set"
    mode_state_topic: "heat/floor{{ESP_MAC}}/4/mode"
    modes:
      - "heat"
      - "off"
    availability_topic: "heat/floor{{ESP_MAC}}/online"
    payload_available: "True"
    payload_not_available: "False"
    qos: 0

  - platform: mqtt
    name: "{{zone5}}"
    current_temperature_topic: "heat/floor{{ESP_MAC}}/5/current"
    temperature_command_topic: "heat/floor{{ESP_MAC}}/5/target_set"
    temperature_state_topic: "heat/floor{{ESP_MAC}}/5/target"
    mode_command_topic: "heat/floor{{ESP_MAC}}/5/mode_set"
    mode_state_topic: "heat/floor{{ESP_MAC}}/5/mode"
    modes:
      - "heat"
      - "off"
    availability_topic: "heat/floor{{ESP_MAC}}/online"
    payload_available: "True"
    payload_not_available: "False"
    qos: 0

  - platform: mqtt
    name: "{{zone6}}"
    current_temperature_topic: "heat/floor{{ESP_MAC}}/6/current"
    temperature_command_topic: "heat/floor{{ESP_MAC}}/6/target_set"
    temperature_state_topic: "heat/floor{{ESP_MAC}}/6/target"
    mode_command_topic: "heat/floor{{ESP_MAC}}/6/mode_set"
    mode_state_topic: "heat/floor{{ESP_MAC}}/6/mode"
    modes:
      - "heat"
      - "off"
    availability_topic: "heat/floor{{ESP_MAC}}/online"
    payload_available: "True"
    payload_not_available: "False"
    qos: 0

  - platform: mqtt
    name: "{{zone7}}"
    current_temperature_topic: "heat/floor{{ESP_MAC}}/7/current"
    temperature_command_topic: "heat/floor{{ESP_MAC}}/7/target_set"
    temperature_state_topic: "heat/floor{{ESP_MAC}}/7/target"
    mode_command_topic: "heat/floor{{ESP_MAC}}/7/mode_set"
    mode_state_topic: "heat/floor{{ESP_MAC}}/7/mode"
    modes:
      - "heat"
      - "off"
    availability_topic: "heat/floor{{ESP_MAC}}/online"
    payload_available: "True"
    payload_not_available: "False"
    qos: 0

  - platform: mqtt
    name: "{{zone8}}"
    current_temperature_topic: "heat/floor{{ESP_MAC}}/8/current"
    temperature_command_topic: "heat/floor{{ESP_MAC}}/8/target_set"
    temperature_state_topic: "heat/floor{{ESP_MAC}}/8/target"
    mode_command_topic: "heat/floor{{ESP_MAC}}/8/mode_set"
    mode_state_topic: "heat/floor{{ESP_MAC}}/8/mode"
    modes:
      - "heat"
      - "off"
    availability_topic: "heat/floor{{ESP_MAC}}/online"
    payload_available: "True"
    payload_not_available: "False"
    qos: 0

  - platform: mqtt
    name: "{{zone9}}"
    current_temperature_topic: "heat/floor{{ESP_MAC}}/9/current"
    temperature_command_topic: "heat/floor{{ESP_MAC}}/9/target_set"
    temperature_state_topic: "heat/floor{{ESP_MAC}}/9/target"
    mode_command_topic: "heat/floor{{ESP_MAC}}/9/mode_set"
    mode_state_topic: "heat/floor{{ESP_MAC}}/9/mode"
    modes:
      - "heat"
      - "off"
    availability_topic: "heat/floor{{ESP_MAC}}/online"
    payload_available: "True"
    payload_not_available: "False"
    qos: 0

  - platform: mqtt
    name: "{{zone10}}"
    current_temperature_topic: "heat/floor{{ESP_MAC}}/10/current"
    temperature_command_topic: "heat/floor{{ESP_MAC}}/10/target_set"
    temperature_state_topic: "heat/floor{{ESP_MAC}}/10/target"
    mode_command_topic: "heat/floor{{ESP_MAC}}/10/mode_set"
    mode_state_topic: "heat/floor{{ESP_MAC}}/10/mode"
    modes:
      - "heat"
      - "off"
    availability_topic: "heat/floor{{ESP_MAC}}/online"
    payload_available: "True"
    payload_not_available: "False"
    qos: 0

  - platform: mqtt
    name: "{{zone11}}"
    current_temperature_topic: "heat/floor{{ESP_MAC}}/11/current"
    temperature_command_topic: "heat/floor{{ESP_MAC}}/11/target_set"
    temperature_state_topic: "heat/floor{{ESP_MAC}}/11/target"
    mode_command_topic: "heat/floor{{ESP_MAC}}/11/mode_set"
    mode_state_topic: "heat/floor{{ESP_MAC}}/11/mode"
    modes:
      - "heat"
      - "off"
    availability_topic: "heat/floor{{ESP_MAC}}/online"
    payload_available: "True"
    payload_not_available: "False"
    qos: 0

  - platform: mqtt
    name: "{{zone12}}"
    current_temperature_topic: "heat/floor{{ESP_MAC}}/12/current"
    temperature_command_topic: "heat/floor{{ESP_MAC}}/12/target_set"
    temperature_state_topic: "heat/floor{{ESP_MAC}}/12/target"
    mode_command_topic: "heat/floor{{ESP_MAC}}/12/mode_set"
    mode_state_topic: "heat/floor{{ESP_MAC}}/12/mode"
    modes:
      - "heat"
      - "off"
    availability_topic: "heat/floor{{ESP_MAC}}/online"
    payload_available: "True"
    payload_not_available: "False"
    qos: 0

  - platform: mqtt
    name: "{{zone13}}"
    current_temperature_topic: "heat/floor{{ESP_MAC}}/13/current"
    temperature_command_topic: "heat/floor{{ESP_MAC}}/13/target_set"
    temperature_state_topic: "heat/floor{{ESP_MAC}}/13/target"
    mode_command_topic: "heat/floor{{ESP_MAC}}/13/mode_set"
    mode_state_topic: "heat/floor{{ESP_MAC}}/13/mode"
    modes:
      - "heat"
      - "off"
    availability_topic: "heat/floor{{ESP_MAC}}/online"
    payload_available: "True"
    payload_not_available: "False"
    qos: 0

  - platform: mqtt
    name: "{{zone14}}"
    current_temperature_topic: "heat/floor{{ESP_MAC}}/14/current"
    temperature_command_topic: "heat/floor{{ESP_MAC}}/14/target_set"
    temperature_state_topic: "heat/floor{{ESP_MAC}}/14/target"
    mode_command_topic: "heat/floor{{ESP_MAC}}/14/mode_set"
    mode_state_topic: "heat/floor{{ESP_MAC}}/14/mode"
    modes:
      - "heat"
      - "off"
    availability_topic: "heat/floor{{ESP_MAC}}/online"
    payload_available: "True"
    payload_not_available: "False"
    qos: 0

  - platform: mqtt
    name: "{{zone15}}"
    current_temperature_topic: "heat/floor{{ESP_MAC}}/15/current"
    temperature_command_topic: "heat/floor{{ESP_MAC}}/15/target_set"
    temperature_state_topic: "heat/floor{{ESP_MAC}}/15/target"
    mode_command_topic: "heat/floor{{ESP_MAC}}/15/mode_set"
    mode_state_topic: "heat/floor{{ESP_MAC}}/15/mode"
    modes:
      - "heat"
      - "off"
    availability_topic: "heat/floor{{ESP_MAC}}/online"
    payload_available: "True"
    payload_not_available: "False"
    qos: 0    

sensor wavinBattery:
  - platform: mqtt
    state_topic: "heat/floor{{ESP_MAC}}/0/battery"
    availability_topic: "heat/floor{{ESP_MAC}}/online"
    payload_available: "True"
    payload_not_available: "False"
    name: "{{zone0}}"
    unit_of_measurement: "%"
    device_class: battery
    qos: 0
    
  - platform: mqtt
    state_topic: "heat/floor{{ESP_MAC}}/1/battery"
    availability_topic: "heat/floor{{ESP_MAC}}/online"
    payload_available: "True"
    payload_not_available: "False"
    name: "{{zone1}}"
    unit_of_measurement: "%"
    device_class: battery
    qos: 0
    
  - platform: mqtt
    state_topic: "heat/floor{{ESP_MAC}}/2/battery"
    availability_topic: "heat/floor{{ESP_MAC}}/online"
    payload_available: "True"
    payload_not_available: "False"
    name: "{{zone2}}"
    unit_of_measurement: "%"
    device_class: battery
    qos: 0
    
  - platform: mqtt
    state_topic: "heat/floor{{ESP_MAC}}/3/battery"
    availability_topic: "heat/floor{{ESP_MAC}}/online"
    payload_available: "True"
    payload_not_available: "False"
    name: "{{zone3}}"
    unit_of_measurement: "%"
    device_class: battery
    qos: 0
    
  - platform: mqtt
    state_topic: "heat/floor{{ESP_MAC}}/4/battery"
    availability_topic: "heat/floor{{ESP_MAC}}/online"
    payload_available: "True"
    payload_not_available: "False"
    name: "{{zone4}}"
    unit_of_measurement: "%"
    device_class: battery
    qos: 0
    
  - platform: mqtt
    state_topic: "heat/floor{{ESP_MAC}}/5/battery"
    availability_topic: "heat/floor{{ESP_MAC}}/online"
    payload_available: "True"
    payload_not_available: "False"
    name: "{{zone5}}"
    unit_of_measurement: "%"
    device_class: battery
    qos: 0
    
  - platform: mqtt
    state_topic: "heat/floor{{ESP_MAC}}/6/battery"
    availability_topic: "heat/floor{{ESP_MAC}}/online"
    payload_available: "True"
    payload_not_available: "False"
    name: "{{zone6}}"
    unit_of_measurement: "%"
    device_class: battery
    qos: 0
    
  - platform: mqtt
    state_topic: "heat/floor{{ESP_MAC}}/7/battery"
    availability_topic: "heat/floor{{ESP_MAC}}/online"
    payload_available: "True"
    payload_not_available: "False"
    name: "{{zone7}}"
    unit_of_measurement: "%"
    device_class: battery
    qos: 0
    
  - platform: mqtt
    state_topic: "heat/floor{{ESP_MAC}}/8/battery"
    availability_topic: "heat/floor{{ESP_MAC}}/online"
    payload_available: "True"
    payload_not_available: "False"
    name: "{{zone8}}"
    unit_of_measurement: "%"
    device_class: battery
    qos: 0
    
  - platform: mqtt
    state_topic: "heat/floor{{ESP_MAC}}/9/battery"
    availability_topic: "heat/floor{{ESP_MAC}}/online"
    payload_available: "True"
    payload_not_available: "False"
    name: "{{zone9}}"
    unit_of_measurement: "%"
    device_class: battery
    qos: 0
    
  - platform: mqtt
    state_topic: "heat/floor{{ESP_MAC}}/10/battery"
    availability_topic: "heat/floor{{ESP_MAC}}/online"
    payload_available: "True"
    payload_not_available: "False"
    name: "{{zone10}}"
    unit_of_measurement: "%"
    device_class: battery
    qos: 0
    
  - platform: mqtt
    state_topic: "heat/floor{{ESP_MAC}}/11/battery"
    availability_topic: "heat/floor{{ESP_MAC}}/online"
    payload_available: "True"
    payload_not_available: "False"
    name: "{{zone11}}"
    unit_of_measurement: "%"
    device_class: battery
    qos: 0
    
  - platform: mqtt
    state_topic: "heat/floor{{ESP_MAC}}/12/battery"
    availability_topic: "heat/floor{{ESP_MAC}}/online"
    payload_available: "True"
    payload_not_available: "False"
    name: "{{zone12}}"
    unit_of_measurement: "%"
    device_class: battery
    qos: 0
    
  - platform: mqtt
    state_topic: "heat/floor{{ESP_MAC}}/13/battery"
    availability_topic: "heat/floor{{ESP_MAC}}/online"
    payload_available: "True"
    payload_not_available: "False"
    name: "{{zone13}}"
    unit_of_measurement: "%"
    device_class: battery
    qos: 0
    
  - platform: mqtt
    state_topic: "heat/floor{{ESP_MAC}}/14/battery"
    availability_topic: "heat/floor{{ESP_MAC}}/online"
    payload_available: "True"
    payload_not_available: "False"
    name: "{{zone14}}"
    unit_of_measurement: "%"
    device_class: battery
    qos: 0
    
  - platform: mqtt
    state_topic: "heat/floor{{ESP_MAC}}/15/battery"
    availability_topic: "heat/floor{{ESP_MAC}}/online"
    payload_available: "True"
    payload_not_available: "False"
    name: "{{zone15}}"
    unit_of_measurement: "%"
    device_class: battery
    qos: 0

Secrets probably work.

what do you meen by “Secrets”?
Can you change the code to what you think it should be?

secrets.yaml.

I don’t use secrets in my config but if you search for it then you will see.
The secrets.yaml is a file where you place passwords and such, and in the config you reference it with !secrets… something… I don’t know.
Anyways that way your variables could be secrets.

Okay but the variables does not need to be secret i just want to wright the MAC once at the top of the code :slight_smile:

I understand that.
But this is how the configuration is limited.

There is no global variables as far as I know. The only place you can have variables is within a template, and this template is limited to the place it is positioned so all variables that you use in one template dies and can’t be used in the other template.
The only two ways to get around that is either secrets or input_text (or sensors, or entities in some form).

You could create entities for all variables and then reference them using states or state_attribute.
Or use the secrets.

I don’t know which is the best but I would probably go with secrets since it’s probably more universal.

it’s easiest is to use input_text helpers with the text you want.

or you can use a custom integration called “hass-variables

you can create those input_texts/variables and use them anywhere in HA as you can any entity.

Hi @Dan_Nielsen ,

Could you pleas explain a bit more why you want to change your current solution where actually use kind of local constants.

Is it because you want to refer to these values different places in automations or different files?
If so, then yes, variables is the right choice .
Here you can define both a main state value and as many related attributes of you own choice as needed.

Here is an example of one of my variables.

variable: 
  - sensor_openclose3:
      name: "vindue spise"
      value: false 
      restore: true
      attributes:
        friendly_name: 'Spisestue vindue'
        icon: mdi:silverware-variant
        open: '111001010000001100001010'   #E503 0A
        close: '111001010000001100001110'   #E503 0E

In your case it could be:

variable: 
  - esp_mac:
      name: "ESP_MAC"
      value: "A848FAC08D39" 
      restore: true
      attributes:
        zone0: 'Køkken'
        zone1: 'Køkken1'
        zone2: 'Køkken2'
        zone3: 'Køkken3'
        zone4: 'Køkken4'
        zone5: 'Køkken5'
        zone6: 'Køkken6'
        zone7: 'Køkken7'
        zone8: 'Køkken8'
        zone9: 'Køkken9'
        zone10: 'Køkken10'
        zone11: 'Køkken11'
        zone12: 'Køkken12'
        zone13: 'Køkken13'
        zone14: 'Køkken14'
        zone15: 'Køkken15'

And you can refer to it in your code, like any other sensor, like this:

climate wavinAhc9000:
  - platform: mqtt
    name: {{ state_attr ('sensor.esp_mac','zone0')}}
    current_temperature_topic: "heat/floor{{ states ('sensor.esp_mac')}}/0/current"
    temperature_command_topic: "heat/floor{{ states ('sensor.esp_mac')}}0/target_set"
    temperature_state_topic: "heat/floor{{ states ('sensor.esp_mac')}}/0/target"
    mode_command_topic: "heat/floor{{ states ('sensor.esp_mac')}}/0/mode_set"
    mode_state_topic: "heat/floor{{ states ('sensor.esp_mac')}}/0/mode"
    modes:
      - "heat"
      - "off"
    availability_topic: "heat/floor{{ states ('sensor.esp_mac')}}/online"
    payload_available: "True"
    payload_not_available: "False"
    qos: 0

Just discovered that this is an old thread . Don’t know whether it is still relevant for you, if you figured it out your self or found another solution.
Any way, let me know if you need further explanation